以struts-2.3.24为例。
搭建Struts2环境步骤:
1、下载相关jar包
Apache Struts官方网站 http://struts.apache.org/、http://people.apache.org/builds/struts/
struts-2.5.2\apps :样例包
struts-2.5.2\docs :文档包
struts-2.5.2\docs\struts2-core-apidocs :核心api
struts-2.5.2\docs\struts2-plugins :插件api
struts-2.5.2\lib:驱动包
struts-2.5.2\src:源码
2、创建web项目
File -> New -> Dynamic Web Project
3、创建并完善相关配置文件
4、创建Action并测试启动(访问路径http://localhost:8080/HelloWord/helloword.action)
引入jar包
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_9" 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>HelloWord</display-name> <!-- 过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 过滤器filter映射 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <action name="helloword" class="com.action.HelloWord"> <result>/result.jsp</result> </action> </package> </struts>
HelloWord.java
package com.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWord extends ActionSupport { @Override public String execute() throws Exception { System.out.println("执行action"); return SUCCESS; } }