controller层接收前台参数
1.利用@RequestParam注解
代码:
@Controller
public class Login{
@RequestMapping("/login")
//使用@RequestParam注解接收前台参数
public String login(@RequestParam("userName") String userName , @RequestParam("passWord") String passWord , Model model){
if("admin".equals(userName) && "admin".equals(passWord)){
model.addAttribute("username" , userName);
model.addAttribute("password" , passWord);
return "loginSuccess.jsp";
}else {
returne "login.jsp";
}
}
}
2.当参数少时,直接在方法中写参数
代码:
@Controller
publib class Login{
public String login(String userName , String passWord , Model model){
if("admin".equals(userName) && "admin".equals(passWord)){
model.addAttribute("username" , userName);
model.addAttribute("password" , passWord);
return "loginSuccess.jsp"
}else{
returne "login.jsp";
}
}
}
3.使用HttpServletRequest接收(get和post方式都可以)
代码:
public String login(HttpServletRequest request){
String username = request.getParameter("userName");
String password = request.getParameter("password");
System.out.print(username);
System.out.print(password);
}
4.将参数封装到bean中
public class User{
private String name;
private String pass;
}
@RequestMapping("/login.do")
public String login(User user) {
syso(user.getName());
syso(user.getPass());
}