Spring学习(四) 结果跳转方式和数据处理

38 篇文章 0 订阅
5 篇文章 0 订阅

四、SpringMVC 结果跳转方式

4.1、ModelAndView

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

页面真实位置:【视图解析器前缀】+ viewName +【视图解析器后缀】

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="internalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
@Controller
public class RestFulController {

    @RequestMapping(value = "/add/{a}/{b}",method = RequestMethod.GET)
    public String test1(@PathVariable int a, @PathVariable int b , Model model){
        int result = a + b;
        model.addAttribute("msg", "结果为" + result);
        return "hello";
    }

}

4.2、ServletAPI

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

  1. 通过HttpServletResponse进行输出
  2. 通过HttpServletResponse进行重定向
  3. 通过HttpServletResponse实现转发
@RequestMapping("/test1")
public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
    
    response.getWriter().println("hello");
}
@RequestMapping("/test2")
public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
    //重定向
    response.sendRedirect("/index.jsp");
}

@RequestMapping("/test3")
public void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    //转发
    request.setAttribute("msg", "/test3");
    request.getRequestDispatcher("/jsp/test1.jsp").forward(request, response);
}

4.3、SpringMVC 重定向和转发

不需要视图解析器

@RequestMapping("/test2")
public String test2(Model model){

    //转发
    model.addAttribute("msg", "hello");
    return "forward:/WEB-INF/jsp/hello.jsp";
}
    @RequestMapping("/test2")
    public String test2(Model model){

        //重定向
        model.addAttribute("msg", "hello");
        return "redirect:index.jsp";
    }

有视图解析器

默认情况下为转发 如果需要重定向 在返回的路径上加上redirect

@RequestMapping("/test2")
public String test2(Model model){

    //转发
    model.addAttribute("msg", "hello");
    return "hello";
}
   @RequestMapping("/test2")
    public String test2(Model model){

        //重定向
        model.addAttribute("msg", "hello");
        return "redirect:index.jsp"";
    }

五、SpringMVC 数据处理

5.1、处理提交数据

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

    提交数据:http://localhost:8080/hello?name=zhangsan

    处理方法:

        @GetMapping("/t1")
        public String test3(String name,Model model){
            //接受前端参数
            System.out.println(name);
            //将参数返回前端
            model.addAttribute("msg", name);
            return "hello";
        }
    
  2. 提交的域名称和处理方法的参数不一致

    提交数据:http://localhost:8080/hello?username=zhangsan

    处理方法:

    // @RequestParam("提交的域名称")   
    	@RequestMapping("/hello")
        public String test2(@RequestParam("username")String name){
    
            System.out.println(name);
           return "hello";
        }
    
  3. 提交的是一个对象

    要求提交的表单域和对象的属性名一致,参数使用对象即可

    实体类:

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User {
        private int id;
        private String name;
        private  int age;
    
    }
    

    提交数据:http://localhost:8080/hello?name=zhangsan&id=2&age=23

    处理方法:

      @RequestMapping("/user")
        public String test2(User user){
            System.out.println(user);
           return "hello";
        }
    

5.2、数据在前端显示

  1. 通过ModelAndView

      @Override
        public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
            ModelAndView mv = new ModelAndView();
            mv.addObject("msg", "HelloController");
            mv.setViewName("test");
            return mv;
        }
    
  2. 通过ModelMap

在这里插入图片描述

  1. 通过Model

        @GetMapping("/t1")
        public String test3(String name,Model model){
            //接受前端参数
            System.out.println(name);
            //将参数返回前端
            model.addAttribute("msg", name);
            return "hello";
        }
    

对比:

  • Model :只有几个方法只适合用于存储数据,简化了对于Model对象的操作和理解
  • ModelMap:继承了LinkedHashMap,除了实现自身的一些方法,同样的集成LinkedHashMap的方法和特性
  • ModelAndView:可以在存储数据的同时,进行设置返回的逻辑视图,进行控制显示层的跳转
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值