struts2与spring的整合示例

来源:互联网 发布:oracle队列 Java 编辑:程序博客网 时间:2024/05/16 06:54

为了简单起见,文中只是讲操作过程,不讲原理,请大家理解.

(1)新建一个web项目

加入spring-2.0.jar,struts2-core-2.0.11.jar,struts2-spring-plugin-2.0.11.jar,xwork-2.0.4.jar,freemarker-2.3.8.jar,commons-logging-1.0.4.jar,ognl-2.6.11.jar等jar包

(2)修改WEB-INF/web.xml文件了,内容为:

<? xml version="1.0" encoding="UTF-8" ?>
< web-app 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 > Struts 2 IoC Demo </ display-name >

< filter >
< filter-name > struts-cleanup </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.ActionContextCleanUp
</ filter-class >
</ filter >

< filter >
< filter-name > struts2 </ filter-name >
< filter-class >
org.apache.struts2.dispatcher.FilterDispatcher
</ filter-class >
</ filter >

< filter-mapping >
< filter-name > struts-cleanup </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >

< filter-mapping >
< filter-name > struts2 </ filter-name >
< url-pattern > /* </ url-pattern >
</ filter-mapping >

< listener >
< listener-class >
org.springframework.web.context.ContextLoaderListener
</ listener-class >
</ listener >

< welcome-file-list >
< welcome-file > index.html </ welcome-file >
</ welcome-file-list >
</ web-app >

清单1 WEB-INF/web.xml

较纯struts2程序多了两项:

1:加入Spring的ContextLoaderListener监听器,方便Spring与Web容器交互

2:struts-cleanup让程序知道在正确的时候清除请求(servletContext)而不是立即清除,方便在程序中取得servletContext

(3)在Struts.properties中加入以下项:

struts.objectFactory = spring

告知Struts 2运行时使用Spring来创建对象(如Action等).

(4)创建接口ChatService

package ss;

import java.util.Set;

public interface ChatService {
Set
< String > getUserNames();
}

再创建一个默认实现ChatServiceImpl

package ss;

import java.util.HashSet;
import java.util.Set;

public class ChatServiceImpl implements ChatService {

public Set < String > getUserNames() {
Set
< String > users = new HashSet < String > ();
users.add(
" Max " );
users.add(
" Scott " );
users.add(
" Bob " );
return users;
}


}

下来,就该新建Action了。tutorial.ChatAction.java的代码如下:

package ss;

import java.util.Set;

import com.opensymphony.xwork2.ActionSupport;

public class ChatAction extends ActionSupport {
private static final long serialVersionUID = 8445871212065L ;

private ChatService chatService;
private Set < String > userNames;

public void setChatService(ChatService chatService) {
this .chatService = chatService;
}


public Set < String > getUserNames() {
return userNames;
}


@Override
public String execute() {
userNames
= chatService.getUserNames();
return SUCCESS;
}


}

然后,配置Spring的applicationContext.xml(位于WEB-INF下)文件,内容如下:

<?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" >
    <bean id ="chatService" class ="ss.ChatServiceImpl" />
    <bean id ="chatAction" class ="ss.ChatAction" scope ="prototype" >
        <property name ="chatService" >
            <ref local ="chatService" />
        </property >
    </bean >
</beans >

述代码有二点值得大家注意的:

  1. Struts 2会为每一个请求创建一个Action对象,所以在定义chatAction时,使用scope="prototype"。这样Spring就会每次都返回一个新的ChatAction对象了;
  2. 因为ChatServiceImpl被配置为默认的scope(也即是singleton,唯一的),所以在实现时应保证其线程安全(关于编写线程安全的代码的讨论已经超出本文的范围,更超出了本人的能力范围,大家可以参考Addison Wesley Professional出版的《Java Concurrency in Practice》)。

接下来,在classes/struts.xml中配置Action,内容如下

<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml" />
<package name="ss" extends="struts-default">
   <action name="Chat" class="ss.ChatAction">
    <result>/UserList.jsp</result>
   </action>
</package>
</struts>

最后,让我们看看/UserList.jsp,内容如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="Utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
   <meta http-equiv="Content-Type"
    content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
</head>
<body>
   <h2>
    User List
   </h2>
   <s:iterator value="userNames">
     <li>
      <s:property />
     </li>
    </s:iterator>
</body>
</html>
大功告成,分布运行应用程序,在浏览器中键入http://172.16.1.111:8080/strutsspring/Chat.action,出现如图2所示页面:

源程序下载地址:stutsspring@163.com

用户名:stutsspring

密码;admin123