以下介绍几个配置部分的配置,这几个部分配置好了,也就可以了,先上工程图
一、配置web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>*.action</url-pattern>
</filter-mapping>
以上主要配置spring为随着服务器启动而自启动,配置struts2过滤器,配置spring配置文件的位置
二、action类代码
public class LoginAction {
LoginService loginService;
public LoginService getLoginService() {
return loginService;
}
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
public String execute() throws Exception {
loginService.hello();
return "success";
}
}
LoginService 为要注入的对象,需有set方法
三、spring配置文件beans.xml
<bean id="loginService" class="com.ssh2.serviceimpl.LoginServiceImpl"></bean>
<bean id="login" class="com.ssh2.action.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"/>
</bean>
这里名为login的bean是struts2中的action,由spring负责action的创建,在该action中
设置loginService属性
四、struts2的配置文件
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<include file="struts-default.xml"></include>
<package name="" extends="struts-default" namespace="/mm">
<action name="login" class="login">
<result name="success">/success.jsp</result>
</action>
</package>
以上指定了对象工厂为StrutsSpringObjectFactory,这样struts2的action就不再又struts2而是由spring
负责产生了
另外action元素中的class属性不再指向其实际的class,而是指向beans.xml中某个action bean的id
测试:http://localhost:8080/SS2H/mm/login.action
这样就完成了两者的整合
另外要注意的是要导入好所需要的jar包,比如struts2的spirng插件包,spring的web包
分享到:
相关推荐
Spring整合Struts2 SSH 最少jar包
三大框架集成及两两集成的jar包,spring集成hibernate的jar包以及spring集成struts2的jar包已经单独打包
spring整合struts2与hibernate核心配置文件
Spring 整合 Struts2 详解
SSH笔记-Spring整合Struts2,作用是使用 IOC 容器来管理 Struts2 的 Action
只使用spring和struts进行整合
深层次理解Spring整合Struts2开发原理,更有效地组织开发过程
一个spring整合struts2的完整实例源码
Spring 整合 Struts,很强大
使用Spring集成struts2、ibatis、poi实现的增删改查功能,包括采用jquery实现的无刷新查询机分页、dwr实现的两级联动、以及采用poi动态将数据库数据导出成excel,本demo采用mysql数据库,附有建表sql,项目导入...
里面有spring+struts2+ibatis整合的jar包,
spring与struts2整合的小示例,适合初学者,具体说明在博客里
Spring整合集成Struts1.2最简单例子
Java:SSH模式-Spring整合struts2以及hibernate框架,导入直接可用,节省开发时间,十分方便,也可供参考、学习。已经配置好依赖等基础,内有增删改查的例子。
使用Spring整合Struts编写多页面用户注册模块 使用Spring整合Struts编写多页面用户注册模块
基于注解的Spring+Struts2+Mybatis+Maven项目 Spring3.0
SSH+Redis整合
spring-struts2整合详细步骤。适合初学者参考。内容详尽。