0
点赞
收藏
分享

微信扫一扫

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


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

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !

重定向和转发

ModelAndView

设置ModelAndView对象,根据view的名称、视图解析器跳转到指定的页面
页面:{视图解析器前缀}+ viewName +{视图解析器后缀}
•  springmvc-servlet.xml
<!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
• Controller类
//实现了Controller类说明这就是控制器
public class ControllerTest1 implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        //创建模型和视图对象
        ModelAndView modelAndView = new ModelAndView();
        封装数据,向模型添加属性msg与值,可以在jsp页面取出、渲染
        modelAndView.addObject("msg","ControllerTest1");
        //视图跳转
        modelAndView.setViewName("test");

        //返回视图模型对象
        return modelAndView;
    }
}

ServletAPI

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

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

@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t1")
    public String test(HttpServletRequest request, HttpServletResponse response){

        HttpSession session = request.getSession();
        System.out.println(session.getId());
        return "test";
    }
}

SpringMVC

通过SpringMVC实现转发和重定向,不需要视图解析器
• 转发
@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t1")
    public String test(Model model){
        //转发
        model.addAttribute("msg","ModelTest");
        return "/WEB-INF/jsp/test.jsp";
        //return "forward:/WEB-INF/jsp/test.jsp";
    }
}
• 重定向
@Controller
public class ModelTest1 {

    @RequestMapping("/m1/t1")
    public String test(Model model){
        //重定向
        model.addAttribute("msg","ModelTest");
        return "redirect:/index.jsp";
    }
}

数据处理

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

处理提交数据

• 提交的域名称和处理方法的参数名一致

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/t1")
    public String test(String name, Model model) {

        //接受前端参数
        System.out.println("接收到前端的参数为:" + name);

        //将放回结果传递给前端
        model.addAttribute("msg",name);

        //跳转视图
        return "test";
    }
}
• 提交的域名称和处理方法的参数名不一致

@RequestParam("username")@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/t1")
    public String test(@RequestParam("username") String name, Model model) {

        //接受前端参数
        System.out.println("接收到前端的参数为:" + name);

        //将放回结果传递给前端
        model.addAttribute("msg",name);

        //跳转视图
        return "test";
    }
}
• 提交是一个对象(参数名和对象名需要一致,否则返回Null)
• User类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private int age;
}
• UserController类
@Controller
@RequestMapping("/user")
public class UserController {
    
    @RequestMapping("/t2")
    public String test2(User user){
        System.out.println(user);
        return "test";
    }
    
}
//输出结果:User(id=1, name=weishuo, age=18)

数据显示到前端

ModelAndView

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

        //返回视图模型对象
        return modelAndView;
    }
}

Model

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/t1")
    public String test(@RequestParam("username") String name, Model model) {

        //接受前端参数
        System.out.println("接收到前端的参数为:" + name);

        //将放回结果传递给前端
        model.addAttribute("msg",name);

        //跳转视图
        return "test";
    }
}

ModelMap

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/t3")
    public String test3(ModelMap map){
        map.addAttribute("msg","HELLO ModelMap");
        return "test";
    }
    
}

总结

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

过滤器解决乱码

Javaweb过滤器

• EncodingController类
@Controller
public class EncodingController {
    
    @GetMapping("/e/t1")
    public String test1(String name, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        System.out.println(name);
        model.addAttribute("msg",name);
        return "test";
    }
}
• EncodingFilter过滤器
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>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类
@Controller
public class EncodingController {

    @RequestMapping("/e/t1")
    public String test1(String name, Model model, HttpServletRequest request) throws UnsupportedEncodingException {
        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>

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝

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


举报

相关推荐

0 条评论