SI series of hands-on hands to build the Spring MVC project
The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.
SI before the article, Spring MVC + MyBatis abbreviation, because spring MVC internal has injected function, so do not consider whether it is using spring (with the foot Fork think also take advantage of spring, but think too much, easy to be struck by lightning, then when there is no good, that is, Sp Ring MVC Self-injected function).
The overall process should be:
I. Web. XML configuration
The Java Web container finds a Servlet that requests a response through Web application XML.
1, the Java Web container receives the request, according to the servlet and matching pattern configured in Web. XML, find the corresponding servlet and forward the request to the servlet processing;
2. We configure the Spring MVC Distributor to handle all expected request matching patterns, and the complete configuration of Web. XML is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_3_0.xsd "id=" webapp_id "version=" 3.0 "><display-name>testsi</ Display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file >index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file> Default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file> default.jsp</welcome-file></welcome-file-list><servlet><servlet-name>testsi</ Servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param><!--and Spring Specify the context profile location--><param-name>contextconfiglocation</param-name> <param-value>classpath:spring-contRoller.xml, classpath:testsi-servlet.xml</param-value></init-param><load-on-startup>1</ load-on-startup></servlet><servlet-mapping><servlet-name>testsi</servlet-name>< Url-pattern>/*</url-pattern></servlet-mapping></web-app>
The above completed the Java Web part of the configuration, request someone to deal with, Bob, the container of decentralization, love who will make Teng teng, it can not love tube this nosy son, next will have to Spring MVC to make up!
3, avoid the lily, the spring listener to match, and then in spring's global context configuration file configuration to inject the bean, the result of spring MVC is not used to inject beans, but self-created; instead, the spring configuration file should be configured to SPR ing MVC, Spring MVC implements injection based on the configuration and finds the Bean to be used from the injection function.
Second, testsi-servlet.xml configuration
The Testsi here is based on the <servlet-name/> defined by the Spring MVC dispatcher Dispatcherservlet in Web. XML, which is the name +-servlet.xml,DISPATC Herservlet after accepting the request, look for the configuration file to determine in which class package the controller class is searched, and of course these classes require note-driven:
Testsi-servlet.xml The complete available configuration is as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" xsi: schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/sche Ma/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/context Http://www.springfra Mework.org/schema/context/spring-context-3.1.xsd Http://www.springframework.org/schema/mvc http:/ /www.springframework.org/schema/mvc/spring-mvc-3.1.xsd "><!--scan the Controller's root package--><context: Component-scan base-package= "Com.testsi.controller"/><!--shorthand configuration Note Driver--><mvc:annotation-driven/>< !--Controller returns a string match page--><beanclass= "Org.springframework.web.servlet.view.InternalResourceViewresolver "><property name=" prefix "value="/web-inf/view/"></property><property name=" suffix " Value= ". JSP" ></property></bean></beans>
The above configuration,Dispatcherservlet will go to the Com.testsi.controller bag to find the controller class in the method of matching, of course, this should be annotated, any kind of the liner@Controller, can become the controller class, isDispatcherservlet Search, specifically file parsing or how, I have not studied, that is the relevant content of the annotations, interested in the follow-up study;
specifically to which method the request is assigned to which controller, it depends on the annotations before the method, simply,
@RequestMapping (Value = "", method = Requestmethod. GET)
This line, can match any content? About @RequestMapping This annotation is the key to matching the request to the next station, which needs to be studied in depth.
As configured in the above Web. XML, thetestsi-servlet.xml configuration file location under the Classpath root package, we build a directory of any name under Java Resources and SRC, it will be the same as SRC, and eventually The contents of the groupings under these Java Resources will be copied to the Web-inf/classes directory by directory structure:
Of course,. Java class source files must be compiled into a. class to be copied into the new classes directory in the Web-inf in the original directory structure, along with the contents of other directories of the SRC sibling.
class, instead of storing the corresponding. class file in the directory hierarchy.
Of course, these configuration files or some resource files, can also be placed under the class root package in any one level package, or under SRC, and COM peer, SRC represents the root package, so that in the configuration file as long as the use of classpath: to qualify it, of course, there is a usage classpath* : This is to indicate that such a structure under any sub-hierarchy under the root package can match.
In Java code, the GetResource ("/") of the class loader can be used to obtain the disk path where the root package resides, or getresource ("") can obtain the package path of the current class, and there are many usages that can achieve the purpose of locating the class resource through the classpath. Removal of a hard-coded path poses a migration hazard.
Three, according to the above two-step planning, practical details
The above description is enough, fine enough, let's see, there are things that need to be done:
1, that is, the path of the two configuration files in Web. XML, then build a directory as shown above, and then separate the two configuration files by name and put it in that position;
2, the above testsi-servlet.xml content copy paste in, which needs to build a view subdirectory under Web-inf, and in which a index.jsp file is created;
3, the Controller search class package set up Com.testsi.controller, which is built under the SRC;
4, under the Com.testsi.controller set up Indexcontroller class, and then enter @Controller annotations on a single line of the class;
@Controllerpublic class Indexcontroller { }
5. Establish the method indexin the Indexcontroller class , and add @RequestMapping annotation on the method line:
@Controllerpublic class Indexcontroller {@RequestMapping (value = "", method = requestmethod.get) Public String Index ( HttpServletRequest request) throws Exception {return "index";}}
After the save, clean and automatically compiled, the corresponding error prompts, prompting the lack of the corresponding class library, then we will gathered up the required class library.
Four, the simplest Spring MVC required class Library
1. First, these libraries can be downloaded from mvnrepository.com, the advantages of this MAVEN repository:
One is the ability to search the necessary Jar packages by name,
The second is that each jar package relies on a package that also provides lists and download links as well as the applicable version of the jar package.
2, we know in advance to use the Dispatcherservlet, but pay attention to its all-inclusive structure: Org.springframework.web.servlet.DispatcherServlet, the beginning of handwriting down, unexpectedly put org into COM, This dispatcherservlet is not found for Tomcat 6 boot error .
Then we need to download is springframework in the SPRING-WEBMVC, in fact, these I also spent a little bit of time to find, a little bit to add, and not with others out of the box, is to know what is the simplest, from the actual measurement, memory and understanding is more profound.
In these 7 Jar packages, i first searched the Spring-webmvc and found the 3.2.9 version, and found that the direct search springframework will find all the relevant categories.
SPRING-WEBMVC is configured in Web. XML, Spring-context and Spring-web are error hints that occur when adding annotations and parameters to classes and methods, and are found as prompted;
The other four is exactly every time you run a prompt to find the class, as prompted to search and download down, together with the simplest of the 7 package.
However, there is still a problem, returning the "index" string in the controller, prompting the relevant warning in the console and not displaying the index.jsp page:
Warning: No mapping found for HTTP request with URI [/testsi/web-inf/view/index.jsp] in Dispatcherservlet with name ' Testsi '
The problem is the set surface of the matching model for Dispatcherservlet in Web. xml:
<servlet-mapping><servlet-name>testsi</servlet-name><url-pattern>/*</url-pattern ></servlet-mapping>
"/*", all requests will be forwarded toDispatcherservlet;
"/" will only be forwarded to Dispatcherservlet for the host/servlet of this form of request;
This explanation, I have not made too clear, interested can be here.
Change, and then run it, categorization malleability son no problem, there is a problem, that is not thought of the problem, welcome to Huitie.
Follow-up will then inject today's tuned Spring MVC into the MyBatis Mapper, then do the above project, including the Jar package and other details, from the beginning.
See you next weekend!
Project code has been packaged, be free study CSDN upload, share to everyone.
SI series of hands-on hands to build the Spring MVC project