美文网首页
一起来学习SpringMVC:跳转方式,数据处理,乱码以及Res

一起来学习SpringMVC:跳转方式,数据处理,乱码以及Res

作者: __y | 来源:发表于2018-06-08 19:47 被阅读27次

    前言:前面我们已经入了门了,看看SpringMVC的一些知识点和应用吧!本文的知识点介绍:

    • SpringMVC的跳转方式
    • 数据的处理
    • 乱码以及Restful
    • RequestMapping详解
      废话不多说,我们进入正题

    1.跳转方式

    通过ModelAndView对象

    通过ModelAndView对象,根据View的名称和视图解析器跳转到指定的页面

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello")
        public ModelAndView hello() {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("hello");
            mv.addObject("msg", "hello springMVC");
            return mv;
        }
    
    }
    

    通过ServletAPI对象来实现

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello")
        public void hello(HttpServletResponse response,HttpServletRequest request) throws IOException {
         resp.getWriter().println("hello springMVC");
        }
    
    }
    

    重定向:

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello")
        public void hello(HttpServletResponse response,HttpServletRequest request) throws IOException {
    
            response.sendRedirect("index.jsp");
        }
    

    请求转发:

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello")
        public void hello(HttpServletResponse response,HttpServletRequest request) throws IOException, ServletException {
            request.getRequestDispatcher("index.jsp").forward(request,response);
        }
    
    }
    
    

    通过SpringMVC(没有视图解析器的时候)

    转发实现的第一种方式:

      @RequestMapping(value = "/hello1")
        public String hello() {
            //转发
            return "index.jsp";
        }
    
    

    转发实现的第二种方式:

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello1")
        public String hello() {
            //转发
            return "forward:index.jsp";
        }
    
    }
    
    

    重定向:

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello() {
            //重定向
            return "redirect:index.jsp";
        }
    
    }
    
    

    通过SpringMVC(有视图解析器的时候,测试的时候记得加上视图解析器的配置)
    转发方式:

    @RequestMapping(value = "/hello1")
        public String hello() {
            //转发
            return "index";
        }
    
    

    注意:重定向不需要视图解析器,如果编写成return "redirect:index"会跳到另外的映射路径去,因为相当于重新发了一次请求;

    2. 数据处理

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

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello(String name) {
            System.out.println(name);
            return "index.jsp";
        }
    
    }
    
    
    image.png image.png

    提交的域名称和处理方法不一致

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello(@RequestParam("username") String name) {
            System.out.println(name);
            return "index.jsp";
        }
    
    }
    
    
    image.png image.png

    提交的是一个对象

    public class User {
        private Integer id;
        private String name;
        //get set 略
    }
    
    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello(User user) {
            System.out.println(user.toString());
            return "index.jsp";
        }
    
    }
    
    
    image.png image.png

    将数据显示到UI层

    第一种:通过ModeAndView的方式---需要视图解析器(相当于转发)

    @Controller
    public class HelloController {
        @RequestMapping(value = "/hello")
        public ModelAndView hello() {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("hello");
            mv.addObject("msg", "annotation ------");
            return mv;
        }
    
    }
    

    第二种:通过ModelMap---不需要视图解析器

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/hello")
        public String hello(String name, ModelMap modelMap) {
            //ModelMap 必须放在参数里面声明,不然会没有效果 
            modelMap.addAttribute("msg",name);
            return "index.jsp";
        }
    
    }
    
    image.png

    3.乱码以及Restful

    乱码解决:

    SpringMVC中提供了过滤器来解决乱码问题;
    在web.xml中配置

     <!-- 编码过滤器 -->
        <filter>
            <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    

    值得注意的是:该过滤编码器只能解决POST的乱码问题!
    如果是get方式的话:
    1.修改Tomcat配置
    2.自定义一个乱码解决的过滤器

    restful

    从上文可以看出我们以前传值是/hello?name=xxx这种,下面要介绍的是restful风格的传值方式

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/{name}/hello")
        public String hello(@PathVariable String name, ModelMap modelMap) {
            System.out.println(name);
            modelMap.addAttribute("name",name);
            return "/index.jsp";
        }
    
    }
    
    
    
    image.png

    4.RequestMapping详解

    @RequestMapping能够控制请求路径和请求方式
    1.多种请求路径,分别解决相应的业务

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/{name}/hello")
        public String hello(@PathVariable String name, ModelMap modelMap) {
            System.out.println(name);
            modelMap.addAttribute("name",name);
            return "/index.jsp";
        }
        @RequestMapping(value = "/{id}/hello2")
        public String hello(@PathVariable int id, ModelMap modelMap) {
            System.out.println(id);
            modelMap.addAttribute("name",id);
            return "/index.jsp";
        }
    
    }
    
    

    PS:
    当我们分模块开发的时候可以把@RequestMapping这个注解写到类上面去代表了模块的路径
    2.限制请求方式
    我们如果想要限定某个业务控制方法,只允许GET或POST请求方式访问。还是通过@RequestMapping来实现。只要设定它的method属性就行了!

    @Controller
    public class HelloController {
    
        @RequestMapping(value = "/{name}/hello",method = RequestMethod.POST)
        public String hello(@PathVariable String name, ModelMap modelMap) {
            System.out.println(name);
            modelMap.addAttribute("name",name);
            return "/index.jsp";
        }
    

    相关文章

      网友评论

          本文标题:一起来学习SpringMVC:跳转方式,数据处理,乱码以及Res

          本文链接:https://www.haomeiwen.com/subject/nupasftx.html