SpringMVC-05-数据处理及结果跳转

5.1、结果跳转

SpringAPI

在Spring之前,我们使用的Servlet方法中的跳转时通过req与resq参数来实现的,

在ServletAPI中,不需要使用视图解析器

通过resp进行输出,实现重定向,转发等功能

当我们不用视图解析器时,使用resp是可以实现视图跳转的

 @Controller
 public class ModelTest1 {
 ​
     @RequestMapping("/m1/t1")
     public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
         response.sendRedirect("/hello.jsp");
     }
 }

ModelAndView

设置ModelAndView对象,根据view的名字,与视图解析器的前缀后缀结合从而跳转到指定页面

页面:【视图解析器前缀】+ViewName+【视图解析器后缀】

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <!--前缀-->
 <property name="prefix" value="/WEB-INF/jsp/"/>
 <!--后缀-->
 <property name="suffix" value=".jsp"/>
 ​
 </bean>

对应的controller

 @Controller
 @RequestMapping("/control")
 public class controller1 {
     @RequestMapping("/test2")
     public String test(Model model){
         model.addAttribute("msg","controller1");
         return "hello"; //封装并跳转的页面名称
     }

SpringMVC

SpringMVC内置的转发与重定向的功能,不需要视图解析器

 @RequestMapping("/mvc/t1")
 public String test1(){
     //转发
     return "/hello.jsp";
 }
 @RequestMapping("/mvc/t2")
 public String test2(){
     //显式转发
     return "forward:/hello.jsp";
 }
 @RequestMapping("/mvc/t3")
 public String test3(){
     //重定向
     return "redirect:/hello.jsp";
 }

转发不改变url,但是重定向是请求一个新的网页

 

 

如果使用视图解析器的方式,默认情况下是转发,若想要实现重定向,则在return中,加redirect即可实现

5.2、数据处理

处理提交的数据

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

    提交数据:http://localhost:8080/user/t1?name=mosang

 @RequestMapping("/user")
 public class UserController {
     @RequestMapping("/t1")
     public String test1(String name){
         System.out.println("接收到的参数值为:"+name);
         return "hello";
     }

 

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

    提交数据:http://localhost:8080/user/t1?username=mosang

     @RequestMapping("/t2")
     public String test2(@RequestParam("username") String name){
         System.out.println("接收到的参数值为:"+name);
         return "hello";
     }

 

若仍使用name来提交则会报错

 

当设置了@Requestparam 则必须使用设置的参数名来提交

  1. 提交的是一个对象

     public class user {
         public int id;
         public String name;
         public int age;
     }

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

    提交数据:http://localhost:8080/user/t3?id=1&name=mosang&age=20

     @RequestMapping("/t3")
     public String test3(user user){
         System.out.println("接收到的参数值为:"+user.toString());
         return "hello";
     }

     

  1. 接收端接收到用户传递的参数,若判断参数的名字与方法名一致,则可直接使用

  2. 若传递的是一个对象类型,先判断是否与对象中 的字段名匹配,若匹配则可使用,若无法匹配则为null

显示数据给前端

  1. ModelAndView

     public class controller implements Controller {
         @Override
         public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
             ModelAndView mv = new ModelAndView();
             mv.addObject("msg","hellocontroller");
             mv.setViewName("hello");
             return mv;
         }
     }

    通过ModelAndView对象来进行转发与数据回显

  2. Model

     public class controller1 {
         @RequestMapping("/test2")
         public String test(String name,Model model){
             model.addAttribute("msg",name);
             return "hello"; //封装并跳转的页面名称
         }

    使用Model方法,将数据返回给前端

  3. ModelMap

     public class controller1 {
         @RequestMapping("/test2")
         public String test(String name,ModelMap model){
             model.addAttribute("msg",name);
             return "hello"; //封装并跳转的页面名称
         }

对比:

 ModelAndView:可以在存储数据的同时,设置返回的逻辑视图,控制展示层的跳转
 ModelMap:继承了LinkedMap,有LinkedMap的所有方法和特性
 Model:简化了ModelMap的方法,只适用于存储数据

5.3、乱码问题

对于一个表单页面,当用户所提交的数据为中文字符时,可能会出现乱码,例如:

对于一个简单的表单:

 <form action="/enc/t1" method="post">
 <input type="text" name="name">
     <input type="submit">
 </form>

后台的处理类:

 @Controller
 @RequestMapping("/enc")
 public class EncodingController {
     @PostMapping("/t1")
     public String test1(String name, Model model){
         model.addAttribute("msg",name);
         System.out.println(name);
         return "hello";
     }
 }

 

发现输出为乱码

解决:

  1. 使用过滤器

SpringMVC提供了一个过滤器,可直接在web.xml中配置即可

 <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>
  1. 修改Tomcat配置

    文件位置:Tomcat8\conf\server.xml

    添加

     <Connector URIEncoding="utf-8" port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值