SpringMVC学习(三)——数据处理及跳转

78 篇文章 0 订阅
8 篇文章 2 订阅
页面跳转
ServletAPI

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

1、通过HttpServletResponse进行输出

2、通过HttpServletResponse实现重定向

3、通过HttpServletResponse实现转发

@Controller
public class ResultGo {

    @RequestMapping("/result/t1")
    public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.getWriter().println("Hello,Spring BY servlet API");
    }

    @RequestMapping("/result/t2")
    public void test2(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
        rsp.sendRedirect("/index.jsp");
    }

    @RequestMapping("/result/t3")
    public void test3(HttpServletRequest req, HttpServletResponse rsp) throws Exception {
        //转发
        req.setAttribute("msg","/result/t3");
        req.getRequestDispatcher("/WEB-INF/jsp/test.jsp").forward(req,rsp);
    }

}
通过SpringMVC,无视图解析器

视图解析器的作用就是简化路径,没有视图解析器的时候就需要写路径的全称

@Controller
public class ResultSpringMVC {
    @RequestMapping("/rsm/t1")
    public String test1(){
        //转发 无视图解析器
        return "/index.jsp";
    }
    @RequestMapping("/rsm/t2")
    public String test2(){
        //转发二
        return "forward:/index.jsp";
    }
    @RequestMapping("/rsm/t3")
    public String test3(){
        //重定向
        return "redirect:/index.jsp";
    }
}
通过SpringMVC,有视图解析器

有视图解析器,处理重定向,其他的页面可以省略前缀和后缀。

@Controller
public class ResultSpringMVC2 {
    @RequestMapping("/rsm2/t1")
    public String test1(){
        //转发
        return "test";
    }
    @RequestMapping("/rsm2/t2")
    public String test2(){
        //重定向
        return "redirect:/index.jsp";
        //return "redirect:hello.do"; //hello.do为另一个请求/
    }


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


}
数据处理
处理提交数据
  1. 提交的域名称和处理方法的参数名一致
@RequestMapping("/hello")
public String hello(String name){
    System.out.println(name);
    return "hello";
}

在这里插入图片描述

  1. 提交的域名称和处理方法的参数名不一致
@RequestMapping("/hello1")
public String hello1(@RequestParam("username") String name){
    System.out.println(name);
    return "hello";
}

在这里插入图片描述

3.提交的是一个对象

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

在这里插入图片描述

数据显示到前端
  1. ModelAndView

通过此方法显示数据需要注册bean

<bean class="com.zhang.controller.ControllerTest1" id="/controllerTest1" />
public class ControllerTest1 implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //ModelAndView 模型和视图
        ModelAndView mv = new ModelAndView();
        //封装对象,放在ModelAndView中。Model
        String aa = "HelloSpringMVC!";
        mv.addObject("msg",aa);
        //封装要跳转的视图,放在ModelAndView中
        mv.setViewName("controllerTest"); //: /WEB-INF/jsp/controllerTest.jsp
        return mv;
    }

}

在这里插入图片描述

  1. ModelMap

通过http://localhost:8080/ControllerTest2/hello?username=zhangsan 访问

@RequestMapping("/ControllerTest2/hello")
public String hello(@RequestParam("username") String name, ModelMap model){
    //封装要显示到视图中的数据
    //相当于req.setAttribute("name",name);
    model.addAttribute("name",name);
    System.out.println(name);
    return "hello";
}

在这里插入图片描述

  1. Model

通过http://localhost:8080/ControllerTest2/hello1?username=zhangsan访问

@RequestMapping("/ControllerTest2/hello1")
public String hello(@RequestParam("username") String name, Model model){
    //封装要显示到视图中的数据
    //相当于req.setAttribute("name",name);
    model.addAttribute("msg",name);
    System.out.println(name);
    return "hello";
}

在这里插入图片描述

评论 99
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值