SPRINGMVC Annotation-based requests
before using SPRINGMVC annotations, first enable annotations, in the spring The mvc3.x version provides a very simple way to enable the annotation method with only the-servlet.xml added <mvc:annotation-driven/>, You also need to tell the application which packages use annotations and add <context:component-scanbase-package= "Controller" to the-servlet.xml/> Describes the use of annotations in the controller package, the annotations in the controller are automatically scanned when the application is started, and the controller and processing methods corresponding to the request mappings are found.
in Request mapping, the main use of @controller annotations and @requestmapp annotations, @Controller annotation A class, indicating that the class is a controller, @RequestMapping annotation method, containing the mapping name parameters, This parameter specifies the processing method that corresponds to the mapping . For example:
login.jsp
< form Action="Login"method= "">
username:<inputtype="text"name="Name "/><br/>
Password:<inputtype="password"name="password"/ ><br/>
< input type="Submit"value="Login"/>
</ form >
Logincontroller.java
@Controller
Public class Logincontroller {
@RequestMapping ("/login")
Public Modelandviewlogin (httpservletrequest request,httpservletresponse response)
{
System. out. println ("-------------");
returnnew Modelandview ("OK");
}
}
-servlet.xml
< Mvc:annotation-driven />
< Context:component-scan base-package="Controller"/>
< Bean id= "viewresolver"class=" Org.springframework.web.servlet.view.InternalResourceViewResolver ">
< Property name="prefix"value="/"/>
< Property name="suffix"value=". jsp"/>
</ Bean >
in @requestmapping, the other two parameters, method and Param, method has requestmethod.post, requestmethod.get equivalent, indicating the way of the request, for example in the above example @requestmapping (value= "/login", method= REQUESTMETHOD.PST) means that the request processed by the method must be a POST request method, then method= "POST" in the form form
The value of Param is a string array that indicates which request parameters must be included in the request, such as @requestmapping (value= "Login", param={"name", "Password"}) The description request must contain the name and password two parameters before the method is processed.
@RequestMapping can not only annotate the method, but also annotate the class, the annotation class is generally used in a multi-method processor, such as in the previous example using @requestmapping ("/user") Annotated Logincontroller class, Then the value of the action in the form form is changed to "User/login" in order to pass the request to the login method.
In the request path, no "/" represents an absolute path, plus "/" represents the path relative to the current resource.
Annotation-based Springmvc