- 处理提交数据
1.提交的域名称和处理方法的参数名一致
提交数据:http:/localhost:8080/hello?name=aa
处理方法:
@RequestMapping("/hello") public String hello(String name){ System.out.println(name); return "hello"; }
2.提交的域名称和处理方法的参数名不一致
提交数据:http:/localhost:8080/hello?name=aa
处理方法:
//@RequestParam("username"):username提交的域的名称 @RequestMapping("/hello") public String hello(@RequestParam("name") String name){ System.out.println(name); return "hello"; }
3.提交的是一个对象
要求提交的表单域和对象的属性名一致,参数使用对象即可
1.实体类
@Data//get和set @AllArgsConstructor//有参 @NoArgsConstructor//无参 public class User { private int id; private String name; private int age; }
2.提交数据
http://localhost:8080/mvc04/u1?name=yyw&id=1&age=12
3.处理方法
@Controller @RequestMapping("/u1") public class UserController { //localhost:/8080/user/t1?name=xxx; @GetMapping("/t1") public String test1(String name, Model model){ //1.接收前端参数 System.out.println("接收到前端的参数为:"+name); //2.将返回的结果传递给前端 model.addAttribute("msg",name); //3.跳转视图 return "test"; }
说明:如果使用对象的话,前端传递的参数名和对象名必须一致,否则就是null。
- 数据显示到前端
第一种:通过ModelAndView
public class ControllerTest1 implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("msg","ControllerTest1"); mv.setViewName("test"); return mv; } }
第二种:通过ModelMap
ModelMap:继承了LinkedHashMap,所以他拥有LinkedHashMap的全部功能
public String test3(ModelMap map){ map.addAttribute("msg","ModelMap"); return "test"; }
第三种:通过Model
Model:精简版(大部分情况下,直接使用Model)
@GetMapping("/t1") public String test1(@RequestParam("username") String name, Model model){ //1.接收前端参数 System.out.println("接收到前端的参数为:"+name); //2.将返回的结果传递给前端 model.addAttribute("msg",name); //3.跳转视图 return "test"; }
- 对比
Model:只有寥寥几个方法只适合于储存数据,简化了新手对于Model对象的操作和理解。
ModelMap:继承了LinkedMap,除了实现自身的一些方法,同样的继承LinkedMap的方法和特性。
ModelAndView:可以在存储数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转。
- 乱码问题
1.先编写一个提交的表单
<form action="/e1" method="post"> <input type="text" name="name"> <input type="submit"> </form>
2.后台编写对应的处理类
@Controller public class EncodingController { @PostMapping("/e1") public String test1(String name, Model model){ model.addAttribute("msg",name); return "test"; } }
3.输入中文测试,发现乱码
SpringMVC给我们提供了一个过滤器,可以在web.xml中配置,修改了xml文件需要重启服务器!
<filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
处理方法:
1.修改tomact配置文件:设置编码!
位置:*\apache-tomcat-8.5.38\conf\server.xml
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URLEncoding="UTF-8"/>
2.自定义过滤器(极端情况使用)
package com.yyw.filter; import lombok.SneakyThrows; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; public class GenericEncodingFilter implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { //处理response的字符编码 HttpServletResponse myResponse = (HttpServletResponse) response; myResponse.setContentType("text/html;charset=UTF-8"); //转型为与协议相关对象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; //对request包装增强 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest,response); } public void destroy() { } //自定义request对象,HttpServletRequest的包装类 class MyRequest extends HttpServletRequestWrapper{ private HttpServletRequest request; //是否编写标记 private boolean hasEncode; //定义一个可以转入HttpServletRequest对象的构造函数,以便对其进行装饰 public MyRequest(HttpServletRequest request){ super(request);//super必须写 this.request=request; } //对需要增强方法进行覆盖 public Map getParameterMap(){ //先获取请求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { //post请求 try { request.setCharacterEncoding("utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }else if(method.equalsIgnoreCase("get")){ //get请求 Map<String,String[]> parameterMap = request.getParameterMap(); if(!hasEncode){ for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values!=null) { for (int i=0;i<values.length;i++){ try { values[i] = new String(values[i] .getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } hasEncode=true; } return parameterMap; } return super.getParameterMap(); } //取一个值 @Override public String getParameter(String name){ Map<String,String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); if (values==null){ return null; } return values[0];//取参数的第一个值 } //取所有值 @Override public String[] getParameterValues(String name){ Map<String,String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } } }