4.前端给后端传递一个对象【对象】
1.接受前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用 2.传递的是一个对象,匹配User对象中的字段名;如果说名字一致则赋值,否则就不能进行赋值
如果使用对象的话,前端传递的参数名和对象名必须一致,否则直接为null
控制层
package Com.Jsxs.Controller; import Com.Jsxs.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller @RequestMapping("/user") public class Controller6 { /* 1.接受前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用 2.传递的是一个对象,匹配User对象中的字段名;如果说名字一致则赋值,否则就不能进行赋值 */ @GetMapping("/t2") public String test2(User user,Model model){ model.addAttribute("msg",user); return "hello"; } }
http://localhost/SpringMVC_06_Controller_war_exploded/user/t2?id=1&name=%22%E6%9D%8E%22&age=3
一一对应的操作
并不是一一对应的情况下
5.后端展现给前端数据(四种)
(1).Model
@GetMapping("/t2") public String test2(User user,Model model){ model.addAttribute("msg",user); return "hello"; }
(2).Servlet进行获取
@GetMapping("/t1") public String test(String name, HttpServletRequest request, HttpServletResponse response){ // 2.将结果返回给前端 HttpSession session = request.getSession(); session.setAttribute("msg",name); return "hello"; }
(3).ModelAndView进行获取
@GetMapping("/t3") public ModelAndView test3(String name){ ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("msg",name); modelAndView.setViewName("hello"); return modelAndView; }
(4).ModelMap 【暂且不适用,还没掌握】
@GetMapping("/t4") public String test4(String name){ ModelMap modelMap=new ModelMap(); modelMap.addAttribute("msg",name); return "hello"; }
6.后端传入前端方法的对比
- Model 只有寥寥几个方法只适合于存储数据,简化版
- ModelMap 继承了LinkedMap,除了实现了自身的一些方法,同样实现了LinkedMap的方法和特性
- ModelAndView 可以在储存数据的同时,可以进行设置返回逻辑视图
(十一)、乱码问题解决
1.form表单提交的方式是Post然而控制层是Get
Form表单
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/encoding/t1" method="post"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
控制层
package Com.Jsxs.Controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/encoding") public class EncodingController { @GetMapping ("/t1") public String test1(){ return "hello"; }; }
2.form表单提交转发出现乱码的问题
前端页面展示
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="${pageContext.request.contextPath}/encoding/t1" method="post"> <input type="text" name="name"> <input type="submit"> </form> </body> </html>
控制层代码展现
package Com.Jsxs.Controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/encoding") public class EncodingController { @PostMapping ("/t1") public String test1(String name, Model model) { model.addAttribute("msg",name); return "hello"; } }
输入中文并且提交
提交转发后出现乱码数据
(1).过滤器解决乱码
过滤器类
package Com.Jsxs.filter; import org.springframework.stereotype.Component; import javax.servlet.*; import java.io.IOException; public class EncodingFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request,response); } @Override public void destroy() { } }
web.xml注册过滤器
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 注册过滤器 --> <filter> <filter-name>encoding</filter-name> <filter-class>Com.Jsxs.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(2).Method的提交方式
GET: 方法不会出现乱码
POST: 可能会出现乱码
(3).SpringMVC web.xml配置解决中文乱码问题
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- 配置DispatcherServlet--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 注册过滤器 --> <filter> <filter-name>encoding</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>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
(4).Tomcat配置中文UTF-8解决中文乱码