最近项目不是很忙,于是就自己搭建了个Struts2,Spring,Hibernate的框架,实现了一张表的增删改查。因为一次不能上传超过10M的项目,我把jar包烤出来放了两个文件夹里面,三个附件下载完后需要把jar包拷贝到WEB-INF/lib里面。项目方能运行。工程名字就叫Struts2,数据库是MySql.项目的几个重要配置文件如下:
web.xml
<!-- 上下文参数(包含要监听的配置文件) -->
<context-param>
<param-name>contexConfigLoacation</param-name>
<!-- 可以写多个配置文件,用逗号隔开或者空格 -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
applicationContext.xml
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/hrm_db?useUnicode=true&characterEncoding=utf-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">
true
</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/soft/model/Taskinfo.hbm.xml</value>
</list>
</property>
</bean>
<bean id="taskDao" class="com.soft.dao.Impl.TaskInfoDaoImpl">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="taskService" class="com.soft.service.Impl.TaskInfoServiceImpl">
<property name="taskDao">
<ref bean="taskDao"/>
</property>
</bean>
<bean id="taskAction" class="com.soft.action.TaskInfoAction">
<property name="taskService">
<ref bean="taskService"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="baseTransactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="persist*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="save">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="edit*">PROPAGATION_REQUIRED</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="disPlay*">PROPAGATION_REQUIRES_NEW</prop>
</props>
</property>
</bean>
Struts.xml
<struts>
<package name="system" extends="struts-default">
<action name="Taskdoadd" class="com.soft.action.TaskInfoAction" method="doadd">
<result name="success">/WEB-INF/jsp/taskquery.jsp</result>
</action>
<action name="Taskupdate" class="com.soft.action.TaskInfoAction" method="update">
<result name="success">/WEB-INF/jsp/taskquery.jsp</result>
</action>
<action name="Taskdelete" class="com.soft.action.TaskInfoAction" method="delete">
<result name="success">/WEB-INF/jsp/taskquery.jsp</result>
</action>
<action name="task*" class="com.soft.action.TaskInfoAction" method="{1}">
<result name="success">/WEB-INF/jsp/task{1}.jsp</result>
</action>
</package>
</struts>