数据处理
处理提交数据
- 提交的域名称和处理方法的参数名一致
- 提交的域名称和处理方法的参数名不一致
@GetMapping("/t1") public String test1(@RequestParam("name1") String name, Model model) { //1.接收前端参数 System.out.println("接收到前端的参数为:" + name); //2.将返回代结果传递给前端 model.addAttribute("msg",name); //3.跳转视图 return "test"; }
- 提交的是一个对象
@GetMapping("/t2") public String test2(User user) { System.out.println(user); return "test"; }
http://localhost:8080/springmvc_03_servlet_war_exploded/user/t2?id=1&name=q&age=1
Model、ModelMap、ModeAndView的区别
- Model 只有个别方法,适合存储数据,简化
- ModelMap 继承了 LinkedMap,除了实现自身方法,还可以使用LinkedMap的方法与特性
- ModelAndView 可以在存储数据的同时,进行设置返回的逻辑视图,进行控制展示层的跳转
乱码问题
解决方案一、过滤器
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() {
}
}
<filter>
<filter-name>encoding</filter-name>
<filter-class>cn.imut.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<!-- 过滤所有的请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
@Controller
public class EncodingController {
//过滤器解决乱码
@PostMapping("/e/t1")
public String test1(String name, Model model) {
model.addAttribute("msg",name);
return "test";
}
}
解决方案二、SpringMVC自带过滤器
<!-- springMVC自带过滤器-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>eccoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
原文地址:https://www.cnblogs.com/yfyyy/p/12433557.html
时间: 2024-10-16 23:51:07