s2s2

本文介绍如何在Eclipse中创建Dynamic Web Application,并实现Struts2与Spring框架的集成。主要步骤包括配置必要的库文件、修改web.xml文件、编写HelloWorld Action、配置struts.xml和applicationContext.xml文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Eclipse下建立一个Dynamic Web Application。

从struts2.0.6的lib目录中复制下面的库文件到WEB-INF/lib目录下:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.9.jar
struts-api-2.0.6.jar
struts-core-2.0.6.jar
struts-spring-plugin-2.0.6.jar
xwork-2.0.0.jar

从spring中lib目录中复制下面的库文件到WEB-INF/lib目录下:
spring.jar

修改web.xml,增加一个struts的分派器filter,映射所有的url-pattern,再增加一个spring的ContextLoaderListener监听器。修改后的内容如下:
xml 代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appid="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/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <display-name>struts2tutorial</display-name>
  7. <filter>
  8. <filter-name>struts2</filter-name>
  9. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>struts2</filter-name>
  13. <url-pattern>*.action</url-pattern>
  14. </filter-mapping>
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. </web-app>


写一个简单的Action,HelloWorld:
java 代码
  1. packagetutorial;
  2. importcom.opensymphony.xwork2.ActionSupport;
  3. publicclassHelloWorldextendsActionSupport{
  4. publicstaticfinalStringMESSAGE="Strutsisupandrunning...";
  5. publicStringexecute()throwsException{
  6. setMessage(MESSAGE);
  7. returnSUCCESS;
  8. }
  9. privateStringmessage;
  10. publicvoidsetMessage(Stringmessage){
  11. this.message=message;
  12. }
  13. publicStringgetMessage(){
  14. returnmessage;
  15. }
  16. }


在源文件路径下(项目的src目录)增加struts.xml配置action。这个文件是集成spring的关键所在,这里面描述有如何将spring2集成到struts2的相关信息:
xml 代码
  1. <!DOCTYPEstrutsPUBLIC
  2. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.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. <constantname="objectFactory"value="spring"></constant>
  9. <packagename="struts2tutoial"extends="struts-default"namespace="/">
  10. <!-- 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id
  11. 详细信息请看下面的spring的bean配置文件applicationContext.xml-->
  12. <actionname="HelloWorld"class="helloWorld">
  13. <result>/helloWorld.jsp</result>
  14. </action>
  15. <!--Addyouractionshere-->
  16. </package>
  17. </struts>


在WEB-INF/目录下增加spring的bean配置文件applicationContext.xml:
xml 代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans>
  4. <beanid="helloWorld"class="tutorial.HelloWorld"></bean>
  5. </beans>

配置很简单,只有一个bean。

最后,在WebContent目录下增加helloWorld.jsp:
xml 代码
  1. <%@taglibprefix="s"uri="/struts-tags"%>
  2. <html>
  3. <head>
  4. <title>HelloWorld!</title>
  5. </head>
  6. <body>
  7. <h2><s:propertyvalue="message"/></h2>
  8. </body>
  9. </html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值