`
jsx112
  • 浏览: 308633 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

S2SH(struts2+spring+hibernate整合)要点,注意点

阅读更多
S2SH(struts2+spring+hibernate整合)要点,注意点
------------------------统一IDE,Struts2,Spring,Hibernate版本:
Myeclipse: 7.0
web project: javaEE 1.4
jdk :   1.5(5.0)
Tomcat: tomcat6.0.18
oracle:   10g
Struts2: struts2.0.14
Spring: spring2.5-------spring2.5 AOP Libraries,
     sping2.5 Core Libraries,
     spring2.5 Persisteence Core Libraries
     spring2.5 Persisteence JDBC Libraries
     spring2.5 Web Libraries
   
/WebRoot/WEB-INF/applicationContext.xml
hibernate托管给spring


Hibernate: hibernate3.2------------H3.2 Annotations&Entity Manager
      H3.2 Core Libraries
      H3.2 Advanced Support Libraries


-------------------------------------------S2SH搭建环境
目录结构:DAO(接口IUser,实现类User),service,action,po

struts2的配置包的导入,需要的是5个jar包分别是:

struts2-core-2.0.11.2.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

xwork-2.05.jar

commons-logging-1.0.4.jar

struts2+spring配置包:struts2-spring-plugin-2.0.11.2.jar
----------------------------------------------------------------Web.<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
   <!-- 配置spring的监听器 -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value> /WEB-INF/applicationContext.xml,
    /WEB-INF/spring-config/spring_dao.xml </param-value>
</context-param>
    <!-- 开启监听 -->
<listener>
   <listener-class> org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
<!--
   配置OpenSessionInViewFilter,必须在struts2监听之前,用来解决could not initialize
   proxy - no Session报错问题,配置自己写的filter
-->
<filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter
        </filter-class>
    </filter>
   
<filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</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>
<!--
   设置访问资源,能访问web-inf下的jsp页面 <servlet> <servlet-name>test</servlet-name>
   <jsp-file>/WEB-INF/ui/jsp/globle/main.jsp</jsp-file> </servlet>
   <servlet-mapping> <servlet-name>test</servlet-name>
   <url-pattern>/test.jsp</url-pattern> </servlet-mapping>
-->
<welcome-file-list>
   <welcome-file>/WEB-INF/ui/jsp/globle/main.jsp</welcome-file>
</welcome-file-list>
</web-app>


--------------------------------------------------------------------applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!--
   解决:Write operations are not allowed in read-only mode
   (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO
   or remove 'readOnly' marker from transaction definition.
-->
<bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Advice(事务的传播特性) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="get*" propagation="SUPPORTS" />
    <tx:method name="search*" propagation="SUPPORTS" />
    <tx:method name="show*" propagation="SUPPORTS" />
 
   </tx:attributes>
</tx:advice>
<!-- 配置事务管理器应用的范围 -->
<aop:config>
   <aop:pointcut id="affectMethods"
    expression="execution(* lk.edu.tms.*.*(..))" />
   <aop:advisor advice-ref="txAdvice" pointcut-ref="affectMethods" />
</aop:config>

。。。。。。。

/////配置注入:service<-DAO,可以多个dao注入同一个service
<!-- dao注入service, class是service实现类,ref的UserDAO也是实现类 -->
<bean id="userService" class="service.UserService">
<property name="iuserDao" ref="UserDAO"></property>
</bean>

/////配置注入:Action<-service 
<!--struts2整合spring时:service注入action中的bean id要与struts.xml配置文件中的action class一致 -->
<bean id="loginAction" class="action.LoginAction" scope="prototype">
<property name="iuserservice" ref="userService"></property>
</bean>


------------------------------------------------------


-------------------------------------------------解决乱码:
1.JSP页面s:textfield中输入的值传入oracle数据库,数据库中显示乱码,解决在jsp页面中用:<%@ page language="java" contentType="text/html; charset=utf-8"%>

2.JSP页面s:textfield中从数据库查询数据显示在文本框中的是乱码,
解决:配置tomcat(E:\webWorkspace7.0\apache-tomcat-6.0.18\conf\server.xml)中加入URIEncoding="UTF-8"
<Connector port="80" protocol="HTTP/1.1"
    connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
   
-----------------------------------------------------
数据库连接用oracle时加入classes12.jar包,用mysql时加入mysql-connector-java-5.0.5-bin.jar

-------------------------------------------------------------------------

S2SH使用oracle数据库时,(工程有红叉,但子项内无红叉)若提示“表名无效”,则将hbm.xml文件中的table加''(table='"user"'),因为myeclipse生成hbm.xml时没有''.

---------------------------------------------------------web.xml中配置spring监听器、FilterDispatcher:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
   <!-- 配置spring的监听器 -->
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value> /WEB-INF/applicationContext.xml,
    /WEB-INF/spring-config/spring_dao.xml </param-value>
</context-param>
    <!-- 开启监听 -->
<listener>
   <listener-class> org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
<!--
   配置OpenSessionInViewFilter,必须在struts2监听之前,用来解决could not initialize
   proxy - no Session报错问题,配置自己写的filter
-->
<filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OurOpenSessionInViewFilter
        </filter-class>
    </filter>
   
<filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</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>
<!--
   设置访问资源,能访问web-inf下的jsp页面 <servlet> <servlet-name>test</servlet-name>
   <jsp-file>/WEB-INF/ui/jsp/globle/main.jsp</jsp-file> </servlet>
   <servlet-mapping> <servlet-name>test</servlet-name>
   <url-pattern>/test.jsp</url-pattern> </servlet-mapping>
-->
<welcome-file-list>
   <welcome-file>/WEB-INF/ui/jsp/globle/main.jsp</welcome-file>
</welcome-file-list>
</web-app>

-----------------“不能实例化action”的错误:可能dao,service接口实现时没写implements---------------------

----------------------------------------------------------------------
<struts>
<!-- Struts 2的Action必须放在指定的包空间下定义 -->
<package name="struts2" extends="struts-default">
<!--伪action 它的class必须与applicationContext.xml中service注入action配置时的bean id一致,action name与login.jsp中的form表单的action一致(Login.action) -->
      <action name="Login" class="loginAction">
           <!-- 定义处理结果和资源之间映射关系。 -->
           <result name="input">/login.jsp</result>
    <result name="error">/error.jsp</result>
    <result name="success">/welcome.jsp</result>
   </action>
</package>
</struts>

-----------------------------------------------------------------------
8、为了解决hibernate延迟加载的问题,使用Spring中提供的过滤器来解决,它能够让Session在请求解释完成之后再关闭,web.xml配置方式如下:

<!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 ,用来解决could not initialize proxy - no Session报错问题-->
    <filter>
        <filter-name>lazyLoadingFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
      
      <!--如果还报错(Write operations are not allowed in read-only mode )则加入init-param值为false即可解决:
   -->
    <init-param>
    <param-name>singleSession </param-name>
    <param-value>false </param-value>
   </init-param>
       
       
    </filter>
   
<filter-mapping>
    <filter-name>lazyLoadingFilter</filter-name>
    <url-pattern>*.action</url-pattern>
    </filter-mapping>
----------------------------
报错:Illegal attempt to associate a collection with two open sessions;
解决:

假若你的web.xml中配置了hibernate的OpenSessionInViewFilter的filter那么去掉后,在spring 中这样配置:
<bean name="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="flushMode" value="1"/>
<property name="singleSession" value="false" />
</bean>
   
   
-------------------

9、因为OpenSessionInViewFilter在getSession的时候,会把获取回来的session的flush mode 设为FlushMode.NEVER。故进行insert、 update和delete操作时会产生异常:org.springframework.dao.InvalidDataAccessApiUsageException: Write operations are not allowed in read-only mode (FlushMode.NEVER/MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or remove 'readOnly' marker from transaction definition. 因此需要采用spring的事务声明,使方法受transaction控制:

   <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

<!-- 配置事务管理器 -->
<bean id="transactionManager"
   class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置Advice(事务的传播特性) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
   <tx:attributes>
    <tx:method name="add*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
    <tx:method name="search*" propagation="SUPPORTS" read-only="true" />
   </tx:attributes>
</tx:advice>
<!-- 配置事务管理器应用的范围 -->
<aop:config>
   <aop:pointcut id="affectMethods"
    expression="execution(* lk.edu.tms.*.*(..))" />
   <aop:advisor advice-ref="txAdvice" pointcut-ref="affectMethods" />
</aop:config>
-------------------------

--------------------------------

tiles框架的应用:(tiles就如同html中的frameset框架)

layout.jsp:------------------模板(框架分为上下左中右)
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<html>
<body topmargin="0" leftmargin="0"> <!-- topmargin等用来去除页面上下左右的空隙 -->
<table border="1" bordercolor="red" width="100%" height="100%" >
<tr height="20%"><td colspan="3"><tiles:insert attribute="header"></tiles:insert> <!-- 框架模板 -->
</td>
</tr>

<tr height="70%">
<td width="20%"><tiles:insert attribute="left"></tiles:insert></td>

<td width="60%"><tiles:insert attribute="center"></tiles:insert></td>

<td width="20%"><tiles:insert attribute="right"></tiles:insert></td>
</tr>

<tr height="10%">
<td colspan="3"><tiles:insert attribute="footer"></tiles:insert></td>
</tr>

</table>
</body>
</html>


main.jsp:-----------显示页面
<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<tiles:insert page="layout.jsp" flush="true">

<tiles:put name="header" value="header.jsp"></tiles:put>   <!-- 具体页头页面 -->
<tiles:put name="left" value="left.jsp"></tiles:put>
<tiles:put name="center" value="center.jsp"></tiles:put>
<tiles:put name="right" value="right.jsp"></tiles:put>
<tiles:put name="footer" value="footer.jsp"></tiles:put> <!-- 具体页尾页面 -->
</tiles:insert>
-----------------------------------------------------------

----------------------------------------------

hbm.xml的手动创建方法(这种创建方法可以在写代码时按Alt+/有提示):
New/other/XML(Basic Templates)/命名/create...DTD file/select XML Catalog entry/Hibernate Mapping DTD 3.0//EN


----------------------------

struts2配置中Action的name 和package的name和namespace是什么作用
      09:26在struts2 struts.xml 配置中
      <struts>
      <package name="caixuedong" extends="struts-default" namespace="/core">
      <action name="greeting" class="com.tarena.struts2.action.GreetingAction" >

      <result name="success">/core/greeting.jsp </result>
      </action>
      </package>
      </struts>


      name="caixuedong"
      namespace="/core"
      name="greeting"
      这三个怎么去理解啊
      ----------------------------
      1.package中的name只是一个标识,你可以随意命名;
      2.action中的name对应你页面中的一个action跳转,比如你页面有个表单提交: <form
      action="struts.xml中action里的name">
      3.namespace是用于区分包中相同的类,如果没写,说明该包为默认空间,如果写了(如你的这个例子),那么就说明包caixuedong的空间为/core。

      namespace实际上是在包的基础上对Action的进一步组织和划分,可以解决Action重名问题,因为在不同的命名空间中是可以有相同的Action名的,就好比一班有个叫张三的,二班也有个叫张三的,当我们呼叫某个张三时,就得带上“命名空间”,Action也是如此,例子的greeting就必须通过
      /core/greeting.action的方式来进行调用。

      greeting那个name就是Action的名字,相当于上个例子的“张三”


----------------------------------------------------------------oracle创建表空间

cmd-----C:\Users\user>sqlplus / as sysdba
(上面不能进就用这个:cmd-----sqlplus sys/tower@orcl as sysdba)
SQL> create user tower identified by tower;

SQL> grant create table to tower;

授权成功。

SQL> create tablespace towertablespace datafile 'E:\oracle\product\10.2.0\oradata
\orcl\towerts.dbf' size 1000m;

表空间已创建。

SQL> alter user tower default tablespace towertablespace;

用户已更改。

SQL> alter user tower quota unlimited on towertablespace;

用户已更改。

SQL>
------------------------------------purge recyclebin清空oracle回收站

当表的名字变成不可识别的长串字符时,有可能是此表已经被删除,但还能看到,用SQL>purge recyclebin;清空回收站即可彻底删除此表。


----------------------------------------查询一个表中的所有行,并输出到jsp页面
要查询一个表中的所有行,List findAll(),返回List集合,在Action类中使用ActionContext.getContext().put("y",list)--(request??只在一次请求中有效)将list集合放入y中,然后在另外一个jsp页面中取得此list,然后使用<s:iterator><s:property>来迭代输出此list到页面上。
示例:
ActionContext.getContext().put("groupUserList", list);//Action类中写,//ActionContext.getContext().getSession().put("groupUserList", list);写此句不能查到数据  
<s:iterator id="group" status="index" value="groupUserList"> //jsp页面中获取值并输出
<s:property id="group" value="(联合主键生成类的id)id.tdSaUsergroup.gid" />//如果想将此值显示在文本框中,这样写<s:textfield label="密码" value="%{id.tdSaUsergroup.gid}"></s:textfield>
<s:property id="group" value="id.tdSaUser.id" />

</s:iterator>
     
----------------------------------jsp页面上从一个action中取得了数据,但另一Action要用到此值(如登录后显示id,但又要根据此id将此人删除,id不需要再手动输入)
jsp页面:
<%String uid = (String) request.getAttribute("uid");%>
工号:<%=uid%>
<input type="hidden" name="uid" value="<%=uid%>">

//action中:
ServletActionContext.getRequest().getParameter("uid");

//////////////////////////////////////
action中保存从数据查到的List:ActionContext.getContext().put("userList", u);
jsp页面上获取并隐藏List中的uid:
<%
TdSaUser user=(TdSaUser) ActionContext.getContext().get("userList");
String uid=user.getId();
%>
<input name="uid" type="hidden" value="<%=uid%>" >
然后再在另一个action获取此uid:
String uid = (String) ServletActionContext.getRequest().getParameter("uid");// 获取参数(用户ID工号)

------------------------------怎样在框架集上点击一个链接同时改变两个框架

框架集分上(top),左(left),右(main)三部分,现在要求点击top上的链接时同时改变left和main的内容.

A:在left的Body中插入以下代码:

<body onload="window.open('x x x.html','mainFrame')"></body>

------------------------------------在jsp页面中获取session中的List

Action中:list里是userGroup类
ServletActionContext.getRequest().getSession().setAttribute("userGroupList", list);
JSP页面中:
<s:iterator id="group" status="index" value="#session.userGroupList">
<s:property id="group" value="gid" />
<s:property id="group" value="name" />
</s:iterator>
---------------------
Action中: "user"存的是对象,它可以包含id,name...属性
ActionContext.getContext().put("user", user);
jsp页面中:
<s:textfield label="姓名" name="name" value="%{#session.user.name}"></s:textfield>
------------------------------------------------------
Action中:List里存只有类的属性userId(String)
ServletActionContext.getRequest().getSession().setAttribute("userIdList", list);
JSP页面:属性通过id显示出来
<s:iterator id="ids" value="#session.userIdList">
<s:property value="ids" />
</s:iterator>
  
----------------------------------List放入s:select下拉列表中
Action类中:一个表dept中放了部门id,name
List deptList=iusermagservice.getDept();//查询获得表中数据返回一个List
ServletActionContext.getRequest().setAttribute("deptList", deptList);//将List存入request中
 
JSP页面中:将List中的id,name显示在下拉列表中
<s:select label="部门" name="dept" list="#request.deptList"
      listKey="id" listValue="name" >       
</s:select>
listKey最好写与listvalue相同,因为向数据库中插入的是listkey的值,jsp页面上显示的是listvalue的值
----------------------------------------
通过选择下拉菜单(工号,姓名),并输入工号或姓名来登录:

JSP页面中:
<s:select name="userSearchType" list="#{'gonghao':'工号','xingming':'姓名'}"></s:select>
Action中:   
ServletActionContext.getRequest().getParameter("userSearchType")----得到值:gonghao,xingming

------------------------------写validation.xml校验文件
Action类中写了很多方法,校验文件命名为:xxxAction-方法对应的action name-validation.xml
Action类只有execute()方法,校验文件命名为:xxxAction-validation.xml

1.校验文件写好与Action类放在同一文件夹下;
2.jsp页面中可写<s:fielderror/>集中显示校验出错提示
3.在struts.xml文件中的action result中写出错时返回的输入页面<result name="input">/mgr/showUserInfo.jsp</result>

------------------加上校验后,表格消失了
当使用校验时,若有两个提交按钮对应2个Action,此时<s:form>中不需写action=""也可提交页面,但会出现没有表格约束的界面,比较丑,加上<s:form action="">中的action=""就会出现表格了
<body>
<s:fielderror/>
<s:form action="">
<table>
<tr>
   <td colspan="2" align="center">
   <s:submit value="修改提交" action="UpdateUser" theme="simple"></s:submit>
   <s:submit value="删除此用户" action="DelUser" theme="simple"></s:submit>
   <a href="javascript:history.back();">返回</a>
</td>
</tr>
</table>
</s:form>
</body>
--------------------------------
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics