申明 要Spring+Strut2 HelloWord代码的留下你们的邮箱,有时间的话就跟你们发过来
In the past, I posted an example on how to use Displaytag with Struts and Spring, using Spring JDBC for data access(1, 2). In this post, I will describe how to do the same using Struts 2.0. The only major step that needs to be done here is to override the default Struts 2.0 OjbectFactory. Changing the ObjectFactory to Spring give control to Spring framework to instantiate action instances etc. Most of the code is from the previous post, but I will list only the additional changes here.
- Changing the default Object factory: In order to change the Ojbect factory to Spring, you have to add a declaration in the struts.properties file.
struts.objectFactory = spring
struts.devMode = true
struts.enable.DynamicMethodInvocation = false
src/struts.properties
- The Action class: Here is the code for the action class
package actions;
import java.util.List;
import business.BusinessInterface;
import com.opensymphony.xwork2.ActionSupport;
public class SearchAction extends ActionSupport {
private BusinessInterface businessInterface;
private String minSalary;
private String submit;
private List data;
public String getSubmit() {
return submit;
}
public void setSubmit(String submit) {
this.submit = submit;
}
public BusinessInterface getBusinessInterface() {
return businessInterface;
}
public String execute() throws Exception {
try {
long minSal = Long.parseLong(getMinSalary());
System.out.println("Business Interface: " + businessInterface + "Minimum salary : " + minSal);
data = businessInterface.getData(minSal);
System.out.println("Data : " + data);
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public void setBusinessInterface(BusinessInterface bi) {
businessInterface = bi;
}
public String getMinSalary() {
return minSalary;
}
public void setMinSalary(String minSalary) {
this.minSalary = minSalary;
}
public List getData() {
return data;
}
public void setData(List data) {
this.data = data;
}
}
SearchAction.java
- The Action class here does not have access to the HttpServetRequest and HttpServletResponse. Hence the action class itself was changed to the session scope for this example (see below)
- In order for the action class to be aware of the Http Session, the action class has to implement the ServletRequestAware interface, and define a setServletRequest method, which will be used to inject the ServletRequest into the action class.
- The BusinessInterface property is injected by Spring framework.
- The struts Configuration:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="Struts2Spring" namespace="/actions" extends="struts-default">
<action name="search" class="actions.SearchAction">
<result>/search.jsp</result>
</action>
</package>
</struts>
src/struts.xml
- The action's class attribute has to map the id attribute of the bean defined in the spring bean factory definition.
- The Spring bean factory definition
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"
default-autowire="autodetect">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName">
<value>oracle.jdbc.driver.OracleDriver</value>
</property>
<property name="url">
<value>jdbc:oracle:thin:@localhost:1521:orcl</value>
</property>
<property name="username">
<value>scott</value>
</property>
<property name="password">
<value>tiger</value>
</property>
</bean>
<!-- Configure DAO -->
<bean id="empDao" class="data.DAO">
<property name="dataSource">
<ref bean="dataSource"></ref>
</property>
</bean>
<!-- Configure Business Service -->
<bean id="businessInterface" class="business.BusinessInterface">
<property name="dao">
<ref bean="empDao"></ref>
</property>
</bean>
<bean id="actions.SearchAction" name="search" class="actions.SearchAction" scope="session">
<property name="businessInterface" ref="businessInterface" />
</bean>
</beans>
WEB-INF/applicationContext.xml
- The bean definition for the action class contains the id attribute which matches the class attribute of the action in struts.xml
- Spring 2's bean scope feature can be used to scope an Action instance to the session, application, or a custom scope, providing advanced customization above the default per-request scoping.
- The web deployment descriptor
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
<display-name>Struts2Spring</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
web.xmlThe only significant addition here is that of the RequestContextListener. This listener allows Spring framework, access to the HTTP session information.
- The JSP file: The JSP file is shown below. The only change here is that the action class, instead of the Data list is accessed from the session.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page import="actions.SearchAction,beans.Employee,business.Sorter,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%>
<html>
<head>
<title>Search page</title>
<link rel="stylesheet" type="text/css" href="/StrutsPaging/css/screen.css" />
</head>
<body bgcolor="white">
<s:form action="/actions/search.action">
<table>
<tr>
<td>Minimum Salary:</td>
<td><s:textfield label="minSalary" name="minSalary" /></td>
</tr>
<tr>
<td colspan="2"><s:submit name="submit" /></td>
</tr>
</table>
</s:form>
<jsp:scriptlet>
SearchAction action = (SearchAction)session.getAttribute("actions.SearchAction");
session.setAttribute("empList", action.getData());
if (session.getAttribute("empList") != null) {
String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));
Sorter.sort((List) session.getAttribute("empList"), sortBy);
</jsp:scriptlet>
<display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending" requestURI="">
<display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" />
<display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" />
<display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" />
<display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" />
</display:table>
<jsp:scriptlet>
}
</jsp:scriptlet>
</body>
</html:html>
search.jsp
分享到:
相关推荐
### Struts2框架整合Spring框架在文件上传下载中的应用 #### 一、引言 随着互联网技术的迅速发展,Web应用程序的功能越来越丰富,文件的上传下载功能已成为许多Web应用不可或缺的一部分。例如,在博客、电子邮件...
4. **整合Spring和Struts2**:使用Spring的Struts2插件,使Spring管理的Bean可以直接在Struts2 Action中注入使用。 5. **整合Spring和MyBatis**:通过Spring的SqlSessionFactoryBean,将MyBatis的SqlSessionTemplate...
在整合Struts2时,我们需要配置Spring的`spring-beans.jar`、`spring-context.jar`等,通过Spring的ApplicationContext加载bean,并将这些bean注入到Struts2的Action中,以实现业务逻辑的解耦。同时,Spring还提供了...
在整合中,Spring用于管理Struts2和Hibernate的实例,通过IoC容器进行依赖注入,同时可以提供事务管理,确保数据的一致性。 整合过程: 1. 配置环境:首先需要在项目中引入Struts2、Hibernate和Spring的相应库,并...
虽然Struts 2 本身具备一定的依赖注入能力,但在实际项目中,为了更好地管理组件之间的依赖关系,通常会选择与Spring这样的外部IoC容器进行整合。 #### 三、Struts 2 和 Spring3 的整合步骤 1. **环境准备**: - ...
在SSH整合中,Spring通常用来管理Struts2和Hibernate的bean实例,实现依赖注入,降低组件之间的耦合度。此外,Spring的事务管理功能能够统一管理数据库操作,确保数据的一致性。 Hibernate是持久层框架,它简化了...
Struts2 整合 Spring 是一个常见的 Java Web 开发中的集成技术,主要目的是利用 Spring 提供的依赖注入(Dependency Injection, DI)和控制反转(Inversion of Control, IoC)来管理 Struts2 中的 Action 类,使得...
Struts2整合Spring的主要目的是利用Spring的Inversion of Control(IoC)容器管理Struts2中的Action类和其他业务层组件,以实现松耦合和更好的依赖注入。在整合过程中,Struts2会委托Spring来创建和管理Action实例,...
在整合过程中,开发者需要注意配置文件的正确设置,包括Struts2的struts.xml、Hibernate的hibernate.cfg.xml以及Spring的applicationContext.xml等。还需要确保各框架之间的依赖注入正确无误,例如,Spring需要知道...
在Struts2和MyBatis的整合中,Spring可以协调这两个框架,管理Struts2的Action和MyBatis的Mapper接口,实现依赖注入和事务控制。 整合这三大框架的过程主要包括以下几个步骤: 1. 配置Spring:创建Spring的配置...
在Java企业级应用开发中,Spring和Struts 2都是重要的框架,它们分别在不同的层面上发挥着关键作用...通过学习和实践这些材料,开发者可以掌握在实际项目中整合Spring和Struts 2的技能,从而提升自己的JavaEE开发能力。
开发人员可以通过分析这些文件,了解如何配置Struts2、Spring4和Hibernate4的整合,学习如何在实际项目中应用这三大框架。此外,还可以通过阅读源码,理解它们之间的交互机制,加深对MVC模式和Java Web开发的理解。 ...
- 示例可能会展示如何配置Action类,如何在Spring中定义bean,以及如何在Struts2中引用这些bean。 通过上述内容,我们可以看到Struts2与Spring整合的关键技术和优势,这有助于构建更健壮、更易于维护的Java Web...
在提供的"spring+struts2+ibatis整合的jar包"中,lib1可能包含了这三个框架以及它们的依赖库。这些jar文件是运行整合应用必不可少的组件,它们包含了框架的API、实现和一些工具类,帮助开发者快速搭建和运行整合项目...
在这个项目中,Spring主要负责管理对象(包括Hibernate和Struts2中的组件),以及提供事务管理。核心概念包括IoC容器、Bean配置(beans.xml)、AOP代理、数据源和事务管理器。 4. **整合过程**: - 配置Spring:...
在Struts2中,我们需要配置一个Spring插件,以便在请求处理时从ApplicationContext中获取Action实例。 3. **Struts2-Spring插件**:这个插件使得Struts2能够与Spring无缝集成。它会自动扫描并管理Spring配置文件中...
在SSM整合中,Spring作为核心,负责初始化和管理所有组件,包括Struts2的Action和拦截器,以及MyBatis的SqlSessionFactory。Spring的ApplicationContext加载配置文件,创建Bean实例,并通过依赖注入将它们装配到一起...