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; } }