Maven+eclipse+tomcat Configuration Process Logging

Source: Internet
Author: User
Tags aop character set response code tomcat log4j

First, set up MAVEN's setting file location in Eclipse, point to the MAVEN directory you downloaded, and the warehouse will be generated automatically.


First, create a MAVEN project in Eclipse and take note of several items:

Select an item with the artifact ID of Maven-archetype-webapp


II. Create a directory structure: Project right: New-source folder, create the following directories

1, Src/main/java

2, Src/test/java

3, Src/test/resources


Third, set the class output directory of Java,resources directory, Project right: Build Path-source, double-click the Source Folder, select the output directory in the pop-up box.

Also select Allow output folders for source folders.



Iv. turn the project into a Dynamic Web project right-click Project, select Project Facets, and tap convert to faceted from

Configure Project Facets

Select Java and version 1.6, tick the version of Dynamic Web module to 2.5. (3.0 for Java7).

If you are prompted with an error, you may need to set compiler compliance level to 1.6 in Java compiler.           Or you need to change the version of Java in this window to 1.6. Another: If the error cannot ..... Dynamic Web Module to 2.5

Open the Org.eclipse.wst.common.project.facet.core.xml file under the. Setting folder in the project root directory to manually modify the version

<faceted-project>
<fixed facet= "Wst.jsdt.web"/>
<installed facet= "java" version= "1.6"/>< compiler version 1.6, running JDK versions can not be lower than the compiled version >
<installed facet= "Jst.web" version= "2.5"/><servlet2.5 Edition >
<installed facet= "Wst.jsdt.web" version= "1.0"/>
</faceted-project>

------------------------------------------------The following 4 lines can be ignored configuration Modify faceted Project

Click further configuration available ..., pop-up modify faceted Project window

Here is the path to set the Web. xml file, we enter Src/main/webapp.

Generate Web. XML Deployment descriptor automatically generates the Web. xml file, optional. --------------------------------------------------------------

V. Setting the directory location for file publishing when a project is deployed

Open this window in the right-click Project. A deployment Assembly appears in the list on the left, click to enter, as shown below:

1, two items of test need to be removed because test is used for testing and does not need to be deployed.

2, set the Maven jar package to be published under Lib.

ADD, Java Build Path Entries, Maven Dependencies, Finish

Effect diagram after Setup is complete


Vi. adding a jar package to a MAVEN project

6.1 Add the required jar packages to the Pom.xml

Using Maven POM Editor to open the Pom.xml file in the project, select Dependencies, click Add in the Dependencies column, first pop up a search button, such as input spring-web, will automatically search for spring-web related jar packages, we select the 3.0.5 version of spring. Add the spring package all in. Additional jar packages that need to be added are: JUnit, Jstl. or click Pom.xml to edit the Pom.xml file directly. This can be copied directly to dependencies content.

6.2 Setting the scope of the JAR package

When added into a jar package, there are some properties that need to be set, and most importantly, scope, which has the following values:

1. Compile, the default value, applies to all stages and is published along with the project.

2. Provided, similar to compile, expects that the JDK, container or user will provide this dependency. such as Servlet.jar.

3. Runtime, used only at runtime, such as JDBC driver, for run and test phases.

4. Test, used only during testing, to compile and run the test code. Will not be published with the project.

5. System, similar to provided, needs to explicitly provide an jar,maven that contains dependencies and does not look for it in repository.

The jar package that normally springmvc the scope of the project needs to be configured as shown below:

Sometimes found that Servlet-api is still packaged under the Lib, at this time will definitely error. You will need to install the WTP in the Maven plugin as well.

Eclipse Online Installation path: Http://m2eclipse.sonatype.org/sites/m2e-extras. Select for Eclipse WTP.

-------------------------------------------------------------------------------------------------the following parts of the spring framework can be ignored VII. Construction of SPRINGMVC framework

