源码和详细说明文档,可以到下载页下载:http://download.csdn.net/detail/fufengrui/4086099
1. struts2.2配置
下载struts2.2,进入该目录apps,解压:struts2-blank.war,参照这个项目进行配置:
1.1引入需要的jar包
1)commons-fileupload-1.2.1.jar
2)commons-io-1.3.2.ja
3)commons-lang-2.5.jarr
4)freemarker-2.3.16.jar
5)javassist-3.7.ga.jar
6)ognl-3.0.jar
7)struts2-core-2.2.1.jar
8)xwork-core-2.2.1.jar
9) commons-logging-1.1.1.jar
1.2 web.xml中添加配置:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1.3 src下添加文件:struts.xml
1.4 测试struts配置成功:
1.新建HelloAction类
public class HelloAction extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
2.修改struts.xml
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true"/>
<packagename="hello" namespace="/" extends="struts-default">
<actionname="hello" class="cn.donate.test.HelloAction">
<result>/index.jsp</result>
</action>
</package>
</struts>
3.部署到tomcat。
启动,访问:http://localhost:端口/项目名/hello.action,如果跳转至index.jsp,则struts2配置成功
2 配置spring
将下载的spring-framework-3.1.1.RELEASE.zip包解压,将dist中的所有包考到web-lib目录下
在这个目录下:spring-framework-3.1.1.RELEASE\projects\org.springframework.web.servlet\src\test\java\org\springframework\web\context\WEB-INF
将applicationContext.xml文件拷贝至src目录下
修改web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://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">
<display-name>donate</display-name>
<!--Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</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:applicationContext.xml</param-value>
</context-param>
<!--Session超时定义,单位为分钟 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!--著名 Character Encoding filter -->
<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>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!--struts2 配置 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
添加applicationContext.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-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/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!--配置sessionFactory -->
<beanid="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<propertyname="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!--配置和声明事务管理器 -->
<beanid="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<propertyname="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driventransaction-manager="transactionManager" />
<!--配置 hibernate模板-->
<beanid="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<propertyname="sessionFactory" ref="sessionFactory"/>
</bean>
<!--支持自动装配,通过标记代替配置文件 -->
<context:annotation-config/>
<context:component-scanbase-package="cn.tlcl"/>
</beans>
3.配置hibernate3
引入包:D:\开源\hibernate3.6.7\hibernate-distribution-3.6.7.Final\lib\required
D:\开源\hibernate3.6.7\hibernate-distribution-3.6.7.Final\lib\jpa
hibernate3.jar
D:\开源\hibernate3.6.7\hibernate-distribution-3.6.7.Final\lib\optional\c3p0
所有包
添加hibernate.cfg.xml在src目录下:
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE hibernate-configurationPUBLIC
"-//Hibernate/HibernateConfiguration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库方言 -->
<propertyname="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--数据库驱动 -->
<propertyname="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--数据库连接信息 -->
<propertyname="hibernate.connection.url">
jdbc:mysql://localhost:3306/kepuvideo?userUnicode=true&characterEncoding=utf-8
</property>
<propertyname="hibernate.connection.username">
root
</property>
<propertyname="hibernate.connection.password">
123456
</property>
<!--打印SQL语句 -->
<propertyname="hibernate.show_sql">true</property>
<!--不格式化SQL语句 -->
<propertyname="hibernate.format_sql">false</property>
<!--为Session指定一个自定义策略 -->
<propertyname="hibernate.current_session_context_class">
thread
</property>
<!--C3P0 JDBC连接池 -->
<propertyname="hibernate.c3p0.max_size">20</property>
<propertyname="hibernate.c3p0.min_size">5</property>
<propertyname="hibernate.c3p0.timeout">120</property>
<propertyname="hibernate.c3p0.max_statements">100</property>
<propertyname="hibernate.c3p0.idle_test_period">120</property>
<propertyname="hibernate.c3p0.acquire_increment">2</property>
<propertyname="hibernate.c3p0.validate">true</property>
<!--映射文件 -->
<!--<mapping resource="com/lyq/model/user/User.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
4.引入驱动类
mysql-connector-java-5.1.18-bin.jar
异常:
Caused by:java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
需要添加:
aopalliance.jar
添加log4j.properties,内容:
#DEBUG,INFO,
log4j.rootLogger=INFO,ConsoleLog
log4j.appender.ConsoleLog=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleLog.layout=org.apache.log4j.PatternLayout
#log4j.appender.ConsoleLog.layout.ConversionPattern=[%-5p]%d{yyyy-MM-ddHH\:mm\:ss,SSS}%n%m%n%n
log4j.appender.ConsoleLog.layout.ConversionPattern=%t[%-5p] [%d{yyyy-MM-dd HH\:mm\:ss,SSS}] %40l - %m%n
log4j.logger.cn.kepu=DEBUG
log4j.appender.FileLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FileLog.DatePattern='.'yyyy-MM-dd
log4j.appender.FileLog.file=logs/donate_app.log
#log4j.appender.FileLog.encoding=UTF-8
log4j.appender.FileLog.layout=org.apache.log4j.PatternLayout
log4j.appender.FileLog.layout.ConversionPattern=[%-5p]%d{yyyy-MM-ddHH\:mm\:ss,SSS}%n%m%n%n
添加log4j-1.2.16.jar
异常:
nested exception isjava.lang.UnsupportedClassVersionError: Bad version number in .class file(unable to load class org.hibernate.cfg.Configuration)
由于jdk版本问题,tomcat换成jdk1.6编译即可
异常:SLF4J: Failed to load class"org.slf4j.impl.StaticLoggerBinder".
下载slf4j包,添加:slf4j-log4j12-1.6.4.jar这个包
启动测试