• SpringMVC传值、转发、重定向例子


    1. 练习接收页面参数值
      1. 使用request
      2. 使用@RequestParam注解
      3. 使用实体对象
    2. 练习向页面传出数据
      1. 使用HttpServletRequest和session
      2. 使用ModelAndView对象  (内部为利用HttpServletRequest的Attribute传递数据到页面)
      3. 使用ModelMap对象 (内部为利用HttpServletRequest的Attribute传递数据到页面)
      4. 使用@ModelAttribute注解 (内部为利用HttpServletRequest的Attribute传递数据到页面)
    3. 练习使用session
      1. 在Controller方法参数上直接声明HttpSession即可使用
    4. 练习重定向
      1. 使用RedirectView
      2. 使用redirect:
    package web;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.view.RedirectView;
    
    import entity.User;
    
    //非注解方式
    //public class HelloController implements Controller {
    //
    //
    //    public ModelAndView handleRequest(HttpServletRequest request,
    //            HttpServletResponse response) throws Exception {
    //        System.out.println("Hello, Controller.");
    //        return new ModelAndView("jsp/hello");
    //    }
    //
    //}
    
    @Controller
    @RequestMapping("/demo")
    public class HelloController{
        private Integer age=22;
        
        @RequestMapping("hello.do")
        public ModelAndView hello(HttpServletRequest request,
                HttpServletResponse response) throws Exception{   
            return new ModelAndView("jsp/hello");
        }
        
        /**
         * 测试request接收参数*/
        @RequestMapping("test1.do")
        public ModelAndView test1(HttpServletRequest req){
            String userName = req.getParameter("userName");
            String password = req.getParameter("password");
            System.out.println(userName);
            System.out.println(password);
            return new ModelAndView("jsp/hello");
        }

        /**
        * 测试sping会自动将表单参数注入到方法参数
        * 最好每个形参前都添加@requestparameter
        * 通过反射只能得到方法参数类型不能等到方法参数名称 没有加注解能成功获得为编译器自动添加
        */

        @RequestMapping("test2.do")
        public ModelAndView test2(String userName,
                @RequestParam("password") String pwd){
            System.out.println(userName+","+pwd);
            return new ModelAndView("jsp/hello");
        }
        
        /**
         * 测试对象接收参数
         */
        @RequestMapping("test3.do")
        public ModelAndView test3(User user){
            System.out.println(user);
            return new ModelAndView("jsp/hello");
        }
        
        /**
         * 使用ModelAndView传出参数 内部 HttpServletRequest的Attribute传递到jsp页面
       * ModelAndView(String viewName,Map data)data是处理结果
    */ @RequestMapping("test4.do") public ModelAndView test4(User user){ Map<String, Object> data = new HashMap<String, Object>(); data.put("user", user); return new ModelAndView("jsp/hello",data); } /** * 使用ModelMap传出参数 内部HttpServletRequest的Attribute传递到jsp页面 */ @RequestMapping("test5.do") public ModelAndView test5(User user,ModelMap modelMap){ modelMap.put("user", user); return new ModelAndView("jsp/hello"); } /** * 使用ModelAttribute 内部HttpServletRequest的Attribute传递到jsp页面 * 在Contoller的参数部分或者bean属性方法上使用 */ @RequestMapping("test6.do") public ModelAndView test6(@ModelAttribute("user")User user){ return new ModelAndView("jsp/hello"); } @ModelAttribute("age") public Integer getAge(){ return age; } /** * session存储 可以使用HttpServletRequest的getSession方法访问 */ @RequestMapping("test7.do") public ModelAndView test7(HttpServletRequest req){ HttpSession session = req.getSession(); session.setAttribute("salary", 6000.0); return new ModelAndView("jsp/hello"); } //返回String 转发 @RequestMapping("/test8.do") public String test8(User user, ModelMap model) { model.addAttribute("user", user); return "jsp/hello"; } /** * 错误页面 */ @RequestMapping("test9.do") public String test9(){ return "error/error"; } /** *使用RedirectView重定向 */ @RequestMapping("test10") public ModelAndView test10(User user){ if(user.getUserName().equals("123")){ return new ModelAndView("jsp/hello");//test10.do 转发 }else{ return new ModelAndView(new RedirectView("test9.do"));//test9.do?age=22 重定向 } } /** * 使用redirect重定向 */ @RequestMapping("test11") public String test11(User user){ if(user.getUserName().equals("123")){ return "jsp/hello"; }else{ return "redirect:test9.do"; } } }

    user实体

    package com.tarena.entity;
    import java.io.Serializable;
    public class User implements Serializable {
        private Integer id;
        private String userName;
        private String password;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getUserName() {
            return userName;
        }
        public void setUserName(String userName) {
            this.userName = userName;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
    }
  • 相关阅读:
    json和pickle模块
    53. 最大子序和
    69. x 的平方根
    leetcode刷题周记【2020.9.21-2020.9.26】
    推荐学习 Java 的地方
    5、SpringBoot:配置文件及自动配置原理
    4、SpringBoot:运行原理探究
    3、SpringBoot:helloworld
    2、SpringBoot:什么是微服务
    1、SpringBoot:什么是SpringBoot
  • 原文地址:https://www.cnblogs.com/ZqNote/p/5908510.html
Copyright © 2020-2023  润新知