您的位置:首页 > 编程语言 > Java开发

搭建maven+spring+freemaker+mybatis环境之二

2015-11-14 19:10 465 查看
这里是搭建maven+spring+freemaker+mybatis环境的第二部分,maven+springmvc

一、

修改web.xml的内容为

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>Archetype Created Web Application</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>

<filter>
<filter-name>encodingFilter</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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</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>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>

<!-- http 请求分发 -->
<servlet>
<servlet-name>webmvct</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>webmvct</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<!-- 配置session超时时间,单位分钟 -->
<session-config>
<session-timeout>15</session-timeout>
</session-config>

<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>


由最初的web.xml到上面的内容,需要注意的几个问题,

1.修改xml的xmlns 和xsi的版本,因为之前遇到了内部标签顺序颠倒会报错的问题

2.先有ContextLoaderListener,再有DispatcherServlet,而且都有初始化参数,第一个类如果没有初始化参数会报application.xml找不到的错误,他们具体作用,参见网上文档

3.在项目骨架的src/main/resources 文件夹下准备好web.xml里面用到的配置文件log4j.properties,spring.xml,spring-servlet.xml

4.把WEB-INF下的index.jsp换成了index.html

springxml的代码:

<?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" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=" 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
">

</beans>
spring-servlet.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:p="http://www.springframework.org/schema/p"
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:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation=" 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 http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd
">

<!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
<context:component-scan base-package="com.template" />
<!-- 默认的注解映射的支持 -->
<mvc:annotation-driven />

</beans>


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.template.cn</groupId>
<artifactId>template</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>template 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>
<!-- spring相关包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<finalName>template</finalName>
</build>
</project>


log4j.properties的代码,网上找的稍微有改动

#root directory for log files
dir=d://Logs
#log file for online
file00=error.log
#log file for batch
file01=debug.log
#log file size
fileSize=10000KB
#back up numbers for log file
backup=10
###########################default log level and log appender###########################
log4j.rootLogger=DEBUG,CONSOLE,SYSFILE
log4j.logger.platform_debug=DEBUG,DEBUGFILE
log4j.logger.platform_error=ERROR,ERRORFILE
log4j.logger.platform_info=INFO,INFOFILE
########################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%t] method:%l%n    %m%n
########################################################################################
log4j.appender.DEBUGFILE=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.DEBUGFILE.file=\\temp_space\\logs\\platform-debug.log
log4j.appender.DEBUGFILE.file=${dir}/debug.log
log4j.appender.DEBUGFILE.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.DEBUGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.DEBUGFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%t] method:%l%n    %m%n
########################################################################################
log4j.appender.ERRORFILE=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.ERRORFILE.file=\\temp_space\\logs\\platform-error.log
log4j.appender.ERRORFILE.file=/data/logs/platform-error.log
log4j.appender.ERRORFILE.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.ERRORFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ERRORFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%t] method:%l%n    %m%n
########################################################################################
log4j.appender.SYSFILE=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.SYSFILE.file=\\temp_space\\logs\\platform-sys.log
log4j.appender.SYSFILE.file=/data/logs/platform-sys.log
log4j.appender.SYSFILE.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.SYSFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.SYSFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%t] method:%l%n    %m%n
########################################################################################
log4j.appender.INFOFILE=org.apache.log4j.DailyRollingFileAppender
#log4j.appender.INFOFILE.file=\\temp_space\\logs\\platform-info.log
log4j.appender.INFOFILE.file=/data/logs/platform-info.log
log4j.appender.INFOFILE.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.INFOFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.INFOFILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%t] method:%l%n    %m%n
########################################################################################


最后启动,看是否能正常启动
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: