Annotation-based Springmvc

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.