开发流程:
1.搭建开发和运行环境
Eclipse JavaEE 3.6.2
Struts 2.0包:
- commons-logging-1.0.4.jar
- freemarker-2.3.15.jar
- ognl-2.7.3.jar
- struts2-core-2.1.8.1.jar
- xwork-2.1.6.jar
2.打开web.xml将其修改为以下代码:
<?xml version="1.0" encoding="ISO-8859-1"?> <!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>
3.在src新建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="action类路径"> <result>**.jsp</result> </action> </package> </struts>
4.新建 jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Say Hello</title> </head> <body> <h3>Say "Hello" to: </h3> <s:form action="HelloWorld"> Name: <s:textfield name="name" /> <s:submit /> </s:form> </body> </html>
5.添加tomcat
下载tomcat6的eclipse插件,适用于eclipse3.2以上,解压后复制到eclipse的plugins目录下就可以了,然后打开eclipse配置tomcat就可以使用了。
6.打包发布应用程序
右键点击工程->Export\WAR file->选择“Web\WAR file”->输入war文件的路径(如%tomcat%\webapps\Struts2_HelloWorld.war)