zoukankan      html  css  js  c++  java
  • Spring MVC参数处理


    Spring MVC通过分析处理处理方法签名,将HTTP请求信息自动绑定到处理方法的参数中。

    @RequestMapping : 获取前台传递的参数

        @RequestMapping("/p1")
        public String param1(String uname,String pwd){
            //接收参数
            //调用业务层
            //调用Dao。。。。
            if("admin".equals(uname)&&"123".equals(pwd)){
                //登录成功
            }else{
                //登录失败
            }
            System.out.println("uname="+uname+",pwd="+pwd);
            return "hello";
        }

    @RequestHeader : 将请求头中的属性值绑定到处理方法的参数中。

    @RequestMapping("/p4")
        public String param4(@RequestHeader("Cookie")String cookie){
            System.out.println("cookie:"+cookie);
            return "hello";
        }
    

    @CookieValue: 将请求中的Cookie的值绑定到处理方法的参数中。

    @RequestMapping("/p5")
        public String param5(@CookieValue("JSESSIONID")String sessionid){
            return "hello";
        }

    使用POJO,Spring MVC 会将请求参数和POJO中的属性进行匹配,自动填充属性,并支持级联。

    @RequestMapping("/p6")
        public String param6(User user){
            //new User
            //set+Uname
            System.out.println(user);
            return "hello";
        }

    使用Servlet API作为参数

    HttpServletRequest

    @RequestMapping("/s1")
        public String s1(HttpServletRequest request){
            System.out.println(request.getParameter("uname"));
            System.out.println(request.getParameter("pwd"));
            request.setAttribute("sex", "man");
            return "hello2";
        }

    HttpServletResponse

    /**
         * 方法返回值可以是void
         * 如果没有HttpServletResponse,返回值一定不能是void
         */
        @RequestMapping("/s2")
        public void s2(HttpServletResponse response) throws IOException{
            /*response.sendRedirect("../WEB-INF/pages/hello2.jsp");*/
            response.sendRedirect("../test.jsp");
        }
    
    
    @RequestMapping("/s3")
        public void s3(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
            request.getRequestDispatcher("../test.jsp").forward(request, response);
        }
    

    HttpSession

    /**
         * 把User保存在session中
         * @param session
         */
        @RequestMapping("/s4")
        public String s4(HttpSession session,User user){
            session.setAttribute("user", user);
            return "hello3";
        }
    • @SessionAttributes(“user”)
    • @SessionAttributes:
    • 可以把模型数据当中对应的对象存储到session中
    • session.setAttr(“user”,request.getAttr(“user”))
    public class SessionController {
    
        @RequestMapping("/se1")
        public String sess1(User user,Notice notice) {
            return "hello6";
        }
    
        /**
         * 如果参数列表当中有两个类型相同的参数,只会在模型数据中存储一个
         * 使用@ModelAttribute可以给两个类型相同的对象做区分,
         * 让他们都可以存储在模型数据中
         * 如果遇到有两个类型相同并且都需要存储到session中时,
         * 可以使用model去解决问题
         */
        @RequestMapping("/se2")
        public String sess2(User user,User user2,Model model) {
            model.addAttribute("user", user);
            model.addAttribute("user2", user2);
            return "hello6";
        }
    
    }

    使用流作为参数

    InputStream

    OutputStream

    @RequestMapping("/s5")
        public void s5(OutputStream os) throws IOException{
            os.write(new String("hcfgyhgghyvf").getBytes());
        }

    Reader

    Writer

        @RequestMapping("/s6")
        public void s6(PrintWriter writer) {
            writer.print("asdfghjkl");
        }

    总结:

    • request,response,session 可以直接使用,不需要创建这个对象的类型
    • 如果涉及到页面跳转,尽量不要使用request,response
    • 如果使用了request和response进行页面跳转,它就不再使用视图解析器去匹配页面了(需要根据当前控制器的路径去匹配)
    • 方法带返回值:String要在控制器当中做页面的跳转。(普通的控制器)
    • 方法不带返回值:
    • 1.通过request(转发)或者response(重定向)这里两个对象去做跳转
    • 2.通过OutputStream或Writer直接在也页面显示内容。(前台是Ajax访问获取数据时)
    • 如果返回值是void可以搭配ajax使用(ajax拿到的就是当前控制器的信息)
    • 如果返回值不是void,必然要跳转页面。
  • 相关阅读:
    Django和Angular.js模板标签冲突的解决方式
    ImageMagick 使用经验
    Getting Real内容浓缩
    RA layer request failed
    [硬件结构]硬件体系结构中的缓存的定性与定量分析案例
    hdu5373
    百度地图之标注一组地理坐标<2>
    【KMP】hdu1867(A + B for you again) 杭电java a题真坑
    hdu1034 简单模拟
    杂谈之WEB前端project师身价
  • 原文地址:https://www.cnblogs.com/aixing/p/13327654.html
Copyright © 2011-2022 走看看