Annotation-based Springmvc

Source: Internet
Author: User

Springmvc according to the request for comments

Springmvc Previous comments are in use. First, for the description. after the spring mvc3.x version number, a very easy to enable annotation method is only required in the-servlet.xml plus <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 a controller package, when the application is started. You will proactively scan the annotations in your controller. Find the appropriate controller and processing method for the request mapping.

in Request mapping, the main use of @controller annotations and @requestmapp annotations, @Controller annotation of a class, indicating that the class is a controller, @RequestMapping annotation method, containing the map name of the parameters. The parameter specifies the appropriate processing method for the mapping . Like what:

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, the method and the Param, method has requestmethod.post, requestmethod.get equivalent, indicating the way of the request, such as 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. For example, @requestmapping (value= "Login", param={"name", "password"}) indicates that the request must include the name and password two parameters before this method is processed.

@RequestMapping not only be able to annotate methods, but also to annotate classes. Note classes are typically used in multiprocessor processors, such as the @requestmapping ("/user") annotated Logincontroller class in the previous example. Then the value of the action in the form form is changed to "User/login" ability to pass the request to the login method.

In the request path, no "/" represents an absolute path. Add "/" represents the path relative to the current resource.

Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

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.