一、本文适用者
本文适合刚开始接触struts2的用户,本文即教用户如何跑一个类似hello world 程序。
二、开发环境
操作系统:window xp sp2
开发平台:myeclipse 8.5
struts2包:struts-2.1.6-all.zip
三、配置过程
1.File->new->Web Project 新建一个工程名为Struts2HelloWorld,其他项不用改,J2EE Specification Level选Java EE 5.0,然后点Finish即可,后来跳出来的窗口一律点yes。将WebRoot下的index.jsp中<body>标签中的"This is my JSP page.”改成"Hello World!"。
2.将struts-2.1.6-all.zip解压,将apps文件夹下的struts2-blank-2.1.6.war的后缀名"war”改成"zip",然后解压缩到当前文件夹。(下面附件为struts2-blank-2.1.6.zip)
3.将解压后WEB-INF/lib文件夹下的jar文件全部拷贝到Struts2HelloWorld工程下的WebRoot/WEB-INF/lib中
4.将解压后WEB-INF文件夹下web.xml中的如下代码拷贝到Struts2HelloWorld工程下的web.xml中,不做任何修改。
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
5.将解压后WEB-INF/classes文件夹下的struts.xml文件拷贝到Struts2HelloWorld工程的src中,并修改其中的代码为如下:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <!--<include file="example.xml"/> --> <package name="default" namespace="/" extends="struts-default"> <!--<default-action-ref name="index" />--> <action name="index"> <result> /index.jsp </result> <!-- <result type="redirectAction"> <param name="actionName">HelloWorld</param> <param name="namespace">/example</param> </result>--> </action> </package> <!-- Add packages here --> </struts>
6.在tomcat服务器上发布成功后,在浏览器中输入http://localhost:8080/Struts2HelloWorld/index.action就可以看到页面上显示Hello World!至此一个简单的Struts2程序就完成了。