利用spring管理struts
SSH部分
第一:在做SSH配置之前,一定要把jar包文件夹下的所有JAR包COPY到工程中,否则报错误.
第二:在做Service类的时候首先要定义一个调用Dao的接口并添加上Set和Get方法
例:
public class ServiceClass implements ServiceInterface
{
//这个是定义的接口
private DaoInterface mydao;
//这个是Set和Get方法
public DaoInterface getMydao() 
...{
return mydao;
}
public void setMydao(DaoInterface mydao) 
...{
this.mydao = mydao;
}
public List findAll() 
...{
return mydao.findAll();
}
public void insert(Sshtable transientInstance) 
...{
mydao.insert(transientInstance);
}
public void delete(int persistentInstance) 
...{
mydao.delete(persistentInstance);
}
}
第三:如果出现STRUTS乱码问题
在JSP页头写

<%...@ page language="java" import="java.util.*" contentType="text ml;charset=utf-8" pageEncoding="GBK"%>
后台转换:
New String(str.getBytes(“iso-8859-1”),”utf-8”);
就可以了
在数据库连接的地方:
<property name="url" value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf-8&useUnicode=true">
</property>
第四:在配置数据库DAO的HBM文件中,自增主建的时候 一般用native或者assigned(这个不确定或者是其它,哪个好用就用哪一个),
例:
<hibernate-mapping>
<class name="org.yuanyuan.database.Yuan01" table="yuan01" catalog="loveyuanyuan">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="assigned" />
</id>
//或者
<hibernate-mapping>
<class name="Hibernate.Sshtable" table="sshtable" catalog="ssh">
<id name="name" type="java.lang.Integer">
<column name="name" />
<generator class="native" />
</id>
第五:在applicationContext.xml中配置以下红色字的地方就可以在控制台中,打印出hibernate的中的SQL语句
<bean id="SessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="DataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>Hibernate/Sshtable.hbm.xml< alue>
< st>
</property>
</bean>
(文章来源 www.iocblog.net)
第六 struts+spring整和的时候在struts-config.xml中的 下添加(必须是这个位置)
<message-resources parameter="com.ssh.struts.ApplicationResources" /><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
</plug-in>
然后把所有struts-config.xml里action中的type=的内容修改成type="org.springframework.web.struts.DelegatingActionProxy">
以上为struts-config.xml的配置其中注意
value="/WEB-INF/classes/applicationContext.xml"的路径以TOMCAT中路径为标准.
在applicationContext.xml中添加对应struts-config.xml里的action的bean 就可以了
<bean name="/StrutsAction" class="StrutsAction.StrutsAction"></bean>
其中name="/StrutsAction"与action中的path="/StrutsAction"对应
class="StrutsAction.StrutsAction" 就是
type="org.springframework.web.struts.DelegatingActionProxy">替换掉的哪一部分.
例子.
Struts的action
<action path="/StrutsAction"
type=" StrutsAction.StrutsAction "
>
<forward name="finish" path="/index.jsp" />
</action>
//修改成
<action path="/StrutsAction"
type="org.springframework.web.struts.DelegatingActionProxy"
>
<forward name="finish" path="/index.jsp" />
</action>
在applicationContext.xml新建立的BEAN为
(文章来源 www.iocblog.net)
<bean name="/StrutsAction" class="StrutsAction.StrutsAction"></bean>
这样就OK了
赶快试试吧!~~~
文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。