请先参考:http://limingnihao.iteye.com/blog/830409 构建好maven web项目。
maven未安装,请先访问 http://www.cnblogs.com/quanyongan/archive/2013/04/17/3025971.html 安装
maven仓库的话请参考http://blog.csdn.net/zhaotengfei36520/article/details/41041111
http://blog.csdn.net/joewolf/article/details/4876604
结构如下图:
Maven仓库配置
maven settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <!-- localRepository | The path to the local repository maven will use to store artifacts. | | Default: ${user.home}/.m2/repository <localRepository>/path/to/local/repo</localRepository> --> <localRepository>G:/core/repository</localRepository> <mirrors> <!-- mirror | Specifies a repository mirror site to use instead of a given repository. The repository that | this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used | for inheritance and direct lookup purposes, and must be unique across the set of mirrors. | <mirror> <id>mirrorId</id> <mirrorOf>repositoryId</mirrorOf> <name>Human Readable Name for this Mirror.</name> <url>http://my.repository.com/repo/path</url> </mirror> --> <mirror> <id>maven-net-cn</id> <name>Maven China Mirror</name> <url>http://maven.net.cn/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> </mirrors> ..... .....
pom.xml
<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.lenggirl</groupId> <!-- 项目名 --> <artifactId>hunterhug</artifactId> <!--构建名--> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>hunterhug Maven Webapp</name> <url>http://maven.apache.org</url> <!-- spring依赖(只有这么多) --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.5.RELEASE</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-servlet_2.5_spec</artifactId> <version>1.2</version> </dependency> </dependencies> <!-- 远程仓库 --> <repositories> <repository> <id>maven-net-cn</id> <name>Maven China Mirror</name> <url>http://maven.net.cn/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>maven-net-cn</id> <name>Maven China Mirror</name> <url>http://maven.net.cn/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> <build> <finalName>hunterhug</finalName> </build> </project>
配置两次仓库是由于会先从项目里配置的远程仓库下载jar包到本地仓库,如果远程仓库没有会去中央仓库里找,而本地仓库和中央仓库都在settings.xml里配置的,一个是全局文件,一个是局部的。
因为一个jar会依赖另外一个jar,所以有些包会连带被下载,多方便啊。
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app 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/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <!-- 该元素用来声明应用范围(整个WEB项目)内的上下文初始化参数。 --> <!-- 区分项目名称,防止默认重名 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>hunterhug</param-value> </context-param> <!-- spring配置文件所在位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/spring.xml</param-value> </context-param> <!-- 日志配置所在位置--> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/log/log4j.properties</param-value> </context-param> <!--开一条watchdog线程每6秒扫描一下配置文件的变化 --> <context-param> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <!-- 字符集 过滤器 --> <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> <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> <!-- 日志监听器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- spring各种配置加载 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring分发器 --> <servlet> <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/spring/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> <!-- 欢迎页 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
log4j.properties
layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n log.file=${message.web.root}/WEB-INF/log/app.log log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.File=${log.file} log4j.appender.logfile.Append=true log4j.appender.logfile.DatePattern=‘.‘yyyyMMdd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=${layoutPattern} layoutPattern=[%d{HH:mm:ss}] %-5p : %m%n log.file=${message.web.root}/WEB-INF/log/app.log log4j.appender.logfile=org.apache.log4j.DailyRollingFileAppender log4j.appender.logfile.File=${log.file} log4j.appender.logfile.Append=true log4j.appender.logfile.DatePattern=‘.‘yyyyMMdd log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=${layoutPattern}
dispatcher-servlet.xml
<?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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://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.lenggirl.hunterhug" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
<context-param>参考http://blog.csdn.net/jubincn/article/details/9115205
log4日志监听器请参考
http://blog.csdn.net/wxwzy738/article/details/18620421
http://blog.csdn.net/coomon2000/article/details/6782211
ContextLoaderListener监听器请参考
http://blog.csdn.net/seng3018/article/details/6758860
spring mvc配置参考http://www.cnblogs.com/superjt/p/3309255.html
不使用其他struts技术,那么spring.xml可以不用设置,参考http://blog.csdn.net/zb0567/article/details/7930642