本节主要记录SSH整合的时候需要的相关配置,配置的时候有点蒙,特此记录一下。
1.所需jar包
首先是struts所需的jar包(9个):
其次是hibernate所需的jar包(14个):
最后是spring所需的jar包(24个):
共47个jar包
2.目录结构
以下主要展示配置文件所在位置
SpringDemo1
|_src
|_struts.xml
|_WebRoot
|_WEB-INF
|_lib
|_applicationContext.xml
|_web.xml
3.web.xml
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListenerlistener-class>
listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
web-app>
3.struts.xml
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true">constant>
<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
<constant name="struts.il8n.encoding" value="UTF-8" />
<package name="lzf" namespace="/" extends="struts-default">
<action name="loginAction" class="cn.lzf.shopping.action.LoginAction">
<result name="error">/WEB-INF/content/login_err.jspresult>
<result>/WEB-INF/content/login_succ.jspresult>
action>
<action name="*">
<result>/WEB-INF/content/{1}.jspresult>
<result name="login">/WEB-INF/content/login.jspresult>
action>
package>
struts>
3.application.xml
<beans
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean
id="dataSourse"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"
p:driverClass="com.mysql.jdbc.Driver"
p:jdbcUrl="jdbc:mysql://localhost/hibernate"
p:user="root"
p:password="123456"
p:maxPoolSize="40"
p:minPoolSize="2"
p:initialPoolSize="2"
p:maxIdleTime="30" />
<bean
id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSourse">
<property name="annotatedClasses">
<list>
<value>cn.lzf.shopping.orm.Uservalue>
list>
property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect
prop>
<prop key="hibernate.hbm2ddl.auto">updateprop>
props>
property>
bean>
<bean
id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<tx:advice
id="txAdvice"
transaction-manager="transactionManager">
<tx:attributes>
<tx:method
name="get*"
read-only="true" />
<tx:method
name="*"
isolation="DEFAULT"
propagation="REQUIRED"
timeout="5" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut
expression="bean(userService)"
id="myPointcut" />
<aop:advisor
advice-ref="txAdvice"
pointcut-ref="myPointcut" />
aop:config>
<bean
id="userService"
class="cn.lzf.shopping.service.UserServiceImpl"
p:userDao-ref="userDao" />
<bean
id="userDao"
class="cn.lzf.shopping.dao.UserDaoHibernate"
p:sessionFactory-ref="sessionFactory">bean>
<bean
id="user"
class="cn.lzf.shopping.orm.User">bean>
beans>
至此,基本配置已经完成,在Spring整合Struts时有两种方式:
在struts.xml中进行如下配置,class属性值并不是实现类,而是id
<action name="loginAction" class="realLoginAction">
<result name="error">/WEB-INF/content/login_err.jspresult>
<result>/WEB-INF/content/login_succ.jspresult>
action>
在applicationContext.xml中进行如下配置,依赖注入由Spring完成:
<bean
id="realLoginAction"
class="cn.lzf.shopping.action.LoginAction"
scope="prototype"
p:userService-ref="userService"
p:user-ref="user">
bean>
<action name="loginAction" class="cn.lzf.shopping.action.LoginAction">
<result name="error">/WEB-INF/content/login_err.jspresult>
<result>/WEB-INF/content/login_succ.jspresult>
action>
使用自动装配时Spring不需要再单独配置该bean,那么Spring怎么知道为其注入哪些bean呢?
所以这就要求该Action所依赖的Bean的名字与在Spring中配置的名字相同。
如下,LoginAction有如下片段
public class LoginAction extends ActionSupport{
//用户业务逻辑组件
private UserService userService;
private User user;
//...
}
所以在applicationContext.xml中必须要以下片段,注意bean的id与Action中依赖的对象名是相同的。
<bean
id="user"
class="cn.lzf.shopping.orm.User">bean>
<bean
id="userService"
class="cn.lzf.shopping.service.UserServiceImpl"
p:userDao-ref="userDao" />
当使用OGNL将请求参数转换为Bean对象时,稍不注意就会有问题,在jsp中使用:
注意textfield的name属性值
<s:form action="loginAction" id="form1">
<s:textfield label="用户名" name="user.tel">s:textfield>
<s:textfield label="密码" name="user.password">s:textfield>
<s:submit value="登录账号" id="submit">s:submit>
s:form>
表单的数据会被提交到loginAction,那么在loginAction的实现类中肯定有属性名为user的Bean对象,该属性必须有setter、getter方法
这里用到了User对象的tel、name,所以在User类中必须为这两个参数设置setter、getter方法。