2007-02-26
struts2笔记:与spring2集成
关键字: webwork
春节大假结束了,又该开始学习了。春节期间发生了一些高兴的事,其中之一便是apache终于发布了struts2的正式版:struts2.0.6GA,所以我也该从webwork迁移至struts2。
struts2基本上就是webwork的翻版,所以迁移过程倒是很简单,只需要修改下配置文件和一些包名就可以了。如果在Eclipse、Netbeans这些集成开发工具的帮助下,记不清包名也很容易找到想要的类的,呵呵。
在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监听器。修改后的内容如下:
写一个简单的Action,HelloWorld:
在源文件路径下(项目的src目录)增加struts.xml配置action。这个文件是集成spring的关键所在,这里面描述有如何将spring2集成到struts2的相关信息:
在WEB-INF/目录下增加spring的bean配置文件applicationContext.xml:
配置很简单,只有一个bean。
最后,在WebContent目录下增加helloWorld.jsp:
struts2基本上就是webwork的翻版,所以迁移过程倒是很简单,只需要修改下配置文件和一些包名就可以了。如果在Eclipse、Netbeans这些集成开发工具的帮助下,记不清包名也很容易找到想要的类的,呵呵。
在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 代码
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" 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>struts2tutorial</display-name>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
写一个简单的Action,HelloWorld:
java 代码
- package tutorial;
- import com.opensymphony.xwork2.ActionSupport;
- public class HelloWorld extends ActionSupport {
- public static final String MESSAGE = "Struts is up and running ...";
- public String execute() throws Exception {
- setMessage(MESSAGE);
- return SUCCESS;
- }
- private String message;
- public void setMessage(String message){
- this.message = message;
- }
- public String getMessage() {
- return message;
- }
- }
在源文件路径下(项目的src目录)增加struts.xml配置action。这个文件是集成spring的关键所在,这里面描述有如何将spring2集成到struts2的相关信息:
xml 代码
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- 下面这句表明对象是由spring负责产生的.加上这句后,struts会产生让spring负责
- 产生bean,如果spring不能产生bean,则由struts自己产生.也可以在struts.properties
- 文件内定义这个属性.-->
- <constant name="objectFactory" value="spring"></constant>
- <package name="struts2tutoial" extends="struts-default" namespace="/">
- <!-- 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id
- 详细信息请看下面的spring的bean配置文件applicationContext.xml -->
- <action name="HelloWorld" class="helloWorld">
- <result>/helloWorld.jsp</result>
- </action>
- <!-- Add your actions here -->
- </package>
- </struts>
在WEB-INF/目录下增加spring的bean配置文件applicationContext.xml:
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="tutorial.HelloWorld"></bean>
- </beans>
配置很简单,只有一个bean。
最后,在WebContent目录下增加helloWorld.jsp:
xml 代码
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <html>
- <head>
- <title>Hello World!</title>
- </head>
- <body>
- <h2><s:property value="message" /></h2>
- </body>
- </html>
评论
qianlei007
2007-08-22
lijie250 写道
你的spring作用更本没用到啊!表达的意思就是HelloWorld
action 由 spring来管理撒... spring怎么会没有作用呢..
lijie250
2007-06-25
你的spring作用更本没用到啊!表达的意思就是HelloWorld
jinlibing
2007-06-25
struts-api-2.0.6.jar没有这个包。。。
movingboy
2007-04-19
wensky222 写道
如果spring的配置文件名称不是applicationContext.xml
该从哪里修改一下?
该从哪里修改一下?
在web.xml中加入:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
把<param-value>...</param-value>里的路径替换成自己的配置文件路径
wensky222
2007-04-19
如果spring的配置文件名称不是applicationContext.xml
该从哪里修改一下?
该从哪里修改一下?
movingboy
2007-04-03
gcgan 写道
我找老兄的方法做了,但他提示找不到Action阿。
严重: Exception starting filter struts
Action class [loginbean] not found - action - file:/D:/test/.metadata/.plugins/org.
提示一下嘛
严重: Exception starting filter struts
Action class [loginbean] not found - action - file:/D:/test/.metadata/.plugins/org.
提示一下嘛
我按照楼主的办法试验通过了~~~
我看楼主的代码中并没有提到loginbean,你找一下,把loginbean相关的配置去掉再试试
gcgan
2007-03-29
我找老兄的方法做了,但他提示找不到Action阿。
严重: Exception starting filter struts
Action class [loginbean] not found - action - file:/D:/test/.metadata/.plugins/org.
提示一下嘛
严重: Exception starting filter struts
Action class [loginbean] not found - action - file:/D:/test/.metadata/.plugins/org.
提示一下嘛
pignut_wang
2007-03-14
不错,正好在看struts2,多谢楼主共享!
- 浏览: 15289 次
- 性别:

- 来自: Sichuan Chengdu

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
Mule入门
简单来说,mule有两个主要功能: 1:作为一系列协议或者技术的实现引擎,如mu ...
-- by boyingking -
Mule入门
关于中文乱码的问题,到现在我也没有找到好的办法,有一个Encoder的属性,但是 ...
-- by welcomyou -
Mule入门
我也是输入中文出现乱码,楼上的那位兄弟解决了这个问题了么?
-- by changyanping -
Mule入门
兄弟可不可以给个录个视频呀?我老是搞不对!郁闷,要是能得到您的指点的话不慎感激!
-- by changyanping -
Mule入门
谢谢哦 调试通过了 不过输入中文有乱码 能不能说下怎么解决中文乱码的问题
-- by ainrio






评论排行榜