• (四)


    一. 使用Request和Session传值

    使用这种方式需要先引入相关依赖:

                ...
            </dependency>
    
            <dependency>
                <!--servlet编译环境-->
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <!--jsp编译环境-->
                <groupId>javax.servlet</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.0</version>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    
    </project>    

    servlet代码:

    @Controller
    @RequestMapping("/data")
    public class PassData {
    
        //使用request和session传值
        @RequestMapping("/test1")
        public String test1(HttpServletRequest request, HttpSession session) {
            System.out.println("test1");
            request.setAttribute("name", "张三");
            session.setAttribute("age", 18);
            return "data1";
        }
    }

    data1.jsp代码: (使用requestScope / sessionScope 获取值)

    <html>
    <head>
        <title>data1</title>
    </head>
    <body>
    name: ${requestScope.name}<br>
    age: ${sessionScope.age}
    </body>
    </html>

    访问结果:

     二. 使用model传值

    servlet代码:

    @Controller
    @RequestMapping("/data")
    @SessionAttributes(value = {"country","city"}) //设置model的session
    public class PassData {
    
        //使用model传值
        @RequestMapping("/test2")
        public String test2(Model model) {
            System.out.println("test2");
            model.addAttribute("gender", true);
            model.addAttribute("country", "中国");
            model.addAttribute("city", "杭州");
            return "data2";
        }

    data2.jsp代码: (同样使用requestScope / sessionScope 获取值, model会把数据复制到requestScope中)

    <html>
    <head>
        <title>data2</title>
    </head>
    <body>
        gender: ${requestScope.gender}<br>
        country: ${sessionScope.country}<br>
        city: ${sessionScope.city}
    </body>
    </html>

    访问结果:

     三. 使用status.setComplete()清空所有通过model存入的session

    servlet代码:

    @Controller
    @RequestMapping("/data")
    public class PassData {
    
        @RequestMapping("/test3")
        public String test3(SessionStatus status){
            //清空所有通过model存入的session
            status.setComplete();
            return "data2";
        }

    访问结果:

     

     四. 使用 ModelAndView 传值:

    servlet代码:

    @Controller
    @RequestMapping("/data")
    public class PassData {
    
        @RequestMapping("/test4")
        public ModelAndView test4(){
            ModelAndView modelAndView = new ModelAndView();
            //将会跳转到这个jsp
            modelAndView.setViewName("forward:/data3.jsp");
            modelAndView.addObject("name", "Saber");
            return modelAndView;
        }
    }

    data3.jsp代码:

    <html>
    <head>
        <title>data3</title>
    </head>
    <body>
    data3<br>
    name: ${requestScope.name}
    </body>
    </html>

    访问结果:

  • 相关阅读:
    Maven关于web.xml中Servlet和Servlet映射的问题
    intellij idea的Maven项目运行报程序包找不到的错误
    修改Maven项目默认JDK版本
    刷题15. 3Sum
    刷题11. Container With Most Water
    刷题10. Regular Expression Matching
    刷题5. Longest Palindromic Substring
    刷题4. Median of Two Sorted Arrays
    刷题3. Longest Substring Without Repeating Characters
    刷题2. Add Two Numbers
  • 原文地址:https://www.cnblogs.com/Ryan368/p/14116572.html
Copyright © 2020-2023  润新知