SpringMVC uses annotations to complete the ing details between the URL and the Controller & amp; Method
1. Use @ RequestMapping to map URLs to classes and methods.
2. @ RequestMapping annotation can be identified on the class or method 1 ). if the class is not identified, the method is directly relative to the root directory of the WEB application. <a href ="Helloworld? Name = springMVC"> Hello SpringMVC
2 ). if the @ RequestMapping ID is displayed on the class, the @ RequestMapping/in the class is the root directory relative to the WEB application, the method/Corresponds to the path @ RequestMapping ("/springmvc") above the class ")
@ Controller
Public
ClassSpringMVCTest {
@ RequestMapping ("/testRedirect ")
PublicString testRedirect () {System.
Out. Println ("testRedirect ");
Return"Redirect:/index. jsp ";}
→<A href =
"Springmvc/testRedirect"> Test Redirect
3. @ RequestMapping provides fine-grained ing details.
URL, request method, whether it contains any parameters, whether the parameter value is equal to the defined value, including the request header...
1). ing URL: Use the value attribute of @ RequestMapping
2). ing Request method: Use the method attribute of @ RequestMapping
Example: @ RequestMapping (value = "/testRequestMapping2", method = RequestMethod.
POST, Params = {"name", "age = 12 "})
PublicString testRequestMapping2 () {System.
Out. Println ("testRequestMapping2 ");
Return
SUCCESS;}
@ RequestMapping ("/testRequestHeader ")
PublicString testRequestHeader (@ RequestHeader (value = "Accept-Language") String al, @ RequestParam ("age ")
IntAge, @ CookieValue ("JSESSIONID") String jid) {System.
Out. Println ("Accept-Language:" + al + ", age:" + age + ", JSESSIONID:" + jid );
Return
SUCCESS;}
4.★How do I get the Request Parameters in the target method?
1). Use annotation: @ RequestParam or @ PathVariable. ①. @ RequestParam: used to map request parameters.
@ RequestParam (value = "age", required = false) int age: Assign the age request parameter to the age input parameter, and the request parameter is not required!
Required is true by default. If this request parameter is not provided, SpringMVC throws an exception @ RequestMapping (value = "testRequestParam ")
PublicString testRequestParam (@ RequestParam (value = "username", required =
False) String un) {System.
Out. Println ("username:" + un );
Return
SUCCESS;}
②. @ PathVariable: You can bind the placeholder parameter in the URL to the input parameter of the controller processing method.
The {xxx} placeholder in the URL can be bound to the input parameter of the operation method through @ PathVariable ("xxx.
@ RequestMapping ("/testRequestMappingPathVariable/{id }")
PublicString testPathVariable (@ PathVariable ("id") Integer id) {System.
Out. Println ("testRequestMappingPathVariable:" + id );
Return
SUCCESS;}
2 ).
Use classes directly: Use a custom class directly in the input parameter as the input parameter of the method. Applies to form requests. Form parameters are directly mapped to the attributes of objects.
------------------------------ Action ------------------------------------------------------------ @ RequestMapping ("/testPojo ")
PublicString testPojo (User user) {System.
Out. Println ("user:" + user );
Return
SUCCESS;}
--------------------------- Page request -------------------------------------------------- "Springmvc/testPojo"Method =
"POST"> Username: "Text"Name =
"Username"/>
Age: "Text"Name =
"Age"/>
Email: "Text"Name =
"Email"/>
"Submit"Value =
"Test Pojo"/> --------------------------------- POJO ---------------------------------------------------
Public
ClassUser {
PrivateInteger id;
PrivateString username;
PrivateString email;
Private
IntAge;
5. You can use
ServletAPI: Request, response, session as the method input parameter @ RequestMapping ("/testServletAPI ")
PublicString testServletAPI (HttpServletRequest request, HttpServletResponse response, HttpSession session, Reader reader)
ThrowsIOException {System.
Out. Println (request); System.
Out. Println (response); System.
Out. Println (session); System.
Out. Println (request. getReader () = reader); // true
Return
SUCCESS;}
6. Use IO as the input parameter: Servlet's ServletRequest has the getInputStream () and getReader () Methods to read request information. The getOuputStream () and getWriter () Methods of the Servlet's ServletResponse can be used to output response information.
@ RequestMapping ("/testWriter ")
Public
VoidTestWriter (Writer out)
ThrowsIOException {out. write ("Hello ~ ");}