0


SpringMVC原理分析 | 数据处理:ModelAndView

在这里插入图片描述

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


重定向和转发

ModelAndView

设置ModelAndView对象,根据view的名称、视图解析器跳转到指定的页面

页面:{视图解析器前缀}+ viewName +{视图解析器后缀}

  • ​ springmvc-servlet.xml
<!--视图解析器--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"id="internalResourceViewResolver"><!--前缀--><propertyname="prefix"value="/WEB-INF/jsp/"/><!--后缀--><propertyname="suffix"value=".jsp"/></bean>
  • Controller类
//实现了Controller类说明这就是控制器publicclassControllerTest1implementsController{@OverridepublicModelAndViewhandleRequest(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)throwsException{//创建模型和视图对象ModelAndView modelAndView =newModelAndView();封装数据,向模型添加属性msg与值,可以在jsp页面取出、渲染
        modelAndView.addObject("msg","ControllerTest1");//视图跳转
        modelAndView.setViewName("test");//返回视图模型对象return modelAndView;}}

ServletAPI

设置ServletAPI,不需要视图解析器

通过HttpServletResponse进行输出、重定向、转发

@ControllerpublicclassModelTest1{@RequestMapping("/m1/t1")publicStringtest(HttpServletRequest request,HttpServletResponse response){HttpSession session = request.getSession();System.out.println(session.getId());return"test";}}

SpringMVC

通过SpringMVC实现转发和重定向,不需要视图解析器

  • 转发
@ControllerpublicclassModelTest1{@RequestMapping("/m1/t1")publicStringtest(Model model){//转发
        model.addAttribute("msg","ModelTest");return"/WEB-INF/jsp/test.jsp";//return "forward:/WEB-INF/jsp/test.jsp";}}
  • 重定向
@ControllerpublicclassModelTest1{@RequestMapping("/m1/t1")publicStringtest(Model model){//重定向
        model.addAttribute("msg","ModelTest");return"redirect:/index.jsp";}}

数据处理

接受请求参数及数据回显,处理提交数据

处理提交数据

  • 提交的域名称和处理方法的参数名一致
@Controller@RequestMapping("/user")publicclassUserController{@GetMapping("/t1")publicStringtest(String name,Model model){//接受前端参数System.out.println("接收到前端的参数为:"+ name);//将放回结果传递给前端
        model.addAttribute("msg",name);//跳转视图return"test";}}
  • 提交的域名称和处理方法的参数名不一致
@RequestParam("username")
@Controller@RequestMapping("/user")publicclassUserController{@GetMapping("/t1")publicStringtest(@RequestParam("username")String name,Model model){//接受前端参数System.out.println("接收到前端的参数为:"+ name);//将放回结果传递给前端
        model.addAttribute("msg",name);//跳转视图return"test";}}
  • 提交是一个对象(参数名和对象名需要一致,否则返回Null)
  • User类
@Data@AllArgsConstructor@NoArgsConstructorpublicclassUser{privateint id;privateString name;privateint age;}
  • UserController类
@Controller@RequestMapping("/user")publicclassUserController{@RequestMapping("/t2")publicStringtest2(User user){System.out.println(user);return"test";}}//输出结果:User(id=1, name=weishuo, age=18)

数据显示到前端

ModelAndView

//实现了Controller类说明这就是控制器publicclassControllerTest1implementsController{@OverridepublicModelAndViewhandleRequest(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse)throwsException{//创建模型和视图对象ModelAndView modelAndView =newModelAndView();封装数据,向模型添加属性msg与值,可以在jsp页面取出、渲染
        modelAndView.addObject("msg","ControllerTest1");//视图跳转
        modelAndView.setViewName("test");//返回视图模型对象return modelAndView;}}

Model

@Controller@RequestMapping("/user")publicclassUserController{@GetMapping("/t1")publicStringtest(@RequestParam("username")String name,Model model){//接受前端参数System.out.println("接收到前端的参数为:"+ name);//将放回结果传递给前端
        model.addAttribute("msg",name);//跳转视图return"test";}}

ModelMap

@Controller@RequestMapping("/user")publicclassUserController{@RequestMapping("/t3")publicStringtest3(ModelMap map){
        map.addAttribute("msg","HELLO ModelMap");return"test";}}

总结

  • Model 只有寥宴几个方法只适合用于储存数据,简化了新手对于Model对象的操作和理解
  • ModelMap 继承了 LinkedMap,除了实现了自身的一些方法,同样的继承LinkedMap 的方法和特性
  • ModelAndview 可以在储存数据的同时,可以进行设置返回的逻辑视图,进行控制展示层的跳转

过滤器解决乱码

Javaweb过滤器

  • EncodingController类
@ControllerpublicclassEncodingController{@GetMapping("/e/t1")publicStringtest1(String name,Model model,HttpServletRequest request)throwsUnsupportedEncodingException{
        request.setCharacterEncoding("utf-8");System.out.println(name);
        model.addAttribute("msg",name);return"test";}}
  • EncodingFilter过滤器
publicclassEncodingFilterimplementsFilter{@Overridepublicvoidinit(FilterConfig filterConfig)throwsServletException{}@OverridepublicvoiddoFilter(ServletRequest servletRequest,ServletResponse servletResponse,FilterChain filterChain)throwsIOException,ServletException{
        servletRequest.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        filterChain.doFilter(servletRequest, servletResponse);}@Overridepublicvoiddestroy(){}}
  • web.xml配置过滤器
<filter><filter-name>encoding</filter-name><filter-class>com.wei.filter.EncodingFilter</filter-class></filter><filter-mapping><filter-name>encoding</filter-name><url-pattern>/</url-pattern></filter-mapping>

SpringMVC过滤器

SpringMVC中提供了过滤器,可以在web.xml中配置

  • EncodingController类
@ControllerpublicclassEncodingController{@RequestMapping("/e/t1")publicStringtest1(String name,Model model,HttpServletRequest request)throwsUnsupportedEncodingException{
        request.setCharacterEncoding("utf-8");System.out.println(name);
        model.addAttribute("msg",name);return"test";}}
  • web.xml配置SpringMVC乱码过滤器
<!--配置SpringMVC乱码过滤器--><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>

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——

点赞

👍

收藏

⭐️

评论

📝


在这里插入图片描述

标签: mvc spring

本文转载自: https://blog.csdn.net/weixin_62765017/article/details/130923608
版权归原作者 wei_shuo 所有, 如有侵权,请联系我们删除。

“SpringMVC原理分析 | 数据处理:ModelAndView”的评论:

还没有评论