Controller类可以拿到HttpServletRequest、HttpServletResponse对象,所以可以通过Servlet API来获取前端提交的数据,也可以通过Servlet API 来传递返回信息给前端:
@Controller
@RequestMapping("/c6")
public class ControllerTest6 {
@RequestMapping("/t1")
public String test1(HttpServletRequest request, HttpServletResponse response){
String name = request.getParameter("name");
request.getSession().setAttribute("msg",name);
return "test";
}
}
我们可以把方法定义的形参名字设置成和前端传入参数名一样的方法,来获取到数据:
@Controller
@RequestMapping("/c6")
public class ControllerTest6 {
@RequestMapping("/t2")
public ModelAndView test2(String name, int age){
ModelAndView modelAndView = new ModelAndView("user");
modelAndView.addObject("name",name);
modelAndView.addObject("age",age);
return modelAndView;
}
}
这种方式前端传入的参数名与方法形参必须一致,不一样就会出现错误,那有没有什么方法能解决这个问题呢?
答案是肯定的,当方法形参与前端传入的参数名不一致时,只要在方法形参前面使用 @RequestParam(“前端传入参数名”) 对应注入前端传入的参数即可
@Controller
@RequestMapping("/c6")
public class ControllerTest6 {
@RequestMapping("/t3")
public ModelAndView test3(@RequestParam("username") String name,@RequestParam("userAge") int age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name",name);
modelAndView.addObject("age",age);
modelAndView.setViewName("user");
return modelAndView;
}
}
@RequestParam 注解细节,该注解有四个变量 name、value、required、defaultvalue:
先创建一个User对象:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private int age;
}
前端user.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>${user.getId()}</h3>
<h3>${user.getName()}</h3>
<h3>${user.getAge()}</h3>
</body>
</html>
当前端传入的参数刚好可以构成一个对象bean时,可以在方法上使用对应的对象传参,但 前端传入的参数名必须和对象中的属性字段名一样 :
@Controller
@RequestMapping("/c6")
public class ControllerTest6 {
@RequestMapping("/t4")
public ModelAndView test4(User user){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("user",user);
modelAndView.setViewName("user");
return modelAndView;
}
}
当传入的参数少了的话就会使用缺少的属性字段的默认值:
传入的参数可以是随意顺序的:
通过上面的例子我们知道,可以通过Servlet API返回数据给前端:
@RequestMapping("/t1")
public String test1(HttpServletRequest request, HttpServletResponse response){
String name = request.getParameter("name");
request.getSession().setAttribute("msg",name);
return "test";
}
也可以通过ModelAndView对象封装数据和视图返回给前端,此时方法的返回值必须是ModelAndView:
@RequestMapping("/t2")
public ModelAndView test2(String name, int age){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name",name);
modelAndView.addObject("age",age);
modelAndView.setViewName("user");
return modelAndView;
}
这样必须把Model对象放在方法的参数列表上,设置方法返回值为String类型:
@RequestMapping("/t5")
public String test5(Model model,String name, int age){
model.addAttribute("name",name);
model.addAttribute("age",age);
return "user";
}
用法与Model一样:
@RequestMapping("/t6")
public String test6(ModelMap model, String name, int age){
model.addAttribute("name",name);
model.addAttribute("age",age);
return "user";
}
定义一个form.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/c7/t1" method="post">
<input type="text" name="msg">
<input type="submit">
</form>
</body>
</html>
创建一个控制类处理请求:
@Controller
public class ControllerTest7 {
@RequestMapping("/c7/t1")
public String test1(@RequestParam("msg") String msg, Model model){
model.addAttribute("msg",msg);
return "test";
}
}
自定义一个编码过滤器:
// 中文乱码过滤器
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setCharacterEncoding("UTF-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
在web.xml中配置过滤器:
<!--中文乱码过滤器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在 web.xml 中配置Spring自带的乱码过滤器:
<!--配置Spring自带的乱码过滤器-->
<filter>
<filter-name>springMVCEncoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springMVCEncoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>