spring接收参数

时间:2021-10-08 18:34:54
public class LogonModel
{ private String UserName;
private String Password; public String getUserName() {
return UserName;
} public void setUserName(String userName) {
this.UserName = userName;
} public String getPassword() {
return Password;
} public void setPassword(String password) {
this.Password = password;
}
}

定义Bean

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.spinfo.entity.User; @Controller
public class LoginController{ @RequestMapping("/hello")
public String handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception
{
System.out.println("运行springmvc2--Controller");
return "login";
}
/**
* SpringMVC接收表单参数
* 第一种方式:
* 直接把表单的参数写在Controller相应的方法的形参中,返回一个String,该String表示成功跳转到的页面的名称:String.jsp,其路径通过spring-servlet.xml来配置
* @param request
* @param response
* @return
* @throws Exception
*/ // @RequestMapping("/login")
// public String login(String username,String password) throws Exception{
// System.out.println("username:"+username+" password:"+password);
// return "success";
// } /**
* SpringMVC接收表单参数
* 第二种方式:
* 通过HttpServletRequest来接收表单参数
* @param request
* @param response
* @return
* @throws Exception
*/
// @RequestMapping("/login")
// public String login(HttpServletRequest request,HttpServletResponse response) throws Exception{
// String username = request.getParameter("username");
// String password = request.getParameter("password");
// System.out.println("usrename:"+username+" / password:"+password+request.getContextPath());
// return "success";
// } /**
* SpringMVC接收表单参数
* 第三种方式:
* 通过一个bean来接收,建立一个和表单中参数对应的bean,用这个bean来封装接收的参数
* @return
* @throws Exception
// */
// @RequestMapping("/login")
// public String login(User user) throws Exception{
// System.out.println("usrename:"+user.getUsername()+" / password:"+user.getPassword());
// return "success";
// } /**
* SpringMVC接收表单参数
* 第四种方式:
* 通过json数据接收,将表单的数据封装成json字符串来接收,依然可以通过bean来封装接收参数
* @return
* @throws Exception
*/
// @RequestMapping("/login")
// public String login(User user) throws Exception{
// System.out.println("usrename:"+user.getUsername()+" / password:"+user.getPassword());
// return "success";
// } @RequestMapping("/login")
public ModelAndView login(String username,String password){
System.out.println("usrename:"+username+" / password:"+password);
String message = "helloSpringMVC"; return new ModelAndView("success","message",message);
}
}

接收参数方法

<script type="text/javascript">
$(document).ready(function(){
$("#button_submit").click(function(){ //序列化表单元素,返回json数据
var params = $("#userForm").serializeArray(); //也可以把表单之外的元素按照name value的格式存进来
//params.push({name:"hello",value:"man"}); $.ajax({
type:"POST",
url:"${pageContext.request.contextPath}/user/addUser5",
data:params,
success:function(data){
alert("成功");
},
error:function(e) {
alert("出错:"+e);
}
});
});
});
</script>

Jquery传参