- 练习接收页面参数值
- 使用request
- 使用@RequestParam注解
- 使用实体对象
- 练习向页面传出数据
- 使用ModelAndView对象
- 使用ModelMap对象
- 使用@ModelAttribute注解
- 练习使用session
- 在Controller方法参数上直接声明HttpSession即可使用
- 练习重定向
- 使用RedirectView
- 使用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会自动将表单参数注入到方法参数 */ @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页面 */ @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; } }
时间: 2024-10-12 02:40:25