shenang博客技术文档


理论不懂就实践,实践不会就学理论!

posts - 35,comments - 55,trackbacks - 0

 

                   整合Springstruts学习笔记

整合共有三种方法:

使用 Spring ActionSupport类整合 Structs

使用 Spring DelegatingRequestProcessor

覆盖 Struts RequestProcessor Struts Action管理委托给 Spring 框架

前面二种很简单,但不是最优化的,最优优化是第三种,有兴趣的同志,可以到下面网址去看看

http://www.ibm.com/developerworks/cn/java/j-sr2.html#N1008C

下面,我将以一个实例简单介绍一下二者的整合,以作为个人备忘

对于像我一样的新手,大家一起共同学习,有错误的地方,还望指教。

具体步聚如下:

1、首先是导入需要的包
 看了很多别人的文章,这里都写得不太清楚,我觉得这是比较重要的一步,如果包导入不对,程序就高度不出来,所以,我比较看重这步,因为不是所有人都是很清楚这一步,对于像我这种新手还是有用的,呵呵。

Spring的二个包:
spring.jar
struts2-spring-plugin-2.0.9.jar(其实这个包应该是struts的包哈)
Struts的五个基本包
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl.jar
struts2-core-2.0.9.jar
xwork-2.0.4.jar
(当然,在我写的这个小程序中,有一些struts包是用不到的!导入了包,下一步就开始了!)

2、配置Spring监听器.
向web.xml加入下面语句:

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

下面是完整的web.xml 为了方便大家调试,我给出全码如下:

 

 1 <?xml version="1.0" encoding="UTF-8"?>   
 2 <web-app id="WebApp_ID" version="2.4"   
 3     xmlns="http://java.sun.com/xml/ns/j2ee"   
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
 6     <display-name>struts2tutorial</display-name>   
 7         
 8     <filter>   
 9         <filter-name>struts2</filter-name>   
10         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>   
11     </filter>   
12         
13     <filter-mapping>   
14         <filter-name>struts2</filter-name>   
15         <url-pattern>*.action</url-pattern>   
16     </filter-mapping>   
17         
18     <welcome-file-list>   
19         <welcome-file>index.jsp</welcome-file>   
20     </welcome-file-list>   
21         
22     <listener>   
23         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
24     </listener>   
25         
26 </web-app>


3、利用Spring配置文件来注册对象

在原有的struts.xml文件加入

 

 1 <!DOCTYPE struts PUBLIC    
 2     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
 3     "http://struts.apache.org/dtds/struts-2.0.dtd">
 4 <struts>
 5     <!-- 下面这句表明对象是由spring负责产生的.加上这句后,struts会产生让spring负责 
 6         产生bean,如果spring不能产生bean,则由struts自己产生.也可以在struts.properties 
 7         文件内定义这个属性.-->
 8     <constant name="struts.objectFactory.spring.autoWire" value="type" />
 9     <constant name="struts.objectFactory" value="spring" />
10     <!--
11     <constant name="objectFactory" value="spring"></constant>
12     -->
13     <package name="struts2tutoial" extends="struts-default"
14         namespace="/">
15         <!-- 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id 
16             详细信息请看下面的spring的bean配置文件applicationContext.xml -->
17         <action name="HelloWorld" class="helloWorld">
18             <result>/helloWorld.jsp</result>
19         </action>
20         <!-- Add your actions here -->
21     </package>
22 </struts>
23 

看到上文中的
8     <constant name="struts.objectFactory.spring.autoWire" value="type" />
 9     <constant name="struts.objectFactory" value="spring" />
10     <!--
11     <constant name="objectFactory" value="spring"></constant>
12     -->

这个地方只需要注释中的也行!

4、配置applicationContext.xml

注意,这个文件一定要放在WEB-INF下面,否则会出错,默认情况下,容器会到WEB-INF目录下面去寻找applicationContext.xml文件。如果我们想指定别的地方的配置文件或者指定多个配置文件,可以通过在web.xml文件中定义 context-param元素来指定,除非你在web.xml作如下配置

 1 <context-param>
 2 
 3     <param-name>contextConfigLocation</param-name>
 4  <!-- 下面指的是文件存放路径,下面我还用了一个通配路径,classpath下的所在形如:applicationContext-XX.xml都引入-->
 5     <param-value>
 6         /WEB-INF/applicationContext.xml,classpath:applicationContext-*.xml
 7     </param-value>
 8 
 9  </context-param>
10 


下面是我的appliccationContext.xml全码:

<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">   
<beans>   
    
<bean id="helloWorld" class="com.tianji.test.HelloWorld"></bean>   
</beans>
<!--上面的解释下:在前面说了,这个bean中的ID,必须与struts中的action指定的class值一样,此bean中的class就是你要处理的action类,如果你在action时有传值,则在此bean中加入形如:
<property name="message">
    
<value>i love you</value></property>
形式的语句!
 
-->


5、写一个action类
HelloWord.java

 1 package com.tianji.test;    
 2 import com.opensymphony.xwork2.ActionSupport;    
 3 public class HelloWorld extends ActionSupport {    
 4    
 5     public static final String MESSAGE = "Struts is up and running ";    
 6    
 7     public String execute() throws Exception {    
 8         setMessage(MESSAGE);    
 9         return SUCCESS;    
10     }    
11    
12     private String message;    
13    
14     public void setMessage(String message){    
15         this.message = message;    
16     }    
17    
18     public String getMessage() {    
19         return message;    
20     }    
21 }   
22 
23 

这个不用解释哈!

6、创建HelloWord.jsp

1 <%@ taglib prefix="s" uri="/struts-tags" %>   
2 <html>   
3     <head>   
4         <title>Hello World!</title>   
5     </head>   
6     <body>   
7         <h2><s:property value="message" /></h2>   
8     </body>   
9 </html>  

发布,运行服务器,在浏览器中打开:
http://localhost:8080/love/HelloWorld.action

注意修改上面红色部份!

结果:成功!

Struts is up and running````````


 

 


 

posted on 2009-03-25 16:11 重庆理工小子 阅读(692) 评论(3)  编辑  收藏 所属分类: Spring2Struts2技术整合

FeedBack:
# re: 整合Spring和struts学习笔记
2009-03-25 17:54 | 学习的人
好文章,我也在学习,谢谢楼主!我是第一个评论的,呵呵!  回复  更多评论
  
# re: 整合Spring和struts学习笔记
2009-03-25 17:55 | 关注
谢谢,关注中!  回复  更多评论
  
# re: 整合Spring和struts学习笔记
2009-03-25 18:04 | 重庆理工小子
@学习的人

由于刚学,文章写得不好!谢谢!

大家一起进步!
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: