Struts2以插件的方式整合其他Java EE框架,比如Spring。通过插件,Struts2几乎能整合任何框架,甚至是还没有问世的框架。
下面就讲讲Struts2与Spring的整合。Struts2与Spring整合需要依赖包struts2-spring-plugin-2.x.x.jar,此包与Struts一同发布。当然了,我们也可以不用这个包,自己写一个。
Struts2整合Spring的关键就是将Spring中的业务逻辑层注入到Action中,有两种方式实现注入。
Spring创建并管理Action
业务逻辑层:
package org.springstruts.service;
public class UserService {
public boolean login(){
return true;
}
}
Action:
package org.springstruts.action;
import org.springstruts.service.UserService;
public class UserAction {
private UserService service;
public void setService(UserService service) {
this.service = service;
}
public String mylogin(){
boolean flag = service.login();
if (flag) {
return "ok";
}
return "no";
}
}
Spring配置文件:
<?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.xsd">
<bean name="userservice" class="org.springstruts.service.UserService"/>
<bean name="useraction" class="org.springstruts.action.UserAction">
<property name="service" ref="userservice"/>
</bean>
</beans>
struts配置文件:
<?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="default" extends="struts-default">
<action name="login" class="useraction" method="mylogin">
<result name="ok">/index.jsp</result>
</action>
</package>
</struts>
JSP页面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
success!
</body>
</html>
web.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">
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
启动服务器,测试,浏览器效果:
采用这种方式,Struts中配置的Action只是一个傀儡,action元素的class属性指向了Spring的Bean名称。
使用插件自动装配(注入)
在上一种方式的基础上修改三个文件:
1.spring配置文件。spring不再创建和管理Action:
<?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.xsd">
<bean name="userservice" class="org.springstruts.service.UserService"/>
</beans>
2.struts配置文件。Action由插件创建:
<?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="default" extends="struts-default">
<action name="login" class="org.springstruts.action.UserAction" method="mylogin">
<result name="ok">/index.jsp</result>
</action>
</package>
</struts>
3.Action类。插件默认使用名称(byName)自动装配,所以action中的业务层引用名称要与Spring容器中业务层Bean名称一致:
package org.springstruts.action;
import org.springstruts.service.UserService;
public class UserAction {
private UserService userservice;
public void setUserservice(UserService userservice) {
this.userservice = userservice;
}
public String mylogin(){
boolean flag = userservice.login();
if (flag) {
return "ok";
}
return "no";
}
}
可以通过配置改变自动装配的方式(比如byType、constructor),这里就不多说了。
测试就省掉了。
两种方式对比
使用第一种方式,需要在Spring配置文件和Struts配置文件都配置Action,略显冗余,但是依赖关系清晰,容易理解;使用第二种方式,只需要在Struts配置文件配置Action,配置简单,但是因为使用了自动装配,程序可读性略显不足。