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="">
<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的更多相关文章
- spring整合struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jarspring-webmvc-4.0.0.RELEASE ...
- Spring 整合 Struts2
1. Spring 如何在 WEB 应用中使用 ? 1). 需要额外加入的 jar 包: spring-web-4.0.0.RELEASE.jar spring-webmvc-4.0.0.RELEAS ...
- Struts2的使用以及Spring整合Struts2
一.如何单独使用Struts2 (1)引入struts2的jar包 commons-fileupload-1.2.1.jar freemarker-2.3.15.jar ognl-2.7.3.jar ...
- Spring学习6-Spring整合Struts2
一.Spring为什么要整合Struts2 Struts2与Spring进行整合的根本目的就是要让 Spring为Struts2的Action注入所需的资源对象,它们整合的原理则是只要导入了s ...
- 基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包自动装配无效
基于注解整合struts2与spring的时候如果不引入struts2-spring-plugin包,自动装配将无效,需要spring注入的对象使用时将抛出空指针异常(NullPointerExcep ...
- Spring(四):Spring整合Hibernate,之后整合Struts2
背景: 上一篇文章<Spring(三):Spring整合Hibernate>已经介绍使用spring-framework-4.3.8.RELEASE与hibernate-release-5 ...
- Maven项目整合Struts2框架
-------------------------siwuxie095 Maven 项目整合 Struts2 框架 1. ...
- 整合Struts2框架和Spring框架
-----------------------siwuxie095 整合 Struts2 框架和 Spring 框架 1 ...
- Spring整合Struts2框架的第二种方式(Action由Spring框架来创建)(推荐大家来使用的)
1. spring整合struts的基本操作见我的博文:https://www.cnblogs.com/wyhluckdog/p/10140588.html,这里面将spring与struts2框架整 ...
随机推荐
- Android---63---Android中的动画效果
Android中有四种动画效果: AlphaAnimation:透明度动画效果 ScaleAnimation:缩放动画效果 TranslateAnimation:位移动画效果 RotateAnimat ...
- Centos7 安装 Maven 3.5.*
下载 Apache Maven 访问 Maven官方网站,打开后找到下载链接,如下: 解压 tar zxvf apache-maven-3.5.3-bin.tar.gz 添加环境变量 打开 /etc/ ...
- iPhone换电池是原装电池好还是换第三方大容量电池好?
转:https://www.xianjichina.com/news/details_60791.html 最近这段时间苹果降速门事件持续发酵,闹得满城风雨.尽管苹果公司两次致歉,很多果粉都去更换电池 ...
- STM32单片机和51单片机区别
单片机 / AVR / PIC / STM32 / 8051803189C5189S51 6905 单片机简介 单片微型计算机简称单片机,简单来说就是集CPU(运算.控制).RAM(数据存储-内存). ...
- WINDOWS下的squid
今天写这篇教程目的在于分享自己在WINDOWS主机下配置squid的方法.哪些地方写的不完善或是不完整或是需要修改的地方,大家可以提出.我会第一时间纠正.下面看正文部分.先提条件,您预安装配置squi ...
- Msfvenom 学习笔记与总结
平台:Android,可用Payload: android/meterpreter/reverse_http Run a meterpreter server on Android. Tunnel c ...
- JavaWeb学习笔记:Servlet
Servlet JavaWeb 概念 Java Web应用由一组Servlet.HTML页面.类.以及其他能够被绑定的资源构成. 他能够在各种供应商提供的实现Servlet规范的Servlet容器中执 ...
- Hibernate demo之使用注解
1.新建maven项目 testHibernate,pom.xml <?xml version="1.0" encoding="UTF-8"?> & ...
- sharepoint 2013 资源管理器copy大文件到本地失败解决方法
Error 0x800700DF: The file size exceeds the limit allowed and cannot be saved 中文错误信息是:文件大小超出同意范围.不能被 ...
- oracle 推断字符是否为字母
create or replace function ischar(chr varchar2) return varchar2 is ischr varchar2(5); begin sele ...