MVC parameter passing
Request parameter automatic conversion
JSP page
form class="loginForm" action="/user/getUser" method="post" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <Input id = "user" type = "text" name = "userName" placeholder = "Please enter your user name" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form>
Point * Note: Method Parameter Name in the Controller must be consistent with the value of the name attribute of the form element
/*log in*/
@RequestMapping("/getUser")
@ResponseBody
private ModelAndView getUser(String userName, String password, ModelAndView mv, HttpServletRequest request, HttpServletResponse response, HttpSession session){
Users user = userService.getUser(userName,password);
System.out.println("user======"+user);
if (user!=null){
System.out.println ( "success");
//login successful
request.getSession().setAttribute("user",user);
// forward
mv.setViewName("index");
}else{
//Login failed
mv.setViewName("login");
}
return mv;
}
Assembly of the object request parameters POJO
New Person
public class Person {
private String username;
private int age;
// omitted get / set methods
}
Controller
// When the entity name attribute class attribute names and forms of the same element, to complete the automatic assembly
@RequestMapping(value = "personObject",method = RequestMethod.POST)
public String personObject(Person person){
System.out.println(person);
return "hello"; }
@RequestParam comment
@RequestParam effect that in the case where the controller parameter elements and the form does not match a method using annotations @RequestParam declared parameter name.
@RequestParam has three attributes:
(1) value: request parameter name (must be configured)
(2) required: if necessary, the default is true, i.e., the request must include the parameter, if it does not, an exception is thrown (optional)
(3) defaultValue: the default value if the value is set, required automatically set to false, regardless of whether you configure required, configure what values are false (optional)
jsp page
<form class="loginForm" action="/getUser" method="post" onsubmit="return check()" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <Input id = "user" type = "text" name = "userName" placeholder = "Please enter your user name" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form>
Controller
@RequestMapping(value = "/getUser",produces = "text/json;charset=UTF-8")
@ResponseBody
public String getUser(String userName,@RequestParam( name="password", required = true,defaultValue ="123" ) String password){
return "Name:" + userName + ", Password:" + password;
}
RESTFUL style parameter passing
/ * {ID} placeholder * / @RequestMapping ( " / getId / {ID} " ) @ResponseBody public Object getid(@PathVariable("id") Integer id){ List<Accounts> name = accountService.getName(id); return name; }
Objects passing parameters
IUserInfo entity classes:
package com.cmy.entity; import java.util.ArrayList; import java.util.List; public class IUserInfo { private int uid; private String username; // domain attributes injection Private Teacher Teacher; private List<Teacher> teacherList; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public List<Teacher> getTeacherList() { return teacherList; } public void setTeacherList(List<Teacher> teacherList) { this.teacherList = teacherList; } @Override public String toString() { return "IUserInfo{" + "uid=" + uid + ", username='" + username + '\'' + ", teacher=" + teacher + ", teacherList=" + teacherList + '}'; } }
Teacher entity classes:
package com.cmy.entity; public class Teacher { private int tid; private String teachername; public int getTid () { return time; } public void setTid ( int time) { this .tid = time; } public String getTeachername() { return teachername; } public void setTeachername(String teachername) { this.teachername = teachername; } }
Control Layer
@Controller @RequestMapping("/fout") public class FoutController {//属性 @RequestMapping("/getUser") public String getUser(IUserInfo userInfo){ System.out.println(userInfo.toString()); return "welcome"; } }
page
Domain property (1)
<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <input id="user" type="text" name="teacher.teachername" placeholder="请输入用户名" /> <input id="users" type="text" name="teacher.teachername" placeholder="请输入用户名" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form>
Collection (2)
<form class="loginForm" action="/fout/getUser" method="post" onsubmit="return check()" > <div class="inputbox" style="text-align:center; "> <label for="user">用户名:</label> <INPUT ID = " User " type = " text " name = " teacherList [0] .teachername " placeholder = " Please enter a user name " /> <input id="users" type="text" name="teacherList[1].teachername" placeholder="请输入用户名" /> </div> <div class="password" style="text-align:center; " > <label for="mima">密码:</label> <input id="mima" type="password" name="password" placeholder="请输入密码" /> </div> <div class="subBtn" style="text-align:center; "> <input type="submit" value="登录" /> <input type="reset" value="重置"/> </div> </form>