之前已经对Spring以及Struts的基础知识、配置以及单独开发都做了讲述,本文则对Spring整合Struts2开发进行讲述分析。之前讲的直接使用struts2进行开发web应用,存在一个问题,就是struts2作为核心控制器,还需要进行业务逻辑组件的管理,会使性能降低,理想状态是控制器不参与业务逻辑组件的实现,只是负责使用业务逻辑组件,这就是设计模式中的工厂模式。而使用Spring整合struts2的目的,就是解放struts2,将Spring当做一个工厂,负责所有业务逻辑组件的创建,而struts2只需要直接使用工厂创建的组件即可。这样将控制器与业务逻辑组件实现分离,从而提供更好的解耦。
那么控制器如何访问到Spring容器中创建的业务逻辑组件呢?这里有两种方式:
1.Spring容器负责管理控制器Action,并利用依赖注入为控制器注入业务逻辑组件。
2.利用Spring的自动装配,Action将会自动从Spring容器中获取所需的业务逻辑组件。
本文主要讲述的是第一种方式,即Spring容器负责管理控制器Action,并利用依赖注入业务逻辑组件。接下来,通过一个简单的应用,进行讲述完整的整合流程。
首先,在Eclipse上新建一个WEB文件,然后将struts2项目lib目录下的所有jar包复制到项目文件WEB-INF\lib目录下。
然后直接在web.xml文件中配置创建Spring容器。其中ContextLoaderListener是一个监听器类,实现了ServletContextListener接口,该类作为Listener,它会在创建时自动查找WEB-INF/下的applicationContext.xml文件,本文介绍的示例只有一个配置文件,且为applicationContext.xml,则web.xml配置如下,且该文件放在WEB-INF下中。
<?xml version="1.0" encoding="GBK"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 定义Struts 2的FilterDispathcer的Filter -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
如果有多个配置文件要载入,则考虑使用<context-param.../>元素来配置文件的文件名。如下是有两个配置文件daoContext.xml和applicationContext.xml文件的配置。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml
,/WEB-INF/applicationContext.xml</param-value>
</context-param>
然后需要做的是将需要的包都放到web项目WEB-INF\lib下,首先是将struts项目中apps中的struts2-blank解压,将jar包复制到web项目WEB-INF\lib下;又因为是spring整合struts2,所以需要将struts2项目中lib目录下的struts2-spring-plugin-2.3.34.jar复制到web项目WEB-INF\lib下;再将spring项目lib目录下的jar包复制到web项目WEB-INF\lib下。(注意,如果不把相应的jar包复制到web项目WEB-INF\lib下会报错)。
由于将spring当做工厂,所以先将产品写出来,然后交由工厂配置,接下来是Action类,Action类还是跟之前一样,需要继承ActionSupport类,代码如下。
package handle.action;
import com.opensymphony.xwork2.ActionSupport;
import handle.service.*;
public class LoginAction extends ActionSupport
{
// 下面是用于封装用户请求参数的两个成员变量
private String username;
private String password;
// 系统所用的业务逻辑组件
private MyService ms;
// 设值注入业务逻辑组件所必需的setter方法
public void setMs(MyService ms)
{
this.ms = ms;
}
// username的setter和getter方法
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return this.username;
}
// password的setter和getter方法
public void setPassword(String password)
{
this.password = password;
}
public String getPassword()
{
return this.password;
}
// 处理用户请求的execute方法
public String execute() throws Exception
{
// 调用业务逻辑组件的validLogin()方法
// 验证用户输入的用户名和密码是否正确
if (ms.validLogin(getUsername(), getPassword()) > 0)
{
addActionMessage("哈哈,整合成功!");
return SUCCESS;
}
return ERROR;
}
}
上面Action类中使用到了MyService组件,所以,接下来需要进行MyService代码的编写,组件类采用面对接口编程,因此,先编写一个MyService接口,然后实现接口。代码如下:
package handle.service;
public interface MyService {
int validLogin(String username , String pass);
}
package handle.service.impl;
import handle.service.MyService;
public class MyServiceImpl implements MyService
{
public int validLogin(String username , String pass)
{
// 此处只是简单示范,故直接判断用户名、密码是否符合要求
if ( username.equals("carson")
&& pass.equals("0408") )
{
return 99;
}
return -1;
}
}
接着就是对Action类当做Bean类配置,配置文件为applicationContext,配置内容类似于前面Spring开发中的beans.xml文件。并将该配置文件放在web项目下的WEB-INF下。
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 定义一个业务逻辑组件,实现类为MyServiceImp -->
<bean id="myService"
class="handle.service.impl.MyServiceImpl"/>
<!-- 让Spring管理的Action实例,并依赖注入业务逻辑组件 -->
<bean id="loginAction" class="handle.action.LoginAction"
scope="prototype" p:ms-ref="myService"/>
</beans>
然后便是jsp文件的编写,该项目涉及三个jsp文件,注意这些jsp文件需要放在web项目下WebContent目录下,但是不能放在WEB-INF目录下,否则无法被访问,内容分别如下:
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>错误页面</title>
</head>
<body>
您不能登录!
</body>
</html>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>成功页面</title>
</head>
<body>
您已经登录!<br/>
<s:actionmessage />
</body>
</html>
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>登录页面</title>
</head>
<body>
<h3>用户登录</h3>
<s:form action="login">
<s:textfield name="username" label="用户名"/>
<s:textfield name="password" label="密码"/>
<tr align="center">
<td colspan="2">
<s:submit value="登录" theme="simple"/>
<s:reset value="重设" theme="simple"/>
</td>
</tr>
</s:form>
</body>
</html>
最后便是对struts.xml的配置,struts.xml用于将Spring管理器这个大工厂中的Bean类与jsp文件相关联。该配置文件放在web项目src目录下。配置内容如下:
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Struts 2配置文件的DTD信息 -->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- Struts 2配置文件的根元素 -->
<struts>
<!-- 配置了系列常量 -->
<constant name="struts.i18n.encoding" value="GBK"/>
<constant name="struts.devMode" value="true"/>
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
, 而是Spring容器中的Bean实例的ID -->
<action name="login" class="loginAction">
<!-- 为两个逻辑视图配置视图页面 -->
<result name="error">/error.jsp</result>
<result>/welcome.jsp</result>
</action>
<!-- 让用户直接访问该应用时列出所有视图页面 -->
<action name="*">
<result>/content/{1}.jsp</result>
</action>
</package>
</struts>
观察上述配置文件,我们可以发现,与之前struts2单独开发的struts.xml文件不同的是class内容的不同,之前是Action类,而这里指的是Spring容器中Action配置相应的Bean Id,通过这个将Spring与jsp以及Action类关联在一起。
然后运行该项目,得到页面如下:
输入正确的账户密码之后,得到:
输入错误的账户密码之后,得到:
上面整个简易项目是对Spring整合struts2的开发的演示。整个过程的流程如上所示。以下是项目文件结构图: