SSH2整合注意事项

3 篇文章 0 订阅
1 篇文章 0 订阅
 
ssh2  即 struts2 +spring2.5+hibernate3.2
(一)--jar包
 
我使用的开发环境为 Myeclipse6.5 + struts2.1.6 + spring2.5 + hibernate3.2 + Myeclipse6.5自带tomcat6.0
首先我喜欢用工具,工具就是为了方便么,所以spring和hibernate的加载都是Myeclipse自动生成的.
但是这两个框架加载时就有个很古老的问题,就是jar包冲突
删除 asm-2.2.3.jar 即可 , 注: 该包可以在整个工程发布到tomcat测试的时候在删除,删除后不要再次重新发布工程了,修改的内容myeclipse会自动发布上去的.省的每次都要删除一遍该jar包
现在该添加struts2了,
struts-2.0.11.1版本
导入6个包即可:
commons-logging-1.0.4.jar
ognl-2.6.11.jar
freemarker-2.3.8.jar
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar
struts2-spring-plugin-2.0.11.1.jar
struts-2.1.6版本
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
struts2-dojo-plugin-2.1.6.jar
xwork-2.1.2.jar
struts2-spring-plugin-2.1.6.jar
如果提示错误,你还需要添加
commons-io-1.3.2.jar
commons-fileupload-1.2.1.jar
其他的包可以用到了在添加
 
(二)--配置文件
首先是web.xml需要添加的节点
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/spring*.xml
        </param-value>
    </context-param>
    <filter>
        <filter-name>HibernateFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>HibernateFilter</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.context.ContextLoaderListener
        </listener-class>
    </listener>
 
其次是struts.xml,与strut1不同
<struts>
 <include file="struts-default.xml"/>
 <package name="wzl" extends="struts-default">
  <action name="CustomerAction" class="CustomerAction">
   <result name="success">HelloWorld.jsp</result>
   <result name="error">SayHello.jsp</result>
  </action> 
 </package>
</struts>
最后是struts.properties
struts.objectFactory =spring
这一句话将struts2交于spring托管
 
(三)--跳转
 
这里的跳转主要注意的是struts2与spring结合后的跳转写法
首先是action类
继承ActionSupport类,返回类型为String,调用父类的SUCCESS和ERROR常量作为返回值,这里主要是规范代码书写格式,想写自己的返回值也可以,返回值在struts配置文件中调用
public String execute(){
  List customerList=customerDao.findByCustomername(name);
  if(customerList!=null&&!customerList.isEmpty()){
   return SUCCESS;
  }else{
   return ERROR;
  }
 }
其次spring配置文件中管理struts2的action类
<bean id="CustomerAction" class="com.wzl.action.CustomerAction">
  <property name="customerDao">
   <ref bean="CustomerDAO"/>
  </property>
 </bean>
然后在struts2的配置文件中将页面跳转的action类的class路径设置为spring配置文件中bean的id
而不是工程路径
<action name="CustomerAction" class="CustomerAction">
   <result name="success">HelloWorld.jsp</result>
   <result name="error">SayHello.jsp</result>
  </action>
最后是页面调用,这里有两种方法都可以提交到CustomerAction
1.用struts2标签,此时action名称写struts配置文件中action的name名称
<s:form action="CustomerAction">
    Name:<s:textfield name="name"/>
    <s:submit/>
   </s:form>
2.用html标签,此时action中的路径要加上struts2里action的后缀名,默认的是.action,这样才能交由struts2管理
<form action="CustomerAction.action">
 
(四)--传参
 
由于Struts2中没有了ActionForm,并且不需要HttpRequest对象,所以以往通过request.getParameter获取数据的方法在这里不能实现
不过Struts2 中把ActionForm整合到了Action中,通过IOC反转注入方法为其赋值,
页面中通过struts2标签<s:textfield name="name"/>中的name属性传递参数
传递到action中后,在action中声明该name属性的值同名称的变量并封装
private String name;
public String getName() {
  return name;
 }
public void setName(String name) {
  this.name = name;
}
这时struts2自动调用set方法为其赋值
跳转到另一个页面后同样通过Struts2标签获取该属性的值
<s:property value="name"/>
 
 
 
顺便写写SSH整合报错经典集合
1:could not create proxy factory (这是个不太关键的问题,但是咧,看着不爽)
原因是在Spring 和 Hibernate整合时候,Spring AOP包中包含了 asm-2.3.2.jar 的包
hibernate Core包中也加入了 asm.jar包.
冲突解决办法就是删除 asm-2.2.3.jar.然后再刷新工程,就OK了
2:Could not open ServletContext resource [/WEB-INF/applicationContext.xml] (这个问题我想了半天。但是就是没有明白为什么会这样。。呵呵。丢。。)
解决办法将applicationContexst.xml文件拷到web-info目录下,就OK了,或者在web.xml中将spring配置文件路径写上
3.No WebApplicationContext found: no ContextLoaderListener registered (这个很简单,只是忘了。哈哈)
解决办法 将下面的拷到web.xml文件下就OK了、、
<listener>
   <listener-class>
    org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值