一、@Controller
- @Controller 标记一个类是Controller
二、RequestMapping 地址映射
-
2.1 Value的操作。
注解在类上面 (父)
注解在方法上面。 (子)
-
2.1.1 SpringMVC完整的请求,就是父子加请来的请求。
<a href="<%=request.getContextPath()%>/userController/list">list方法</a> <a href="<%=request.getContextPath()%>/userController/save">save方法</a>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/userController") public class UserController { @RequestMapping(value = "/list") public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } @RequestMapping(value = "/save") public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } }
-
2.2 用于映射请求中的参数。
<a href="<%=request.getContextPath()%>/paramController?task=list">list方法</a> <a href="<%=request.getContextPath()%>/paramController?task=save">save方法</a>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/paramController") public class ParamController { @RequestMapping(params = "task=list") public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("r执行list方法"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } @RequestMapping(params = "task=save") public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("r执行save方法"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } }
-
2.3 Method:表示是处理哪一种请求.GET/POST
<a href="<%=request.getContextPath()%>/methodController/add">list方法</a> <form method="post" action="<%=request.getContextPath()%>/methodController/add"> <input type="submit" value="提交"/> </form>
controller:
package com.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/methodController") public class MethodController { @RequestMapping(value = "/add", method = RequestMethod.GET) public ModelAndView add(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("处理GET请求"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } @RequestMapping(value = "/add", method = RequestMethod.POST) // @RequestMapping(value = "/add", method = { RequestMethod.GET, // RequestMethod.POST }) public ModelAndView save(HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("处理POST请求"); ModelAndView mv = new ModelAndView(); mv.addObject("req_key", "Model Data"); mv.setViewName("succ"); return mv; } }
-
2.4 RequestParam:获取地址中的参数信息。
<a href="<%=request.getContextPath()%>/requestParamController/edit?userid=1">list方法</a> <a href="<%=request.getContextPath()%>/requestParamController?task=edit&userid=2">list方法</a>
controller
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/requestParamController") public class RequestParamController { @RequestMapping(value = "/edit", method = RequestMethod.GET) public ModelAndView edit_1( @RequestParam(name = "userid", required = false) String id) throws Exception { System.out.println("userid = " + id); return null; } @RequestMapping(params = "task=edit", method = RequestMethod.GET) public ModelAndView edit_2(String userid) throws Exception { System.out.println("task=edit中的userid = " + userid); return null; } }
-
2.5 PathVariable:REST风格.
<a href="<%=request.getContextPath()%>/pathVarController/edit/1/admin">edit方法</a> <a href="<%=request.getContextPath()%>/pathVarController/edit/2/guest">edit方法</a> <a href="<%=request.getContextPath()%>/pathVarController/edit/3/test">edit方法</a> <a href="<%=request.getContextPath()%>/pathVarController/3/admin/delete">通配符一个方法</a> <a href="<%=request.getContextPath()%>/pathVarController/3/admin/test/xxxx/yyyy/remove">通配符多个方法</a>
controller
package com.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/pathVarController") public class PathVarController { @RequestMapping(value = "/edit/{userid}/{username}", method = RequestMethod.GET) public ModelAndView edit_1(@PathVariable(value = "userid") String userid, @PathVariable(value = "username") String username) throws Exception { System.out.println("userid = " + userid); System.out.println("username = " + username); return null; } /** * 通配符的写法 * * 1个*代表通配一个地址 * * 2个*代表通配多个地址 */ @RequestMapping(value = "{userid}/*/delete", method = RequestMethod.GET) public ModelAndView delete_1(@PathVariable(value = "userid") String userid) throws Exception { System.out.println("delete_1====>userid = " + userid); return null; } @RequestMapping(value = "{userid}/**/remove", method = RequestMethod.GET) public ModelAndView delete_2(@PathVariable(value = "userid") String userid) throws Exception { System.out.println("remove_2====>userid = " + userid); return null; } }
2.6 header和cookie
<a href="<%=request.getContextPath()%>/headerController/header">header</a> <a href="<%=request.getContextPath()%>/headerController/cookie">cookie</a>
controller
package com.shyroke.controller; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.CookieValue; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping(value = "/headerController") public class HeaderController { @RequestMapping(value = "/header", method = RequestMethod.GET) public ModelAndView header(@RequestHeader(name = "Host") String Host, @RequestHeader(name = "User-Agent") String agent, HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("header"); System.out.println("Host= " + Host); System.out.println("agent= " + agent); System.out.println("RequestHeader = " + request.getHeader("Host")); Cookie cookie_a = new Cookie("cookie_a", "Admin/GTUeST/axxxx"); cookie_a.setPath("/"); response.addCookie(cookie_a); return null; } @RequestMapping(value = "/cookie", method = RequestMethod.GET) public ModelAndView cookie( @CookieValue(name = "JSESSIONID", required = false) String sessionID, @CookieValue(name = "cookie_a", required = false) String cookie_a) throws Exception { System.out.println("cookie_1 = " + sessionID); System.out.println("cookie_a = " + cookie_a); return null; } }
结果
时间: 2024-11-01 19:26:01