The previous article has been introduced using Springmvc and a simple way to build the environment Demo test, let's take a look at how the annotations are used, and how they are implemented.
Add a configuration file
Springannotation-servlet.xml
<!--note Scanning package--><context:component-scan base-package= "Com.tgb.web.controller.annotation"/><!--opening annotations, Both of these methods are universal--><!--<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>
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 to <init-param> <param-name>contextConfigLocation</param-name> <!--<param-value >classpath*:config/SpringMVC-servlet.xml</param-value> <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>
Example test:
Add Usercontroller :
@Controllerpublic the annotation for the class Usercontroller {/* method, value is the access address of the browser, method is the Receive method */@RequestMapping (value= "/user/adduser" , method=requestmethod.get) public Modelandview AddUser () {String result = "This is addUser------";/* How to pass the value to the foreground interface---returned to 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 () {/* return page */return new Modelandview ("/jquery");}}
jquery.jsp (the submission method is Post , and if at this point AddUser the receive mode is GET )
<form action= "/springmvc/user/adduser" method= "POST" ><H>SPRINGMVC annotations
The following error occurred:
Access Touser There is no problem, but when the button is clicked, an error occurs!
Explain:
when we visit: Http://localhost:8080/SpringMVC/user/toUser when the display jquery page, but when we click on jquery button on the page, you Post Submit to AddUser the method, and at this time AddUser the method received as Get , so error. The correct way to do this is to Change the AddUser reception mode to POST
Execute the process in the right way:
First Visit Touser , Arrive jquery page, click jquery button on the page, Post Submit to AddUser , while the AddUser in the result parameter returns
Attention:
direct access to the front page only supports GET way, that is, when our Controller Are received in the form of GET , you can enter the appropriate address in the browser to access the corresponding page
For example:
because at this point AddUser the receive mode is GET, But if we change the way it's received to Post , the following error is reported, because when we access the page directly, the Receive method is Post , and the submission method defaults to GET
we want to Get method request can be reached, Post The method request can also be
Do not want to distinguish so clear, directly to the configuration of the deletion (Method=requestmethod.get)
Spring MVC Annotation Optimization
1 , this can be modified for methods that are to be accessed in the same directory
@RequestMapping ("/user")
2 , delete Method the configuration Method=requestmethod.get
3 , the return value is String type
4 , with parameter use
Request.setattribute ("result", result); Import javax.servlet.http.HttpServletRequest is introduced;
the optimized Controller :
Summary: The way that annotations are applied allows us to reduce the workload of developers by eliminating the need to write so many mapping code in the configuration file. But for us, whether it's annotations or the way we configure files, we need to understand that it's easier for us to understand the way we feel about configuring files.
SPRINGMVC Introductory Study (ii) Application Note method + annotation Optimization