SpringMVC学习笔记4:数据处理和跳转

一、结果跳转方式

  • 转发:是服务器内部的跳转,浏览器的地址栏不会发生变化。从一个页面到另一个页面的跳转还是同一个请求,也即是只有一个请求响应。可以通过request域来传递对象。
  • 重定向:是浏览器自动发起对跳转目标的请求,浏览器的地址栏会发生变化。从一个页面到另一个页面的跳转是不同的请求,也即是有两个或两个以上的不同的请求的响应。无法通过request域来传递对象。
     

1.ModelAndView

1.配置视图解析器

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

2.编写Controller

public class ControllerTest1 implements Controller {

   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //返回一个模型视图对象
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

2.ServletAPI

@Controller
public class ResultGo {

   @RequestMapping("/result/t1")    //输出
   public void test1(HttpServletRequest req, HttpServletResponse rsp) throws IOException {
       rsp.getWriter().println("Hello world");
  }

   @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);
  }

}

3.SpringMVC

@Controller
public class TestController {
   @RequestMapping("/test/t1")
   public String test1(){
       //转发
       return "/index.jsp";
  }
   @RequestMapping("/test/t2")
   public String test2(){
       //转发二
       return "forward:/index.jsp";
  }
   @RequestMapping("/test/t3")
   public String test3(){
       //重定向
       return "redirect:/index.jsp";
  }
}

二、数据处理

1.前端提交数据到后端处理

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

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

处理方法:

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

2、提交的域名称和处理方法的参数名不一致

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

处理方法:

//@RequestParam("username") : username提交的域的名称 
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
   System.out.println(name);
   return "hello";
}

3、提交的是一个对象

提交数据 : http://localhost:8080//user?name=xiaoyi&id=1&age=18

处理方法:

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

:使用对象的话,前端传递的参数名和对象名必须一致,否则就是null。

2.后端数据显示到前端

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

1.通过ModelAndView

public class ControllerTest1 implements Controller {

   public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
       //返回一个模型视图对象
       ModelAndView mv = new ModelAndView();
       mv.addObject("msg","ControllerTest1");
       mv.setViewName("test");
       return mv;
  }
}

2.通过ModelMap

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

3.通过Model

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值