Spring 整合Structs
来源:互联网 发布:网络老虎机爆大奖秘诀 编辑:程序博客网 时间:2024/05/31 19:46
1 在web.xml 中做Spring的和Structs2 的基本配置
<?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"> <display-name></display-name> <!-- 配置Structs2 的核心过滤器 --> <filter> <filter-name>structs2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>structs2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:beans.xml <!-- Spring 配置文件所在的位置classPath表示Src 下的路径 --> <!-- 可以配置多个 /WEB-INF/conf/conf.xml 用“,”隔开 表示在/WEB-INF/conf /conf.xml的路径下 --> </param-value> </context-param> <listener> <!-- 用Spring的contextLoaderListener 在项目启动时就加载Spring的配置文件 --> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>2 创建action
public class StudAction extends ActionSupport{ private IStudService service; private String name; public String getName() {return name;}public void setName(String name) {this.name = name;}public IStudService getService() {return service;}public void setService(IStudService service) {this.service = service;}public String execute() throws Exception{ System.err.println("这个是AtudAction"+getName()); service.save(); return SUCCESS; }}
3 配置创建Structs2的类工厂
package com.wuyihuai.factory;import java.util.Map;import javax.servlet.ServletContext;import org.apache.struts2.ServletActionContext;import org.springframework.context.ApplicationContext;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ObjectFactory;/** * 让Structs2 不去创建action 而是去Spring中取需要的Action * */public class MyObjectFactory extends ObjectFactory{@Overridepublic Object buildBean(Class cls, Map<String, Object> ctx)throws Exception {// TODO Auto-generated method stubSystem.err.println("创建了。。。。。。"+cls+""+ctx);//判断cls 是不是Action 的孩子 如果不断地的话由于struct的过滤器在Spring的bean.xml中找不到会报错if(Action.class.isAssignableFrom(cls)){ //是Action Struct2不创建,由Spring的 创建ServletContext cs=ServletActionContext.getServletContext();//此类是以经封装好的beans.xml中的对象数据//ApplicationContext appCtx=(ApplicationContext) cs.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); ApplicationContext appCtx=WebApplicationContextUtils.getWebApplicationContext(cs);Object o=appCtx.getBean(cls);return o;}else{ //不是Action 就有Struct2自己创建 return super.buildBean(cls, ctx);}} }4 配置Struct2.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts> <!-- 配置自己的类工厂 --> <bean type="com.opensymphony.xwork2.ObjectFactory" name="wuyihuai" class="com.wuyihuai.factory.MyObjectFactory" /> <constant name="struts.objectFactory" value="wuyihuai" ></constant> <!-- 配置Action类 --> <package name="wuyihuai" extends="struts-default"> <action name="stud" class="com.wuyihuai.stud.StudAction"> <result>/index.jsp</result> </action> </package></struts>
5 配置Spring.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"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-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置action bean --> <bean id="studAction" class="com.wuyihuai.stud.StudAction" scope="prototype"> <property name="service" ref="studService"></property> </bean> <!-- 声明自己的dao和service --> <bean id="studService" class="com.wuyihuai.service.StudServiceImpl"> <property name="dao"> <bean class="com.wuyihuai.dao.StudDaoJdbc"></bean> </property> </bean></beans>
0 0
- Structs 整合Spring
- Spring 整合Structs
- Structs+Spring整合所需(笔记)
- Spring+Hibernate+Structs整合所需具体jar包
- Spring+Hibernate+Structs三大经典开源框架整合
- structs整合ireport(japser)
- EJB spring hibernate structs
- SSH(Structs Spring Hibernate)
- spring hibernate structs
- structs+spring+hibernate随笔
- 关于structs和Spring框架整合中的模块装配问题心得
- structs+spring+ibatis研究开始
- Structs+Spring+Hibernate快速入门
- Structs+Spring+Hibernate快速入门
- Structs+Spring+Hibernate快速入门
- Structs+Spring+Hibernate快速入门
- structs
- structs
- 使用Nexus搭建Maven私服
- SSH进阶(1)——javax.mail 实现邮件发送
- 畅通工程续
- c# 复制文件后新的文件变成只读文件
- mysql 数据库 如何获取刚刚插入的自增长的id号
- Spring 整合Structs
- 数据库事务
- Kettle——表输出步骤异常处理方式探究
- ibatis 架构
- 3 Maven-POM文件说明
- Spring (五) 声明式事务
- 设置软件启动界面图片(Xcode 7.2)
- 抽象工厂模式
- HDU 1207 汉诺塔II