如何快速地部屬Struts2
開發工具-Eclipse Struts2.0
從新建Web/Dynamic Web Project開始
不說簡單的了,只說幾點要特別注意的
1.應用Struts2
將xwork-2.0.4.jar、commons-logging-1.0.4.jar、freemarker-2.3.8.jar、ognl-2.6.11.jar、struts2-core-2.0.11.jar這幾個包copy到web程序的lib下。其它的不要。否則發佈的時候會報:Unable to load bean: type:com.opensymphony.xwork2.ObjectFactory class:org.apache.struts2.spring.StrutsSpringObjectFactory - bean錯誤
原因是:If any Struts 2 Plugins are included, then other JARs may be needed too. For example, the optional Spring Plugin requires the Spring JARs to be present.
2.部屬
(1)web.xml
<! DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app >
< display-name > Struts 2.0 Hello World </ 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 > /* </ url-pattern >
</ filter-mapping >
< welcome-file-list >
< welcome-file > index.html </ welcome-file >
</ welcome-file-list >
</ web-app >
Struts中默認的web.xml配置如上
(2)struts.xml
<! 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 ="tutorial" extends ="struts-default" >
< action name ="HelloWorld" class ="tutorial.HelloWorld" >
< result > HelloWorld.jsp </ result >
</ action >
</ package >
</ struts >
Struts中默認的struts.xml配置如上
注:文件名必須爲struts.xml
3.Action Class
繼承ActionSupport(com.opensymphony.xwork2.ActionSupport)
實現execute
返回ERROR,INPUT,LOGIN,NONE,SUCCESS之一