当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定
Controller方法的返回值1:返回void类型
- @RequestMapping(“/test_void.action”)
- public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>request.setCharacterEncoding(“utf-8”);
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>String username = request.getParameter(“username”);//通过HttpServletRequest获得请求参数
- System.out.println(”用户名:”+username);
- request.setAttribute(”username”,username);
- User u = new User();
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>u.setUsername(username);
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>userService.insertUser(u);//插入数据库
- request.getRequestDispatcher(”/WEB-INF/jsp/success.jsp”).forward(request, response);//转发
- }
@RequestMapping("/test_void.action")
public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");//通过HttpServletRequest获得请求参数
System.out.println("用户名:"+username);
request.setAttribute("username",username);
User u = new User();
u.setUsername(username);
userService.insertUser(u);//插入数据库
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发
}
Controller方法的返回值2:返回ModelAndView
- @RequestMapping(“/test_modelandview.action”)
- public ModelAndView controller02(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);//转码,Tomcat默认是iso-8859-1
- String username = request.getParameter(”username”);
- System.out.println(”用户名:”+username);
- ModelAndView modelAndView = new ModelAndView();<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//new一个ModelAndView
- modelAndView.addObject(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//相当于request.setAttribute(attrName,attrValue);
- modelAndView.setViewName(”WEB-INF/jsp/success.jsp”);//视图跳转
- return modelAndView;
- }
@RequestMapping("/test_modelandview.action")
public ModelAndView controller02(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1
String username = request.getParameter("username");
System.out.println("用户名:"+username);
ModelAndView modelAndView = new ModelAndView(); //new一个ModelAndView
modelAndView.addObject("username",username); //相当于request.setAttribute(attrName,attrValue);
modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转
return modelAndView;
}
Controller方法的返回值3:返回String类型(逻辑视图)
- @RequestMapping(“/test_string.action”)
- public String controller03(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//转码
- String username = request.getParameter(”username”);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//设置请求参数
- System.out.println(”用户名:”+username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>
- return “/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,代表逻辑视图
- }
@RequestMapping("/test_string.action")
public String controller03(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8"); //转码
String username = request.getParameter("username");
request.setAttribute("username",username); //设置请求参数
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp"; //返回String类型,代表逻辑视图
}
Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)
- @RequestMapping(“/test_model.action”)
- public String controller04(HttpServletRequest request,Model model) throws Exception{
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>request.setCharacterEncoding(“utf-8”);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>String username = request.getParameter(“username”);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>model.addAttribute(“username”, username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//等价于request.setAttribute(attrName,attrValue);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>System.out.println(“用户名:”+username);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>return ”/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,跳转到逻辑视图
- }
@RequestMapping("/test_model.action")
public String controller04(HttpServletRequest request,Model model) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
model.addAttribute("username", username); //等价于request.setAttribute(attrName,attrValue);
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp"; //返回String类型,跳转到逻辑视图
}
Controller方法的返回值5:返回重定向redirect后的逻辑视图名
- @RequestMapping(“/test_redirect.action”)
- public String controller05(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);
- String username = request.getParameter(”username”);
- User u = new User();
- u.setUsername(username);
- userService.insertUser(u);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//由于是redirect,所以请求参数失效
- return “redirect:/controller/test_model.action”;//redirect:重定向到一个Controller里
- }
@RequestMapping("/test_redirect.action")
public String controller05(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
request.setAttribute("username",username); //由于是redirect,所以请求参数失效
return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里
}
Controller方法的返回值6:返回farward转发后的逻辑视图名
- @RequestMapping(“/test_forword.action”)
- public String controller06(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);
- String username = request.getParameter(”username”);
- User u = new User();
- u.setUsername(username);
- userService.insertUser(u);
- System.out.println(”用户名:”+username);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//由于是转发,所以请求参数有效
- return “forward:/controller/test_model.action”;//转发,跳转到一个Controller里
- }
@RequestMapping("/test_forword.action")
public String controller06(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
System.out.println("用户名:"+username);
request.setAttribute("username",username); //由于是转发,所以请求参数有效
return "forward:/controller/test_model.action";//转发,跳转到一个Controller里
}
参数绑定
参数绑定的第一种方法:绑定普通类型
- //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
- @RequestMapping(“/test_parambinding01.action”)
- public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
- //必须进行转码
- username = new String(username.getBytes(“iso8859-1”),“UTF-8”);
- request.setAttribute(”username”,username);
- request.getRequestDispatcher(”/WEB-INF/jsp/success.jsp”).forward(request, response);
- }
//参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
@RequestMapping("/test_parambinding01.action")
public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
//必须进行转码
username = new String(username.getBytes("iso8859-1"),"UTF-8");
request.setAttribute("username",username);
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
}
- //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
- @RequestMapping(“/test_parambinding02.action”)
- public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
- //必须进行转码
- user.setUsername(new String(user.getUsername().getBytes(“iso-8859-1”),“utf-8”));
- userService.insertUser(user);
- ModelAndView modelAndView = new ModelAndView();
- request.setCharacterEncoding(”utf-8”);
- modelAndView.addObject(”username”,user.getUsername());
- modelAndView.addObject(”password”,user.getPassword());
- modelAndView.setViewName(”/WEB-INF/jsp/success.jsp”);
- return modelAndView;
- }
//参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
@RequestMapping("/test_parambinding02.action")
public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
//必须进行转码
user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
userService.insertUser(user);
ModelAndView modelAndView = new ModelAndView();
request.setCharacterEncoding("utf-8");
modelAndView.addObject("username",user.getUsername());
modelAndView.addObject("password",user.getPassword());
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解
- @RequestMapping(“test_RequestParam.action”)
- //将客户端的请求参数”username”与”uname”绑定
- public ModelAndView controller09(@RequestParam(value=“username”) String uname,@RequestParam(value=“password”)String pwd) throws Exception{
- uname = new String(uname.getBytes(“iso-8859-1”),“utf-8”);
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.addObject(”username”,uname);
- modelAndView.setViewName(”/WEB-INF/jsp/success.jsp”);
- return modelAndView;
- }
@RequestMapping("test_RequestParam.action")
//将客户端的请求参数"username"与"uname"绑定
public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
uname = new String(uname.getBytes("iso-8859-1"),"utf-8");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username",uname);
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
</div>
当客户端通过get或post请求发送来的参数通过Controller中方法的参数接受,叫做参数绑定
Controller方法的返回值1:返回void类型
- @RequestMapping(“/test_void.action”)
- public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>request.setCharacterEncoding(“utf-8”);
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>String username = request.getParameter(“username”);//通过HttpServletRequest获得请求参数
- System.out.println(”用户名:”+username);
- request.setAttribute(”username”,username);
- User u = new User();
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>u.setUsername(username);
- <span style=”margin:0px;padding:0px;white-space:pre;”> </span>userService.insertUser(u);//插入数据库
- request.getRequestDispatcher(”/WEB-INF/jsp/success.jsp”).forward(request, response);//转发
- }
@RequestMapping("/test_void.action")
public void controller01(HttpServletRequest request,HttpServletResponse response) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");//通过HttpServletRequest获得请求参数
System.out.println("用户名:"+username);
request.setAttribute("username",username);
User u = new User();
u.setUsername(username);
userService.insertUser(u);//插入数据库
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);//转发
}
Controller方法的返回值2:返回ModelAndView
- @RequestMapping(“/test_modelandview.action”)
- public ModelAndView controller02(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);//转码,Tomcat默认是iso-8859-1
- String username = request.getParameter(”username”);
- System.out.println(”用户名:”+username);
- ModelAndView modelAndView = new ModelAndView();<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//new一个ModelAndView
- modelAndView.addObject(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//相当于request.setAttribute(attrName,attrValue);
- modelAndView.setViewName(”WEB-INF/jsp/success.jsp”);//视图跳转
- return modelAndView;
- }
@RequestMapping("/test_modelandview.action")
public ModelAndView controller02(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");//转码,Tomcat默认是iso-8859-1
String username = request.getParameter("username");
System.out.println("用户名:"+username);
ModelAndView modelAndView = new ModelAndView(); //new一个ModelAndView
modelAndView.addObject("username",username); //相当于request.setAttribute(attrName,attrValue);
modelAndView.setViewName("WEB-INF/jsp/success.jsp");//视图跳转
return modelAndView;
}
Controller方法的返回值3:返回String类型(逻辑视图)
- @RequestMapping(“/test_string.action”)
- public String controller03(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//转码
- String username = request.getParameter(”username”);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//设置请求参数
- System.out.println(”用户名:”+username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>
- return “/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,代表逻辑视图
- }
@RequestMapping("/test_string.action")
public String controller03(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8"); //转码
String username = request.getParameter("username");
request.setAttribute("username",username); //设置请求参数
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp"; //返回String类型,代表逻辑视图
}
Controller方法的返回值4:方法的参数是Model,返回值是String类型(逻辑视图)
- @RequestMapping(“/test_model.action”)
- public String controller04(HttpServletRequest request,Model model) throws Exception{
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>request.setCharacterEncoding(“utf-8”);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>String username = request.getParameter(“username”);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>model.addAttribute(“username”, username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//等价于request.setAttribute(attrName,attrValue);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>System.out.println(“用户名:”+username);
- <span style=“margin:0px;padding:0px;white-space:pre;”> </span>return ”/WEB-INF/jsp/success.jsp”;<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//返回String类型,跳转到逻辑视图
- }
@RequestMapping("/test_model.action")
public String controller04(HttpServletRequest request,Model model) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
model.addAttribute("username", username); //等价于request.setAttribute(attrName,attrValue);
System.out.println("用户名:"+username);
return "/WEB-INF/jsp/success.jsp"; //返回String类型,跳转到逻辑视图
}
Controller方法的返回值5:返回重定向redirect后的逻辑视图名
- @RequestMapping(“/test_redirect.action”)
- public String controller05(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);
- String username = request.getParameter(”username”);
- User u = new User();
- u.setUsername(username);
- userService.insertUser(u);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//由于是redirect,所以请求参数失效
- return “redirect:/controller/test_model.action”;//redirect:重定向到一个Controller里
- }
@RequestMapping("/test_redirect.action")
public String controller05(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
request.setAttribute("username",username); //由于是redirect,所以请求参数失效
return "redirect:/controller/test_model.action";//redirect:重定向到一个Controller里
}
Controller方法的返回值6:返回farward转发后的逻辑视图名
- @RequestMapping(“/test_forword.action”)
- public String controller06(HttpServletRequest request) throws Exception{
- request.setCharacterEncoding(”utf-8”);
- String username = request.getParameter(”username”);
- User u = new User();
- u.setUsername(username);
- userService.insertUser(u);
- System.out.println(”用户名:”+username);
- request.setAttribute(”username”,username);<span style=“margin:0px;padding:0px;white-space:pre;”> </span>//由于是转发,所以请求参数有效
- return “forward:/controller/test_model.action”;//转发,跳转到一个Controller里
- }
@RequestMapping("/test_forword.action")
public String controller06(HttpServletRequest request) throws Exception{
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
User u = new User();
u.setUsername(username);
userService.insertUser(u);
System.out.println("用户名:"+username);
request.setAttribute("username",username); //由于是转发,所以请求参数有效
return "forward:/controller/test_model.action";//转发,跳转到一个Controller里
}
参数绑定
参数绑定的第一种方法:绑定普通类型
- //参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
- @RequestMapping(“/test_parambinding01.action”)
- public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
- //必须进行转码
- username = new String(username.getBytes(“iso8859-1”),“UTF-8”);
- request.setAttribute(”username”,username);
- request.getRequestDispatcher(”/WEB-INF/jsp/success.jsp”).forward(request, response);
- }
//参数绑定的第一种方法:客户端提交的请求的input的name属性会和Controller方法的参数名字一致才会绑定
@RequestMapping("/test_parambinding01.action")
public void controller07(HttpServletRequest request,HttpServletResponse response,String username,String password) throws Exception{
//必须进行转码
username = new String(username.getBytes("iso8859-1"),"UTF-8");
request.setAttribute("username",username);
request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response);
}
- //参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
- @RequestMapping(“/test_parambinding02.action”)
- public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
- //必须进行转码
- user.setUsername(new String(user.getUsername().getBytes(“iso-8859-1”),“utf-8”));
- userService.insertUser(user);
- ModelAndView modelAndView = new ModelAndView();
- request.setCharacterEncoding(”utf-8”);
- modelAndView.addObject(”username”,user.getUsername());
- modelAndView.addObject(”password”,user.getPassword());
- modelAndView.setViewName(”/WEB-INF/jsp/success.jsp”);
- return modelAndView;
- }
//参数绑定的第二种方法:客户端的input标签的那么属性必须和User的属性名对应才可以映射成功
@RequestMapping("/test_parambinding02.action")
public ModelAndView controller08(HttpServletRequest request,User user) throws Exception{
//必须进行转码
user.setUsername(new String(user.getUsername().getBytes("iso-8859-1"),"utf-8"));
userService.insertUser(user);
ModelAndView modelAndView = new ModelAndView();
request.setCharacterEncoding("utf-8");
modelAndView.addObject("username",user.getUsername());
modelAndView.addObject("password",user.getPassword());
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
参数绑定的第三种方法:当input的name与controller的参数名不一致时,可以采用@RequestParam注解
- @RequestMapping(“test_RequestParam.action”)
- //将客户端的请求参数”username”与”uname”绑定
- public ModelAndView controller09(@RequestParam(value=“username”) String uname,@RequestParam(value=“password”)String pwd) throws Exception{
- uname = new String(uname.getBytes(“iso-8859-1”),“utf-8”);
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.addObject(”username”,uname);
- modelAndView.setViewName(”/WEB-INF/jsp/success.jsp”);
- return modelAndView;
- }
@RequestMapping("test_RequestParam.action")
//将客户端的请求参数"username"与"uname"绑定
public ModelAndView controller09(@RequestParam(value="username") String uname,@RequestParam(value="password")String pwd) throws Exception{
uname = new String(uname.getBytes("iso-8859-1"),"utf-8");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username",uname);
modelAndView.setViewName("/WEB-INF/jsp/success.jsp");
return modelAndView;
}
</div>