ssh整合步骤
一、先分好层,如 action service dao model
二、整合hibernate
①加hibernate所需要的包和数据库的驱动包(这里用的是与oracle交互,所以引入classes12.jar):
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.15.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
②加上与数据库的连接,这里用jdbc.properties,简单配置与数据库的连接,后面的配置交给spring
三、加junit的包(这步可省略,这里是为了测试)
junit-4.4.jar
四、 整合struts
① 加struts需要的基础包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
另外:
struts2-convention-plugin-2.1.8.jar(支持sturts的注解,struts.xml就可以不需要了)
②在web.xml中加入struts的核心filter
<!-- 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>
③另外:
还有struts和spring整合的包
struts2-spring-plugin-2.1.8.1.jar(注意相应版本)
注意:action是由struts管理,所以想在struts中注入spring中管理的service、dao
等bean,需要加struts2-spring-plugin文件,然后有以下两种方法
a.
Action类前加@Component,则Action可由spring来管理,例子如下:
Action中写:
@Component("u") //spring管理注解
@Scope("prototype") //多态
public class UserAction extends ActionSupport implements ModelDriven{
//内部属性需要有get/set方法 且需要set方法前加@Resource或@Autowired
}
Struts2配置文件中写
<action name="u" class="u">
Jsp中
<form method="post" action="u.do" >
b.
Action中也可不加@Component,Action由struts2-spring-plugin管理。此时,如果Action中定义的属性有set方法 则@Autowired 与@Resource也可不写,但是如果没有set方法,则需要在属性前加上@Autowired 或@Resource才能生效。
注意:a.这里是struts的action由struts-spring的插件与struts管,是按名称注入,但并非由spring注入,
当它需要的service时,到spring容器中找,进行注入,所以在acton上不用加@Component("u")
b.action里面的属性(需要注入的属性)在set上是可以不用加@Autowired 或@Resource的,因为它会自动按照你属性的名字按名称在spring中找,进行注入,这时在set方法上写了也无用
但若要指定注入的名称,则可在属性名称上加@Autowired 或@Resource
比如:
@Autowired
@Qualifier("portalTemplateFieldService")
private IPortalTemplateFieldService portalTemplateFieldService;
或:
@resource(name ="portalTemplateFieldService")
private IPortalTemplateFieldService portalTemplateFieldService;
c.不过也可以将action交给spring注入,那么就可以在set方法上加@resource注入了
这时的配置如下:
struts.xml中:
<package name="test" namespace="/test" extends="struts-default">
<action name="uu" class="u">
<result>helloWorld2.jsp</result>
<result name="input">helloWorld2.jsp</result>
</action>
</package>
这里的u即为action上@Component后的名称
在action中:
@Component
@Scope("prototype")
public class UserAcion extends ActionSupport{
}
这里千万别忘记了加 @Scope("prototype"),因为action交给spring控制后,默认为单例了,这时要改成多例
五、整合spring
① 加spring需要的jar包
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-dbcp.jar
commons-logging-1.1.1.jar(和struts要求的包一样,可以用最新版本的)
commons-pool.jar
spring.jar
②加spring.xml
配置datasource、sessionFactory、transactionManager等 (参考spring-base.xml)
③在web.xml中加监听器,使web一启动,就加载容器
(会找spring的配置文件,默认寻找/WEB-INF/applicationContext.xml)
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
而这里,我们可以在web.xml中指定spring的配置文件位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:spring-*.xml</param-value>
</context-param>
以上ssh原理上已整合完毕
一、先分好层,如 action service dao model
二、整合hibernate
①加hibernate所需要的包和数据库的驱动包(这里用的是与oracle交互,所以引入classes12.jar):
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
ejb3-persistence.jar
hibernate3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
log4j-1.2.15.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
②加上与数据库的连接,这里用jdbc.properties,简单配置与数据库的连接,后面的配置交给spring
三、加junit的包(这步可省略,这里是为了测试)
junit-4.4.jar
四、 整合struts
① 加struts需要的基础包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
xwork-core-2.1.6.jar
另外:
struts2-convention-plugin-2.1.8.jar(支持sturts的注解,struts.xml就可以不需要了)
②在web.xml中加入struts的核心filter
<!-- 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>
③另外:
还有struts和spring整合的包
struts2-spring-plugin-2.1.8.1.jar(注意相应版本)
注意:action是由struts管理,所以想在struts中注入spring中管理的service、dao
等bean,需要加struts2-spring-plugin文件,然后有以下两种方法
a.
Action类前加@Component,则Action可由spring来管理,例子如下:
Action中写:
@Component("u") //spring管理注解
@Scope("prototype") //多态
public class UserAction extends ActionSupport implements ModelDriven{
//内部属性需要有get/set方法 且需要set方法前加@Resource或@Autowired
}
Struts2配置文件中写
<action name="u" class="u">
Jsp中
<form method="post" action="u.do" >
b.
Action中也可不加@Component,Action由struts2-spring-plugin管理。此时,如果Action中定义的属性有set方法 则@Autowired 与@Resource也可不写,但是如果没有set方法,则需要在属性前加上@Autowired 或@Resource才能生效。
注意:a.这里是struts的action由struts-spring的插件与struts管,是按名称注入,但并非由spring注入,
当它需要的service时,到spring容器中找,进行注入,所以在acton上不用加@Component("u")
b.action里面的属性(需要注入的属性)在set上是可以不用加@Autowired 或@Resource的,因为它会自动按照你属性的名字按名称在spring中找,进行注入,这时在set方法上写了也无用
但若要指定注入的名称,则可在属性名称上加@Autowired 或@Resource
比如:
@Autowired
@Qualifier("portalTemplateFieldService")
private IPortalTemplateFieldService portalTemplateFieldService;
或:
@resource(name ="portalTemplateFieldService")
private IPortalTemplateFieldService portalTemplateFieldService;
c.不过也可以将action交给spring注入,那么就可以在set方法上加@resource注入了
这时的配置如下:
struts.xml中:
<package name="test" namespace="/test" extends="struts-default">
<action name="uu" class="u">
<result>helloWorld2.jsp</result>
<result name="input">helloWorld2.jsp</result>
</action>
</package>
这里的u即为action上@Component后的名称
在action中:
@Component
@Scope("prototype")
public class UserAcion extends ActionSupport{
}
这里千万别忘记了加 @Scope("prototype"),因为action交给spring控制后,默认为单例了,这时要改成多例
五、整合spring
① 加spring需要的jar包
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-dbcp.jar
commons-logging-1.1.1.jar(和struts要求的包一样,可以用最新版本的)
commons-pool.jar
spring.jar
②加spring.xml
配置datasource、sessionFactory、transactionManager等 (参考spring-base.xml)
③在web.xml中加监听器,使web一启动,就加载容器
(会找spring的配置文件,默认寻找/WEB-INF/applicationContext.xml)
<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
而这里,我们可以在web.xml中指定spring的配置文件位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> classpath*:spring-*.xml</param-value>
</context-param>
以上ssh原理上已整合完毕
还可以加上公用的 commons-lang-2.4.jar,来实现一些常用的操作
但是还有几个常见问题需要解决
1.防止session被没有用完就关闭问题
Hibernate如果使用load来查询数据,例如:
Service中:
public User loadById(int id) {
return this.userDao.loadById(id);
}
DAO中:
public User loadById(int id) {
return (User)this.hibernateTemplate.load(User.class, id);
}
此时,session(应该说的是Hibernate的session)在事物结束(通常是service调用完)后自动关闭。由于使用的是load获取数据,在jsp页面申请取得数据时才真正的执行sql,而此时session已经关闭,故报错。
Session关闭解决方法:
在web.xml中增加filter—openSessionInView,用于延长session在jsp调用完后再关闭
如下所示:
注意:filter –openSessionInView 一定要在 filter—struts2之前调用
Filter顺序—先进后出!
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf</param-value>(此处默认指定的sessionFactory应为” sessionFactory” 默认可省略此行 如果shring配置文件中配置的
sessionFactory为”sf” 则此处需要写sf 一般用不到)
</init-param>
</filter>
<filter-mapping>
<filter-name> OpenSessionInViewFilter </filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
2. struts中文乱码问题
① Struts2.1.8已经修正,只需要改i18n.encoding = UTF-8
例:
struts.properties中配置struts.i18n.encoding=UTF-8
或 sturts.xml中配置<constant name="struts.i18n.encoding" value="UTF-8" />
② 使用spring的characterencoding
例:
<!-- 过滤器相关配置 ======== 字符编码过滤======== -->
<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>
但是还有几个常见问题需要解决
1.防止session被没有用完就关闭问题
Hibernate如果使用load来查询数据,例如:
Service中:
public User loadById(int id) {
return this.userDao.loadById(id);
}
DAO中:
public User loadById(int id) {
return (User)this.hibernateTemplate.load(User.class, id);
}
此时,session(应该说的是Hibernate的session)在事物结束(通常是service调用完)后自动关闭。由于使用的是load获取数据,在jsp页面申请取得数据时才真正的执行sql,而此时session已经关闭,故报错。
Session关闭解决方法:
在web.xml中增加filter—openSessionInView,用于延长session在jsp调用完后再关闭
如下所示:
注意:filter –openSessionInView 一定要在 filter—struts2之前调用
Filter顺序—先进后出!
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf</param-value>(此处默认指定的sessionFactory应为” sessionFactory” 默认可省略此行 如果shring配置文件中配置的
sessionFactory为”sf” 则此处需要写sf 一般用不到)
</init-param>
</filter>
<filter-mapping>
<filter-name> OpenSessionInViewFilter </filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>
2. struts中文乱码问题
① Struts2.1.8已经修正,只需要改i18n.encoding = UTF-8
例:
struts.properties中配置struts.i18n.encoding=UTF-8
或 sturts.xml中配置<constant name="struts.i18n.encoding" value="UTF-8" />
② 使用spring的characterencoding
例:
<!-- 过滤器相关配置 ======== 字符编码过滤======== -->
<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>