ArticleDirectory
-
- 1. directly create a Maven Project Method
First, install the maven plug-in eclipse at http://m2eclipse.sonatype.org/sites/m2e.
Installing the maven plug-in using myeclipse has some problems with the maven project. One is that resources will not be released to Tomcat when Tomcat is published; the other is to change the classes under the WEB-INF to the classes under the target, but I don't know why myeclipse either still generates class under the WEB-INF. Either it is not really the WEB-INF to generate classes but when Tomcat is released, the class file will not be released to you in the past, super depressing. However, after you build a Maven project using eclipse, you can use myeclipse to open the project.
Advantages of using Maven: I feel that the main thing is to automatically download the jar package and the package it depends on. This ensures that the jar version is different when many developers develop. In addition, the file structure is clear, and java files, resource files, and test files are clearly divided.
Two methods are introduced: 1. directly create a Maven project method; 2. Create a dynamic web project and convert it to a Maven project method.
1. Create a Maven project directly. 1. Create a Maven project.
Next we will use maven of eclipse to build a web project. The springmvc project will be constructed as an example:
1.1 select to create a Maven Project
Select File-> New-> other, and select Maven-> Maven project in the new window. Click newxt.
1.2 select the project path
Use default workspace location default workspace.
1.3 select a project type
Select Maven-Archetype-webapp from artifact ID
1.4 enter the group ID, artifact ID, and Package
Group ID is generally the name of a large project. Artifact ID is the sub-project name.
For example, spring web package, group ID: org. springframework, and artifactid: Spring-web.
Package is a default package. You can also create a package without writing it.
1.5 The file structure after the creation is as follows:
If many contents are displayed here, it is generally a problem with filters settings. Or perspective is the javaee mode. You can change it to the java mode.
2. Configure the maven Project
Next, you need to change a lot of configurations.
2.1 Add the source folder
Next, add the src/main/Java, src/test/Java, and src/test/resources folders. Right-click the project root directory and choose new> source folder,
Create these three folders. Note that it is not a normal folder, but a source folder.
2.2 change the class path
Right-click the project and choose Java build path> source.
There should be four folders below. Src/main/Java, src/main/resources, src/test/Java, src/test/resources.
Double-click the Output Folder of each folder and select the path.
Src/main/Java, src/main/resources, and select target/classes;
Src/test/Java, src/test/resources, and select target/test-classes;
Select allow output folders for source folders.
2.3 right-click dynamic web project 2.3.1, select project facets, and click convert to faceted from.
2.3.2 configure project facets
Select Java and version 1.6, and select Dynamic Web module version 2.5. (3.0 for Java 7 ).
If an error occurs, you may need to set the level of compiler compliance to 1.6 in Java compiler. Or you need to change the Java version in this window to 1.6.
In addition, if the error cannot ..... Dynamic Web module to 2.5
Open the org. Eclipse. WST. Common. Project. facet. Core. xml file in workspace.
Modify Java version 1.6, JST. Web Version 2.5, save and restart eclipse
<? XML version ="1.0"Encoding =UTF-8"?> <Faceted-Project> <FixedFacet ="WST. jsdt. Web"/> <Installed facet ="Java"Version ="1.6"/> <Installed facet ="JST. Web"Version ="2.5"/> <Installed facet ="WST. jsdt. Web"Version ="1.0"/> </Faceted-Project>
2.3.3 configure the modify faceted Project
Click further configuration available ..., The modify faceted project window is displayed.
Here is the path for setting the Web. xml file. We enter src/main/webapp.
Generate Web. xml deployment descriptor automatically generates the Web. xml file. Optional.
2.4 set deployment Program Set (Web deployment Assembly)
After completing the preceding settings, click OK. The Properties window is closed. Right-click the project to open this window. A deployment Assembly will appear in the list on the left. Click it, for example:
The list here shows the path of the file to be published when a project is deployed.
1. we delete the two items of test because test is used for testing and does not need to be deployed.
2. Set to publish the jar package of Maven to Lib.
Add-> JAVA build path entries-> Maven dependencies-> finish
Set
3. Add a jar package to the maven Project
Maven can manage jar packages that the project depends on. You can use groupid, artifactid, and the version number to uniquely identify a jar package. This prevents inconsistent jar packages under WEB-INF/lib in older web projects. In addition, Maven automatically downloads the jar package on which the added jar package depends.
3.1 Add the required jar package to Pom. xml
Use Maven pom editor to open the POM in the project. in the XML file, select dependencies and click Add in the dependencies column. First, a search button is displayed. For example, if you enter spring-web, the jar package related to spring-web is automatically searched, we chose spring 3.0.5. Add all spring packages. Other jar packages to be added include JUnit and jstl. Or click Pom. XML to edit the Pom. xml file. In this way, the dependencies content can be directly copied.
3.2 set the scope of the jar package
After a jar package is added, some attributes need to be set. The most important attribute is scope, which has the following values:
1. Compile, default value, applicable to all stages and will be released along with the project.
2. Provided, similar to compile, expects JDK, container, or user to provide this dependency. For example, Servlet. jar.
3. runtime, used only at runtime, such as the JDBC driver, applicable to the running and testing stages.
4. Test, used only during testing, for compiling and running testsCode. Will not be released along with the project.
5. system, similar to provided, must explicitly provide the jar containing dependencies. MAVEN will not find it in repository.
Generally, the springmvc project needs to configure the scope jar package, such:
Sometimes it is found that the servlet-API is still packaged under Lib, and an error will certainly be reported at this time. You need to install WTP in the maven plug-in.
Eclipse installation path: http://m2eclipse.sonatype.org/sites/m2e-extras. Select for eclipse WTP.
4. Construct springmvc framework 4.1 and edit the Web. xml file
You need to add log4j, character filtering, spring dispatcher, and so on.
The webx. XML Code is as follows:
XML Code
<? XML version = "1.0" Encoding =UTF-8" ?> <Web-app xmlns = Http://java.sun.com/xml/ns/javaee" Xmlns: xsi = Http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" Version = "2.5" > <! -- Differentiate project names to prevent repeated names by default --> <context-param> <param-Name> webapprootkey </param-Name> <param- Value > Maven. example. Root </param- Value > </Context-param> <! -- Spring log4j listener --> <listener-Class > Org. springframework. Web. util. log4jconfiglistener </listener- Class > </Listener> <! -- Character Set filter --> <filter-Name> characterencodingfilter </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> <param-Name> forceencoding </param-Name> <param- Value >True </Param- Value > </Init-param> </filter> <filter-mapping> <filter-Name> characterencodingfilter </filter-Name> <URL-pattern>/* </url-Pattern> </filter-mapping> <! -- Spring view distributor --> <servlet-Name> dispatcher </servlet-Name> <servlet- Class > Org. springframework. Web. servlet. dispatcherservlet </servlet- Class > <Init-param> <param-Name> contextconfiglocation </param-Name> <param- Value >/WEB-INF/dispatcher-servlet.xml </param- Value > </Init-param> <load-on-startup> 1 </load-on-startup> </servlet> <servlet-mapping> <servlet-Name> dispatcher </ servlet-Name> <URL-pattern> *. Do </Url-pattern> </servlet-mapping> </Web-app>
4.2 compile spring configuration file dispatcher-servlet.xml
For example, you need to add the MVC driver, annotation detection, and view parsing. The dispatcher-servlet.xml code is as follows:
XML Code
<? XML version = "1.0" Encoding = UTF-8" ?> <Beans xmlns = Http://www.springframework.org/schema/beans" Xmlns: AOP = Http://www.springframework.org/schema/aop" Xmlns: context = Http://www.springframework.org/schema/context" Xmlns: MVC = Http://www.springframework.org/schema/mvc" Xmlns: Tx = Http://www.springframework.org/schema/tx" Xmlns: xsi = Http://www.w3.org/2001/XMLSchema-instance" Xsi: schemalocation = Http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" > <MVC: annotation-driven/> <context: component-Scan Base -Package = "Com. xujj" /> <Bean Class ="Org. springframework. Web. servlet. View. internalresourceviewresolver" > <Property name = "Prefix" Value = /WEB-INF/views /" /> <Property name = "Suffix" Value = ". Jsp" /> </Bean> </beans>
4.3 compile a controller-layer test class
Compile a springmvc controller layer test class. This class only has one method for address ing and transmits one data to the page. The Code is as follows:
Java code
package COM. xujj. springweb. CTRL; import Org. springframework. stereotype. controller; import Org. springframework. UI. model; import Org. springframework. web. BIND. annotation. requestmapping; @ controller Public class generalcontroller {@ requestmapping ( value = "index. do ") Public void index (Model) {model. addattribute ( "xujj" , "Hello World" ); system. out . println ( "" ) ;}
4.4 compile the index. jsp page
First, create the folder views under src/main/webapp/WEB-INF. This is the same as the prefix property path in the dispatcher-servlet.xml configuration file.
Create an index. jsp file under views
We use jstl to obtain the data of controlleradd.
The JSP page code is as follows:
HTML code
<% @ page Language = "Java" contenttype = " text/html; charset = UTF-8 " pageencoding = " UTF-8 " %> <% @ taglib prefix = "C" uri = "http://java.sun.com/jsp/jstl/core" %>
"-// W3C // dtd html 4.01 transitional/EN" "http://www.w3.org/TR/html4/loose.dtd "
" Content-Type " content = "text/html; charset = UTF-8 " insert title here
out
value =
"$ {xujj}} "
out
5. Publish to Tomcat
This is nothing to say.
Tomcat common settings under Eclipse: http://limingnihao.iteye.com/admin/blogs/825394
6. Test
Access address: http: // localhost: 8080/springweb/
from: http://limingnihao.iteye.com/blog/830409