Vi. Annotations of Springmvc
configuration of XML → annotations
1. Create a new configuration file or under Config
New Springannotation-servlet.xml
Web. XML modified to initialize <param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
Springannotation-servlet.xml add a scan package. Scan all classes under a package
<base-package= "Com.tgb.web.controller.annotation"></ Context:component-scan>
Continue adding. The above is the annotation scan package, the following is the open annotation.
Spring WEBMVC under the directory MVC under the annotation of the two classes. Annotations
<!--- <class = " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> <class= " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/>
2, SRC under the Java class
Packagecom.tgb.web.controller.annotation;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.servlet.ModelAndView; @Controller Public classUsercontroller {@RequestMapping (value= "/user/adduser", method=requestmethod.get) PublicModelandview AddUser () {String result= "This is the AddUser function"; return NewModelandview ("/jquery", "result", result); } @RequestMapping (Value= "/user/deluser", method=requestmethod.get) PublicModelandview Deluser () {String result= "This is deluser------"; return NewModelandview ("/jquery", "result", result); } @RequestMapping (Value= "/user/touser", method=requestmethod.get) PublicModelandview Touser () {return NewModelandview ("/jquery"); }}
3, jquery form inside the GET method to correspond to the above method=requestmethod.get. If the form is modified to a POST request, the resource is not requested.
Http://localhost:8080/springMVC1/user/toUser Touser→jquery interface → Click the button → form to/springmvc1/user/adduser This interface, return a line of string
Vii. Sprigmvc Annotation Optimization
1. Configuration Optimization
<class= " Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> < class = " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "></ Bean>
Revision changed to
<!--- <mvc:annotation-driven/>
2. Request Type Optimization
Modified to @requestmapping (value= "/user2")
3, continue to optimize
Modify into @requestmapping ("/user2")
4, modify the Modeandview. Change Modeandview to String
5. Passing parameters
Method is modified to public String AddUser (HttpServletRequest request)
Request.setattribute ("result", result); This passes the data
Viii. SPRINGMVC parameter passing
1. Create a new Datacontroller
Packagecom.tgb.web.controller.annotation;Importjavax.servlet.http.HttpServletRequest;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping, @Controller @requestmapping ("/user/data") Public classDatacontroller {@RequestMapping ("/adduser") Publicstring AddUser (string username,string age,httpservletrequest request) {string result= "This is addUser------Optimized Edition"; Request.setattribute ("UserName", UserName); Request.setattribute ("Age", age); return"/usermanager"; } @RequestMapping ("/deluser") Publicstring Deluser (HttpServletRequest request) {string result= "This is deluser------Optimized Edition"; Request.setattribute ("Result", result); return"/jquery"; } @RequestMapping ("/touser") PublicString Touser (httpservletrequest request) {return"/adduser"; }}
See source code.
With the previous annotations turned on.
http://localhost:8080/springmvc1/user/data/touser→ returns to a adduser.jsp interface, and then forms to the Controller to save the data, Passing data to usermanager.jsp
3, the name garbled in the form.
Adduser.jsp inside for Charset=utf-8 code.
Commit to the server, modify the Tomcat config under the
<port= "8080" protocol= "http/1.1" connectiontimeout = "20000" redirectport= "8443"/>
Revision changed to
<uriecoding= "UTF-8" port= "8080" protocol= "HTTP /1.1 " connectiontimeout=" 20000 " redirectport=" 8443 " />
Get method request, and then modify TOMCT inside the code is also UTF-8. So the name is not garbled.
Post method, still garbled
4. Modify the interception in Web. xml
Add the following "where filter is the Characterencodingfilter under filter under the Web under the Spring Web Jar package"
<Filter> <Filter-name>Encodingfilter</Filter-name> <Filter-class>Org.springframework.web.filter.CharacterEncodingFilter</Filter-class> <Init-param> <Param-name>Encoding</Param-name> <Param-value>UTF-8</Param-value> </Init-param> <Init-param> <Param-name>Forceencoding</Param-name> <Param-value>True</Param-value> </Init-param> </Filter> <!--encoding filter for JSP page - <filter-mapping> <Filter-name>Encodingfilter</Filter-name> <Url-pattern>/*</Url-pattern> </filter-mapping>
SPRINGMVC Study notes Two