Maven creates a simple example of the Spring MVC project

Source: Internet
Author: User
Tags aop jboss log4j

Because it is recommended that you use MAVEN to download the associated spring jar package on the Spring official web site, review how to build Spring MVC with Maven, and learn about spring MVC by the way. (The installation of the M2eclipse plug-in is not described here.) )

First, the new Maven project, as shown in the following figure

1, right key new project

2. You can enter Maven directly in the search


3, I choose here is to use the default project space


4. Select Maven WebApp


5, enter the appropriate project name


Note: The meaning of fields such as group ID can be referenced in this article: http://www.iteye.com/topic/973166

6 Click Finish

Second, add dynamic Web Module

1, click the project, use key combination: ALT + ENTER


2. Choose Project Facets


3, add directory: src/test/resources


4, copy webcontent/web-inf/under the Web.xml to/webmvct/src/main/webapp/web-inf eye

5. Delete webcontent file

6, click the project to use the key combination, open the project option, delete the following items:


7. Add Directory: WebApp and Maven dependenices



Iii. Modification of Web.xml documents

<?xml version= "1.0" encoding= "UTF-8"?> <web-app id= "webapp_id" version= "2.4" xmlns= "Http://java.sun.com/xml" /ns/j2ee "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://java.sun.com/xml/ns/ Java http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <display-name>webmvct</display-name> < Context-param> <param-name>webAppRootKey</param-name> <param-value>webmvct.root</ param-value> </context-param> <context-param> <param-name>log4jconfiglocation</param-name > <param-value>WEB-INF/log4j.properties</param-value> </context-param> <listener> <l Istener-class>org.springframework.web.util.log4jconfiglistener</listener-class> </listener> <
	Listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>webmvct</sErvlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name> Webmvct</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> < welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app&
	    
 Gt

Add files under the Web-inf directory: project name-servlet.xml

(Web-inf/webmvct-servlet.xml)

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http:// Www.springframework.org/schema/context/spring-context-2.5.xsd Http://www.springframework.org/schema/jee http:// Www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/tx http:// Www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <!--scan all classes in the package to complete the bean creation and automatic dependency injection capabilities The package will be created later: Webmvct.cmd--> <context:component-scan base-package= "Webmvct.cmd"/> <!--definition SPring MVC template file--> <bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <!--support Jstl--> <property name=" prefix "value="/we  b-inf/jsp/"/> <!--template path--> <property name=" suffix "value=". jsp "/> <!--
 Suffix of template file--> </bean> </beans>

V. Modification of Pom.xml

You can go to the URL to query the corresponding spring package: http://repo.spring.io/webapp/browserepo.html?0

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.learn.cm</groupId> <artifactid>webmvct</ artifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name> WEBMVCT Maven webapp</name> <url>http://maven.apache.org</url> <dependencies> < dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8 .1</version> <scope>test</scope> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPR Ingframework</groupid> <artifactId>spring</artifactId> <version>2.5.6</version> ;/dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK&Lt;/groupid> <artifactId>spring-webmvc</artifactId> <version>2.5.6</version> </depen dency> <dependency> <groupId>org.mortbay.jetty</groupId> &LT;ARTIFACTID&GT;SERVLET-API-2.5&L t;/artifactid> <version>6.0.1</version> </dependency> <dependency> <groupid>ja vax.servlet</groupid> <artifactId>jstl</artifactId> <version>1.1.2</version> </d
			ependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> </dependency> </dependencies> <build> <finalname>we
 Bmvct</finalname> </build> </project>

Create a new class in the Src/main/java folder Webmvctcontroller


Create a new folder JSP in the Web-inf directory and create a new login.jsp in the JSP folder

<!    
    DOCTYPE html>  
Eight, at the same time new file: business.jsp

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Create the log4j profile in the Web-inf directory:

Log4j.properties

# for Jboss:avoid to setup log4j outside $JBOSS _home/server/default/deploy/log4j.xml!
# for all other servers:comment out of the log4j listener in Web.xml to activate log4j.
Log4j.rootlogger=debug, stdout

log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n

X. Add files to the Web-inf folder Applicationcontext.xml

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" x mlns:tx= "Http://www.springframework.org/schema/tx" xmlns:context= "Http://www.springframework.org/schema/context "Xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springframework.org/sche Ma/beans/spring-beans-2.5.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework. Org/schema/aop/spring-aop-2.5.xsd Http://www.springframework.org/schema/tx Http://www.springframewo Rk.org/schema/tx/spring-tx-2.5.xsd Http://www.springframework.org/schema/context HTTP://WWW.SPRINGF Ramework.org/schema/context/spring-context-2.5.xsd "> <!--the parent application context Definit Ion for the Springapp APPLIcation--> <!--<bean class= "
 Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>--> </beans>

Xi. Modification of index.jsp documents

<%
   request.getrequestdispatcher ("/web-inf/jsp/login.jsp"). Forward (request,response);
 %>

12, release, start the project

Enter in Browser: http://localhost:8080/webmvct/




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.