• struts2+hibernate4+spring3+maven搭建项目:简单账务管理系统第二篇:struts+spring搭建


    2.3 struts+spring搭建与之前搭建的hibernate整合。

    目标:把struts和hibernate的bean(包括action)都交给spring管理,依赖注入,即每一个javabean都对应配置文件中的一个bean,bean之间的关系由配置文件来维护。

    先写web.xml文件:

    View Code
     1   <?xml version="1.0" encoding="UTF-8" ?> 
     2 -<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">
     3 - <context-param>
     4   <param-name>contextConfigLocation</param-name> 
     5   <param-value>classpath:applicationContext.xml,/WEB-INF/struts-beans.xml,/WEB-INF/SeviceContext.xml</param-value> 
     6   </context-param>
     7 - <listener>
     8   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
     9   </listener>
    10 - <filter>
    11   <filter-name>struts2</filter-name> 
    12   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 
    13   </filter>
    14 - <filter-mapping>
    15   <filter-name>struts2</filter-name> 
    16   <url-pattern>/*</url-pattern> 
    17   </filter-mapping>
    18 - <welcome-file-list>
    19   <welcome-file>index1.jsp</welcome-file> 
    20   <welcome-file>sum.jsp</welcome-file> 
    21   </welcome-file-list>
    22   </web-app>

    applicationContext.xml(其实就是前面的daoContext.xml了,作为hibernate层的配置文件):

    View Code
      1   <?xml version="1.0" encoding="UTF-8" ?> 
      2 - <!--   Licensed to the Apache Software Foundation (ASF) under one or more
      3   contributor license agreements.  See the NOTICE file distributed with
      4   this work for additional information regarding copyright ownership.
      5   The ASF licenses this file to You under the Apache License, Version 2.0
      6   (the "License"); you may not use this file except in compliance with
      7   the License.  You may obtain a copy of the License at
      8 
      9       http://www.apache.org/licenses/LICENSE-2.0
     10 
     11   Unless required by applicable law or agreed to in writing, software
     12   distributed under the License is distributed on an "AS IS" BASIS,
     13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14   See the License for the specific language governing permissions and
     15   limitations under the License.
     16 
     17   --> 
     18 - <!--  @version $Id: applicationContext.xml 561608 2007-08-01 00:33:12Z vgritsenko $ 
     19   --> 
     20 - <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
     21 - <!-- 定义数据源,该bean 的ID 为dataSource
     22   --> 
     23 - <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     24 - <!--  指定数据库驱动
     25   --> 
     26 - <property name="driverClassName">
     27   <value>com.mysql.jdbc.Driver</value> 
     28   </property>
     29 - <!--  指定连接数据库的URL
     30   --> 
     31 - <property name="url">
     32   <value>jdbc:mysql://127.0.0.1:3306/test</value> 
     33   </property>
     34 - <!--  root 为数据库的用户名
     35   --> 
     36 - <property name="username">
     37   <value>root</value> 
     38   </property>
     39 - <!--  pass 为数据库密码
     40   --> 
     41 - <property name="password">
     42   <value /> 
     43   </property>
     44   </bean>
     45 - <!--  定义Hibernate 的SessionFactory
     46   --> 
     47 - <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
     48 - <!--  依赖注入数据源,注入正是上文定义的dataSource
     49   --> 
     50 - <property name="dataSource">
     51   <ref local="dataSource" /> 
     52   </property>
     53 - <!--  mapp工ngResouces属性用来列出全部映射文件
     54   --> 
     55 - <property name="mappingResources">
     56 - <list>
     57 - <!--  以下用来列出所有的PO 映射文件
     58   --> 
     59 - <!--   <value>lee/MyTest.hbm.xml</value>
     60   --> 
     61   <value>test.hbm.xml</value> 
     62   </list>
     63   </property>
     64 - <!--  定义Hibernate 的SessionFactory的属性
     65   --> 
     66 - <property name="hibernateProperties">
     67 - <props>
     68 - <!--  指定Hibernate 的连接方法
     69   --> 
     70   <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> 
     71 - <!--  不同数据库连接,启动时选择create , update , create-drop
     72   --> 
     73   <prop key="hibernate.hbm2ddl.auto">update</prop> 
     74   </props>
     75   </property>
     76   </bean>
     77 - <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
     78 - <property name="sessionFactory">
     79   <ref local="sessionFactory" /> 
     80   </property>
     81   </bean>
     82 - <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
     83 - <property name="transactionManager">
     84   <ref local="transactionManager" /> 
     85   </property>
     86 - <property name="transactionAttributes">
     87 - <props>
     88   <prop key="insert*">PROPAGATION_REQUIRED</prop> 
     89   <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> 
     90   <prop key="*">PROPAGATION_REQUIRED</prop> 
     91   </props>
     92   </property>
     93   </bean>
     94 - <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
     95 - <property name="beanNames">
     96 - <list>
     97   <value>personDao</value> 
     98   <value>userDao</value> 
     99   <value>addressDao</value> 
    100   <value>marketDao</value> 
    101   </list>
    102   </property>
    103 - <property name="interceptorNames">
    104 - <list>
    105   <value>transactionInterceptor</value> 
    106   </list>
    107   </property>
    108   </bean>
    109   <bean id="helper" class="com.kc.reign.util.ReflectHelper" /> 
    110 - <bean id="personDao" class="com.medclub.tests.daoimpl.TestHibernateDao">
    111 - <constructor-arg index="0">
    112   <ref bean="sessionFactory" /> 
    113   </constructor-arg>
    114 - <property name="helper">
    115   <ref local="helper" /> 
    116   </property>
    117   </bean>
    118 - <bean id="userDao" class="com.medclub.tests.daoimpl.UserHibernateDao">
    119 - <constructor-arg index="0">
    120   <ref bean="sessionFactory" /> 
    121   </constructor-arg>
    122 - <property name="helper">
    123   <ref local="helper" /> 
    124   </property>
    125   </bean>
    126 - <bean id="addressDao" class="com.medclub.tests.daoimpl.AddressDaoImpl">
    127 - <constructor-arg index="0">
    128   <ref bean="sessionFactory" /> 
    129   </constructor-arg>
    130 - <property name="helper">
    131   <ref local="helper" /> 
    132   </property>
    133   </bean>
    134 - <bean id="marketDao" class="com.medclub.tests.daoimpl.MarketDaoImpl">
    135 - <constructor-arg index="0">
    136   <ref bean="sessionFactory" /> 
    137   </constructor-arg>
    138 - <property name="helper">
    139   <ref local="helper" /> 
    140   </property>
    141   </bean>
    142   </beans>

    struts.xml

    View Code
     1   <?xml version="1.0" encoding="UTF-8" ?> 
     2   <!DOCTYPE struts (View Source for full doctype...)> 
     3 - <struts>
     4   <constant name="struts.objectFactory" value="spring" /> 
     5 - <package name="default" namespace="/fortest" extends="struts-default">
     6 - <action name="sum" class="FirstAction" method="execute">
     7   <result name="positive">/positive.jsp</result> 
     8   <result name="negative">/negative.jsp</result> 
     9   </action>
    10 - <!--   
    11      <action name="testCal">
    12     <result name="index1">/index1.jsp</result>    
    13    </action>
    14   --> 
    15 - <action name="populateAddress" class="AddressAction">
    16   <result name="success">/index1.jsp</result> 
    17   </action>
    18   </package>
    19   </struts>

    关键代码

    View Code
    1 <constant name="struts.objectFactory" value="spring" /> 

    这些代码表示定义ObjectFactory常量,表示由spring来接管struts的bean那么就要有额外来配置struts的beans的文件,即web.xml中 的struts-beans.xml:

    struts-beans.xml:

    View Code
     1   <?xml version="1.0" encoding="UTF-8" ?> 
     2 - <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
     3 - <!--  <aop:config proxy-target-class="true"/> 
     4   --> 
     5 - <!--  <bean name="testCal" class="com.medclub.action.actionimpl.TestCal"></bean> 
     6   --> 
     7   <bean name="FirstAction" class="com.reign.action.FirstAction" /> 
     8 - <bean name="AddressAction" class="com.medclub.struts.action.actionimpl.AddressAction">
     9 - <property name="addressService">
    10 - <!--   <bean class="com.medclub.service.impl.AddressServiceImpl"></bean>
    11   --> 
    12   <ref bean="addressService" /> 
    13   </property>
    14   </bean>
    15   </beans>

    这里有可能会报异常ClassCastException因为spring默认采用JDK动态代理,而我们定义的继承自ActionSurport的action类是一个再没有子类的类了因此需要cglib代理来处理(jdk代理面向接口编程,而CGLIB代理更加灵活)

    如下配置为解决代理的问题:配置在beans下面,且classpath中必须有cglib2.1的jar包
    <aop:config target-class="true"/>(实际上,经过测试,发现只要把cglib2.1的包放在classpath中就不会报异常了。)详见:

    http://www.cnblogs.com/hustyangli/archive/2008/09/01/1281319.html(感谢园友的給力文章。)

    还有一点要注意就是命名空间,这里也顺便提一下,(资料链接丢失了,就感谢那个让我茅塞顿开的雷锋园友啦~~)

     命名空间:在使用org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    org.springframework.web.context.ContextLoaderListener架构struts时,而且在struts.xml中配置文件如下时:
    <constant name="struts.objectFactory" value="spring" />
    由spring来管理struts的action类,而action类
    <package name="default" namespace="/fortest" extends="struts-default">
    <action name="populateAddress" class="AddressAction">
      <result name="success">/index1.jsp</result>
      </action>
      </package>
    package元素表示包名,namespace必须要给出,因为如不给出就是继承了struts-default的命名空间,而这个命名空间没有我们要运行的action,action的名称为类名,在struts-beans或者其他的文件中定义name="ooxx..."与struts.xml文件内的action名称需要相同
    注意:struts.xml中action的class属性要与struts-beans.xml的bean的name属性相同

    下面就是配置action类和写jsp页面了。先测试struts+spring架构是否可以顺利运行:

    action类:

    View Code
     1 package com.reign.action;
     2 
     3 import com.opensymphony.xwork2.ActionSupport;
     4 
     5 public class FirstAction extends ActionSupport {
     6 
     7     /**
     8      * 
     9      */
    10     private static final long serialVersionUID = 1L;
    11     private int opprand1;
    12     private int opprand2;
    13     public String execute() throws Exception
    14     {
    15         if(getSum()>0)
    16         {
    17             return "positive";
    18         }
    19         else{
    20             return "negative";
    21         }
    22     }
    23     public int getOpprand1() {
    24         return opprand1;
    25     }
    26     public void setOpprand1(int opprand1) {
    27         this.opprand1 = opprand1;
    28     }
    29     public int getOpprand2() {
    30         return opprand2;
    31     }
    32     public void setOpprand2(int opprand2) {
    33         this.opprand2 = opprand2;
    34     }
    35     public static long getSerialversionuid() {
    36         return serialVersionUID;
    37     }
    38     public int getSum()
    39     {
    40         return opprand1+opprand2;
    41     }
    42 }

    jsp页面sum.jsp:

    View Code
     1 <%@ page language="java" contentType="text/html; charset=gb2312"
     2     pageEncoding="gb2312"%>
     3  <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 <s:form action="sum" namespace="/fortest">
    12 <s:textfield name="opprand1" label="操作数1"/>
    13 <s:textfield name="opprand2" label="操作数2"/>
    14 <s:submit value="代数和"/>
    15 </s:form>
    16 </body>
    17 </html>

    positive.jsp:

    View Code
     1 <%@ page language="java" contentType="text/html; charset=gb2312"
     2     pageEncoding="gb2312"%>
     3     <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 代数和为非负整数<h1><s:property value="sum"/></h1>
    12 </body>
    13 </html>

    negative.jsp:

    View Code
     1 <%@ page language="java" contentType="text/html; charset=gb2312"
     2     pageEncoding="gb2312"%>
     3     <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     8 <title>Insert title here</title>
     9 </head>
    10 <body>
    11 代数和为负整数<h1><s:property value="sum"/></h1>
    12 </body>
    13 </html>

    然后run as->maven build,goal选择tomcat:run跑起来后在网页里输入:http://localhost:8080/test/sum.jsp
    测试,成功,struts+spring整合完毕,前面已经整合好struts+hibernate,因此架构算是搭好啦,至于业务,后面再更新,可能会慢点,边想边做吧。

  • 相关阅读:
    JZ36 两个链表的第一个公共结点
    程序员的表达能力
    Git学习(2)-使用Git 代码将本地文件提交到 GitHub
    初识模块
    三元表达式、递归、匿名函数
    CSRF
    XSS前置课程--同源策略
    XSS
    SQL注入基础入门
    Linux下ettercap的安装,make安装软件步骤
  • 原文地址:https://www.cnblogs.com/saajireign/p/sheyeless.html
Copyright © 2020-2023  润新知