SpringMVC beginners (2) Application annotation + annotation optimization, springmvc Annotation

Source: Internet
Author: User

SpringMVC beginners (2) Application annotation + annotation optimization, springmvc Annotation

The previous article has introduced how to build an environment using SpringMVC and a simple demo test. Next we will introduce how to implement it using annotations.

 

Add configuration file

 

SpringAnnotation-servlet.xml

<! -- Annotation scan package --> <context: component-scan base-package = "com. tgb. web. controller. annotation"/> <! -- Enable annotation. These two methods are common --> <! -- <Bean class = "org. springframework. web. servlet. mvc. annotation. annotationMethodHandlerAdapter "/> <bean class =" org. springframework. web. servlet. mvc. annotation. defaultAnnotationHandlerMapping "> </bean> --> <mvc: annotation-driven> </mvc: annotation-driven> <bean id =" viewResolver "class =" org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean>

Web. xml


<Servlet> <servlet-name> SpringMVC </servlet-name> <servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class> <! -- Modify the name and path of the configuration file --> <init-param> <param-name> contextConfigLocation </param-name> <! -- <Param-value> classpath *: config/SpringMVC-servlet.xml </param-value> -- & gt; <param-value> classpath *: config/springAnnotation-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-name> SpringMVC </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>


Instance test:

 

 

Add UserController:


@ Controllerpublic class UserController {/* method annotation. value is the browser access address and method is the Receiving method */@ RequestMapping (value = "/user/addUser", method = RequestMethod. GET) public ModelAndView addUser () {String result = "this is addUser ------"; /* method of passing values to the foreground interface --- return the foreground page with parameters */return new ModelAndView ("/jquery", "result", result );} @ RequestMapping (value = "/user/delUser", method = RequestMethod. GET) public ModelAndView delUser () {String result = "this is delUser ------"; return new ModelAndView ("/jquery", "result", result );} @ RequestMapping (value = "/user/toUser", method = RequestMethod. GET) public ModelAndView toUser () {/* returned page */return new ModelAndView ("/jquery ");}}


Jquery. jsp (the submission method is post, and if the addUser's receiving method is GET at this time)

<Form action = "/SpringMVC/user/addUser" method = "post"> 

The following error occurs:

 

There is no problem accessing toUser, but an error is reported when you click the button!




Explanation:

 

When we access: http: // localhost: 8080/SpringMVC/user/toUser, The jquery page is displayed. However, when we click the button on the jquery page, the post method is submitted to the addUser method, and the addUser method receives the Get method, so an error is returned. The correct method is to change the Receiving Method of addUser to POST.

 

Execution Process in the correct way:

 

First, access toUser, go to the jquery page, click the button on the jquery page, submit post to addUser, and return the result parameter in addUser




Note:

 

Direct access to the front-end page only supports the GET method, that is, when our controller receives the GET method, you can enter the corresponding address in the browser to access the corresponding page

 

For example:




Because the addUser receiving method is GET at this time, but if we change the Receiving Method to Post, the following error will be reported, because when we access the page directly, the receiving method is Post, the default submission method is GET.




We want the get Method Request to be available, and the post Method Request

If you do not want to distinguish it clearly, delete the configuration directly (method = RequestMethod. GET)

 

 

SpringMVC annotation Optimization

 

1. You can modify

@ RequestMapping ("/user ")

2. Delete the method configuration method = RequestMethod. GET.

3. the return value is of the String type.

4. Use with Parameters

Request. setAttribute ("result", result);, import javax. servlet. http. HttpServletRequest;

 

Optimized Controller:


Conclusion: The application annotation method eliminates the need to write so much ing code in the configuration file, reducing the workload of developers. However, we need to understand both the annotation and configuration file methods. I personally think the configuration file method is easier for us to understand.




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.