7.1 Editing the Web. xml file

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= "HT 
                             Tp://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 "> <context-param>

    <param-name></param-name> <param-value></param-value> </context-param> <!--spring log4j listener--<listener> &LT;LISTENER-CLASS&GT;ORG.SPRINGFRAMEWORK.WEB.UTIL.LOG4JC Onfiglistener</listener-class> </listener> <!--character Set filter--<filter> <fil Ter-name>characterencodingfilter</filter-name> <filter-class> Org.springframework.web.filter.characterencodingfilter</filter-class> <init-param> <par Am-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>for Ceencoding</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> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.di Spatcherservlet</servlet-class> <init-param> <param-name>contextconfiglocation</p Aram-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> &LT;/INIT-PARAM&G
        T <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name >dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 
7.2 Writing the spring configuration file Dispatcher-servlet.xml

To add MVC drivers, annotation detection, view resolution, and more. 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:a
       op= "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.xsd http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SC Hema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframe WORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframew 
       Ork.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd "> <mvc:annotation-driven/> <context:comp Onent-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>

-------------------------------------------------------------------------------------------------above is an ignorable part
Viii. Maven integrates tomcat to support hot deployment

1. Create an administrative user and password in conf tomcat_users.xml in Tomcat, General Manager-script, use text in the Tomcat plugin in Pom.xml, or 403 error may occur

<tomcat-users>
<role rolename= "Manager-script"/>
<user username= "admin" password= "password" roles= "Manager-script"/>
</tomcat-users>

Tomcat role changed:
Note that for Tomcat 7 onwards, the roles required to use the manager application were changed from the single manager Rol E to the following four roles. You'll need to assign the role (s) required for the functionality you wish to access.
Manager-gui-allows access to the HTML GUI and the status pages
Manager-script-allows access to the text interface and the status pages
Manager-jmx-allows access to the JMX proxy and the status pages
Manager-status-allows access to the status pages only
Just use Manager-gui role.

2, start Tomcat, then Access http://localhost:8080/manager/html, enter Admin/password, if the Tomcat default interface, indicates that tomcat all OK, and then you can continue to follow the operation.

3. Configure the Tomcat plugin in the project based on the build in the Pom.xml file in the directory

<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<url>http://localhost:8080/manager/text</url>

<!--

2, Deploy:eg Http://localhost:8080/manager/deploy?path=/phoenix&war=&update=true
But the deploy URL is does not find.
The warning is as follows:
The page tried to access (/manager/deploy) is does not exist.
The Manager application has been re-structured for Tomcat 7 onwards and some of URLs having changed. All URLs used to access the Manager application should now start with one of the following options:
/manager/html for the HTML GUI
/manager/text for the text interface
/manager/jmxproxy for the JMX proxy
/manager/status for the status pages

-
<username>admin</username>
<password>admin</password>
<path>/baseinfo</path>
</configuration>
</plugin>
</plugins>

1, the following error occurs when executing Tomcat:deploy, because the user name or password configured in Tomcat-users is inconsistent with pom.xml

Failed to execute goal org.codehaus.mojo:tomcat-maven-plugin:1.1:deploy (DEFAULT-CLI) on Project Webs:cannot invoke TOMC At Manager:server returned HTTP response code:401 for url:http://localhost:8080/manager/text/deploy?path=%2fwebs& War=, [Help 1]

Restart Tomcat to execute the command again

2, must start Tomcat and then execute Tomcat:deploy or Tomcat:redeploy, otherwise the connection is rejected error message

Cannot invoke Tomcat manager:connection Refused:connect

3, 403 error is due to Tomcat's role setting problem, modify the role name can

Open the Tomcat WebApps directory, and the project is deployed to the folder, and the browser opens the page to test successfully.


Record the relevant plug-ins and knowledge involved in the configuration:

1, mention the use of the Maven-war-plugin plug-in to generate the war, but do not use the plug-in in Tomcat also generated a project war package,

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingExcludes>WEB-INF/web.xml</packagingExcludes>
</configuration>
</plugin>

2, the first project in the Pom.xml configuration of the following nodes, do not know whether it can be omitted

<repositories>
<repository>
<id>people.apache.snapshots</id>
<url> Http://repository.apache.org/content/groups/snapshots-group/</url>
<releases>
<enabled >false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginrepositories>
< Pluginrepository>  
   <id>apache.snapshots</id>  
   <name >apache snapshots</name>  
   <url>  
       http:// repository.apache.org/content/groups/snapshots-group/ 

Related Article

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.