spring2.5整合struts2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring2.5整合struts2相关的知识,希望对你有一定的参考价值。

首先第一步:

导入jar包:

我的做法:

  导入你的基本使用的spring的jar包

  和基本使用的struts2的jar包

  然后struts2中有一个和spring整合的jar包一定要导入,不然会抛异常。包名是这个:struts2-spring-plugin-2.3.30.jar

 

在web.xml中装载spring容器:

   <!-- 装载spring容器 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:beans.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>  

 

在web.xml中装载struts2:

<!-- 装载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>

 

然后在struts2的配置文件中加入常量声明action交给spring管理:

<!-- 将action交给spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>

 

当action交给spring管理之后,我们在action中的class属性可以写成spring容器中的bean的标识:

    <package name="person" namespace="/person" extends="struts-default">
        
        <!-- 这个地方的class属性变成了spring中的bean的标识 -->
        <action name="person_*" class="personAction" method="{1}">
            <result name="message" type="redirectAction">
                <param name="actionName">person_findAll</param>
                <param name="namespace">/person</param>
            </result>
            <result name="list">/person/personList.jsp</result>
        </action>
        
    </package>

 

其他地方正常开发。

 

此例子为ssh2整合的。

补充代码:

技术分享
<?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:context="http://www.springframework.org/schema/context"
       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/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
           
    <context:component-scan base-package="cn.itcast"/>
         
    <context:property-placeholder location="classpath:jdbc.properties"/>  
    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClassName}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>
        <property name="minPoolSize" value="${jdbc.minPoolSize}"/>
        <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>
        <property name="maxIdleTime" value="${jdbc.maxIdleTime}"/>
        <property name="acquireIncrement" value="${jdbc.acquireIncrement}"/>
    </bean>       
    
    <!-- 定义sessionFactory对象 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="mappingResources">
            <list>
                <value>cn/itcast/domain/Person.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.mysqlDialect
                hibernate.hbm2ddl.auto=update
                hibernate.show_sql=true
            </value>
        </property>
    </bean>
    
    <!-- 注册事务管理器 -->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    
    <!-- 注解管理事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>
    
</beans>
beans.xml
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  
  

    <!-- 装载spring容器 -->
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:beans.xml</param-value>
  </context-param>
  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>  
     
  <!-- spring的openSessionInView过滤器 -->
  <filter>
      <filter-name>openSessionInView</filter-name>
      <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>openSessionInView</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
  
  <!-- 装载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>
  
 
  
  <display-name></display-name>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  
</web-app>
web.xml
技术分享
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>

    <constant name="struts.action.extension" value="do"/>
    <constant name="struts.configuration.xml.reload" value="true"/>
    <constant name="struts.devMode" value="true" />
     
    <!-- 将action交给spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>

    <include file="cn/itcast/struts_person.xml"/>
   
</struts>
struts.xml
技术分享
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="person" namespace="/person" extends="struts-default">
        
        <!-- 这个地方的class属性变成了spring中的bean的标识 -->
        <action name="person_*" class="personAction" method="{1}">
            <result name="message" type="redirectAction">
                <param name="actionName">person_findAll</param>
                <param name="namespace">/person</param>
            </result>
            <result name="list">/person/personList.jsp</result>
        </action>
        
    </package>
    
</struts>
struts_person.xml
技术分享
package cn.itcast.web.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import cn.itcast.domain.Person;
import cn.itcast.service.PersonService;

import com.opensymphony.xwork2.ActionSupport;
@Controller
public class PersonAction extends ActionSupport {
    private String message;
    private List<Person> persons;
    private String name;
    
    @Resource private PersonService personService;
    
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    public List<Person> getPersons() {
        return persons;
    }
    public void setPersons(List<Person> persons) {
        this.persons = persons;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    
    public String findAll(){
        this.persons=personService.getPersons();
        return "list";
    }
    
    public String add(){
        personService.save(new Person(name));
        this.message="增加成功!";
        return "message";
    }
    
    
}
personAction.xml
技术分享
package cn.itcast.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.domain.Person;
import cn.itcast.service.PersonService;
@Transactional @Service("personService")
public class PersonServiceBean implements PersonService {
    
    @Resource(name="sessionFactory")
    private SessionFactory sessionFactory;
    
    public void save(Person person){
        //spring会自动帮我们管理session
        sessionFactory.getCurrentSession().persist(person);
    }
    
    public void update(Person person){
        sessionFactory.getCurrentSession().merge(person);
    }
    
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public Person getPerson(Integer personId){
        return (Person) sessionFactory.getCurrentSession().get(Person.class, personId);
    }
    
    public void delete(Integer personId){
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().load(Person.class, personId));
    }
    
    @SuppressWarnings("unchecked") @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public List<Person> getPersons(){
        return sessionFactory.getCurrentSession().createQuery("from Person").list();
        
    }
}
personServiceBean.java
技术分享
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title>My JSP personList.jsp starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
      
    <s:iterator value="persons" var="person">
        ID=<s:property value="#person.id"/>,Name=<s:property value="#person.name"/>
    </s:iterator>
  </body>
</html>
personList.jsp

 

 

我整合ssh的时候出现了get方式提交中文乱码问题,试了过滤器的方式无法解决,谁看到的有方法的回复下,谢谢。要求不修改tomcat的server.xml

以上是关于spring2.5整合struts2的主要内容,如果未能解决你的问题,请参考以下文章

Struts2.1.6 + Spring2.5+Hibernate3.2整合

OA学习笔记-006-SPRING2.5与hibernate3.5整合

spring2.5+hibernate3.2+struts2.0插入数据时调用两次sql语句

低代码开发平台都有哪些?

kinderEditor + Struts2整合

struts2+hibernate整合开发步骤