Maven可以翻译为专家或行家,程序员用Maven大多用于构建,他不可以帮我们自动化构建过程,从清理到编译,从测试到生成报告,在到打包和部署,都能帮我们完成。
之前自己也未使用过Maven进行项目构建,这里总结下Maven的下载和安装的过程,为自己做个记录,也分享下。
下载Maven地址
官方下载:http://maven.apache.org/download.cgi
当然,也可以在CSDN进行下载,地址百度下很多。
这里总结的是JAVA使用的安装步骤,首先必须安装好JDK,并且配置好环境变量,这里JDK的安装步骤就详细叙述了,教程很多。
Maven安装
下载下来后是一个压缩包,直接解压到需要安装的盘符就可以了,当然也可以用个文件夹包裹起来。
image.png
解压后放在需要安装的盘符,也不叫安装,直接解压放在盘里就可以了
image.png
环境变量配置
解压好后还需要对环境变量进行配置,和配置JDK的步骤一样
首先需要建立系统环境变量,变量名就为maven_home,变量值就为解压的地址
image.png
然后再配置path,在path末尾加上%安装地址%\bin
image.png
最后保存配置就完成了安装
验证是否安装成功
打开CMD输入命令:mvn -v 如果出现如下信息,就安装成功了
image.png
安装成功后可以对本地仓路径进行配置,系统默认的路径都不太好找,自己配一个路径也方便管理。例如你需要在D盘创建你的仓库,先在D盘建立文件夹,然后打开本地存放maven目录 例如:D:\Java\apache-maven-3.5.0,打开conf文件夹下的settings.xml文件,找到第53行,把注释去掉,修改成:<localRepository>D:\XXXX(文件夹名)</localRepository>
再次打开CMD,输入:mvn help:system ,如果运行的过程中没有错误,打开我们仓库(D:\MavenRepository)会发现里面多了一些文件。这些文件就是我们从maven的中央仓库下载到本地仓库的。看网速,下载可以需要点时间,这个时候我们去配置其他的。
Myeclipse中进行配置
Myeclipse中集成了系统自带的maven,这里我们使用刚刚自己配置的maven,首先打开Myeclipse-->Windows-->Preferences然后搜索maven
image.png然后在配置刚刚自己安装的xml文件,可以手工修改保存路径,这里就不做修改了
image.png现在我们来搭建一个完整的整合了Spirng+SpirngMVC+Mybatis的项目
右键-->new-->other-->搜索maven-->选择maven project 默认就好,不要选择Create a simple project 选项,这么我们就能使用Maven Arche-type。然后选择maven-archetype-webapp。
image.png
不要勾选Create a simple project
image.png image.png
GroupId:公司名或组织名
ArtifactId:一般是project项目名
Version:版本号,GroupId+ArtifactId+Packaging+Version构成项目的唯一标识
填写完成后点击finish
image.png
创建完成之后目录结构为这样,这时,你会发现index.jsp报错,暂时先不管他,因为缺少jar支持,等下配置了POM.XML就好了。
下一步添加java和test两个文件夹
项目右键 -> Build Path -> New Source Folder
image.png
这个时候就完成了个maven的工程的简单的搭建
image.png如果创建了没有JRE System Library,可以右键项目工程-->>properties->>java bulid path -->>>libraries -->>add library -->>JRE System Library 进行添加
。这里我用一个用以上搭建好了的Maven做目录解释。
通常情况下
src/main/resources下放各种配置文件
src/main/java下放源代码
webapp/jsp下放jsp页面
以此类推。
现在来配置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.wzh</groupId>
<artifactId>mavenDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mavenDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.2.0.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.8</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- java ee包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
</dependency>
<!-- spring框架包 start -->
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring框架包 end -->
<!-- mybatis框架包 start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mybatis框架包 end -->
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<!-- <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId>
<version>1.4</version> </dependency> -->
<!-- jstl标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log END -->
<!-- Json -->
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 start -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- oracle驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.2.0</version>
</dependency>
<!-- 添加CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta2</version>
</dependency>
<!-- pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<!-- jpinyin -->
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<!-- FTP -->
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- xml -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-all</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>mavenDemo</finalName>
<plugins>
<!-- jdk版本号 不指定这个重写方法注解会报错 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
这个是我平时日常练习用的配置,杂七杂八的都有,反正做Spirng的WEB项目基本功能只多不少。
如果下载包比较忙,这个是因为大家都知道的原因,你可以用梯子,也可以用其他人的镜像,这里推荐用阿里爸爸的镜像。
修改maven根目录下的conf文件夹中的setting.xml文件
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
下面进行项目整合,这里配置因为风格的关系就很多了,不过大多数情况差不多
jdbc.properties用于配置连接数据库相关信息,我这里偷懒了,没用专门的jdbc文件
log4j.properties用于配置log4j相关配置信息
applicationContext.xml Sprng相关配置
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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 开启自动扫描机制 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- <context:property-placeholder location="classpath:mysql.properties"/> -->
<!-- 配置数据库连接池 -->
<!-- 商业数据源 c3p0, apache提供的DBCP -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:oracle"></property>
<property name="user" value="自己的账号"></property>
<property name="password" value="自己的密码"></property>
<property name="initialPoolSize" value="15"></property><!-- 初始化连接数的数量 -->
<property name="minPoolSize" value="15"></property>
<property name="maxPoolSize" value="100"></property>
<property name="acquireIncrement" value="5"></property>
<property name="maxStatements" value="200"></property>
<property name="maxIdleTime" value="200"></property><!-- 超时时间 -->
<property name="checkoutTimeout" value="2000"></property><!-- 多长时间回收一次超时连接 -->
</bean>
<!-- SessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 如果有多包需要取类别名的话,使用,或;分割 -->
<property name="typeAliasesPackage" value="com.lovo.beans"></property>
</bean>
<!-- 将Session与Mapper直接进行关联 -->
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.lovo.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="txManage"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- session实例化 -->
<!-- 声明事物 -->
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<tx:method name="*" read-only="true"></tx:method>
<tx:method name="add*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="save*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="insert*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="update*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="modify*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="change*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception" isolation="REPEATABLE_READ"></tx:method>
<tx:method name="delete*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="remove*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<!-- 添加了一个删除方法的命名 -->
<tx:method name="del*" read-only="false" propagation="REQUIRED"
isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="select*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods"
expression="execution(* com.lovo.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"></aop:advisor>
</aop:config>
<!-- 开启切面功能 -->
<aop:aspectj-autoproxy />
<!--CXF配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:/cxf-beans.xml"/>
</beans>
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>
<!-- STDOUT 系统启动日志 -->
<!-- appender 给我们的日志定义一个输出目的地 -->
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{yyyy-MM-dd HH:mm:ss} --%p --%m%n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="STDOUT"></param>
<param name="levelMax" value="STDOUT"></param>
</filter>
</appender>
<!-- DEBUG 系统调试性日志-->
<appender name="DEBUG" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${mavenDemo.root}/logs/sh-debug.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10240KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{yyyy-MM-dd HH:mm:ss} --%p --%m%n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="DEBUG"></param>
<param name="levelMax" value="DEBUG"></param>
</filter>
</appender>
<!-- INFO 系统信息日志-->
<appender name="INFO" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${mavenDemo.root}/logs/sh-info.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10240KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{yyyy-MM-dd HH:mm:ss} --%p --%m%n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="INFO"></param>
<param name="levelMax" value="INFO"></param>
</filter>
</appender>
<!-- WARN 系统警告日志-->
<appender name="WARN" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${mavenDemo.root}/logs/sh-warn.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10240KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{yyyy-MM-dd HH:mm:ss} --%p --%m%n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="WARN"></param>
<param name="levelMax" value="WARN"></param>
</filter>
</appender>
<!-- ERROR 系统错误日志-->
<appender name="ERROR" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${mavenDemo.root}/logs/sh-error.log"/>
<param name="Append" value="true"/>
<param name="MaxFileSize" value="10240KB"/>
<param name="MaxBackupIndex" value="50"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%c %d{yyyy-MM-dd HH:mm:ss} --%p --%m%n"></param>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="ERROR"></param>
<param name="levelMax" value="ERROR"></param>
</filter>
</appender>
<root>
<priority value="DEBUG"></priority>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
</root>
</log4j:configuration>
log4j.properties,这里配得很简单
log4j.rootLogger=DEBUG, stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C\: %m%n
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet =INFO
这里我再web-inf文件夹下配的Spirng-servlet.xml,这个文件是主要用作SpirngMVC的配置
image.pngSpring-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"
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">
<!-- 开启springmvc的注解支持 -->
<mvc:annotation-driven conversion-service="tc" />
<!-- 定义静态资源 -->
<mvc:resources location="/static/" mapping="/static/**" />
<!-- 制定扫描规则 -->
<context:component-scan base-package="com">
<!-- 制定过滤器 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 可以配置一个全局类型转换器 -->
<!-- 配置spring定义的转换工厂类 -->
<bean id="tc"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="com.lovo.converter.GlobalDateConverter"></bean>
</set>
</property>
</bean>
<!-- 配置http消息体数据与JSON转换 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter"></ref>
</list>
</property>
</bean>
<!-- 配置HTTP消息信息与JSON对象之间的一个转换器 -->
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<!-- 视图解析器 JSP -->
<bean id="urlBasedViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/JSP/"></property>
<property name="suffix" value=".jsp"></property>
<property name="order" value="1"/>
</bean>
<!-- 视图解析器 FTL -->
<bean id="viewResolverFtl" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<!-- 将请求和会话属性作为变量暴露给FreeMarker模板使用。要做到这一点,
可以设置exposeRequestAttributes或者exposeSessionAttributes为true
-->
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
<!-- 使用这些宏,必须设置FreeMarkerViewResolver的exposeMacroHelpers属性为true -->
<property name="exposeSpringMacroHelpers" value="true" />
<!-- 使用缓存 -->
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<property name="order" value="0"/>
</bean>
<!-- 配置freeMarker的模板路径 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/FTL/"/>
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
<property name="defaultEncoding" value="UTF-8"/>
<!-- FreeMarker默认每隔5秒检查模板是否被更新,如果已经更新了,就会重新加载并分析模板。
但经常检查模板是否更新可能比较耗时。如果你的应用运行在生产模式下,而且你预期模板不会经常更新,
则可以将更新的延迟时间延长至一个小时或者更久。 可以通过为freemarkerSettings属性设置template_update_delay达到这一目的
-->
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">1</prop>
<!-- 设置标签类型 两种:[] 和 <> 。[] 这种标记解析要快些 -->
<prop key="tag_syntax">auto_detect</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="output_encoding">UTF-8</prop>
<prop key="locale">zh_CN</prop>
<!-- 设置时间格式 -->
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<!-- 设置数字格式 -->
<prop key="number_format">#.##</prop>
<!-- 可以满足一般需要。默认情况变量为null则替换为空字符串,如果需要自定义,写上${empty!"EmptyValue of fbysss"}的形式即可 -->
</props>
</property>
</bean>
<bean id="fmXmlEscape" class="freemarker.template.utility.XmlEscape"/>
<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver 说明: p:defaultEncoding="UTF-8":这里设置默认的文件编码为UTF-8,必须与用户JSP的默认编码一致;
p:maxUploadSize="5000000":指定文件上传大小,单位为字节; p:uploadTempDir="fileUpload/temp":文件上传临时目录,上传完成后,就会将临时文件删除; -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8" p:maxUploadSize="5000000" p:uploadTempDir="fileUpload/temp">
</bean>
</beans>
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" 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>mavenDemo</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>mavenDemo.root</param-value>
</context-param>
<!-- spring容器启动 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<!-- 指定日志刷新的时间,单位毫秒 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置欢迎页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 字符过滤器 -->
<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>
<!-- <filter>
<filter-name>encoding</filter-name>
<filter-class>com.lovo.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> -->
<!-- 启动CXF框架 web service-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<!-- 配置核心控制器 -->
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这么一个简单的项目就搭建好了,可以进行简单的测试了。
网友评论