1、下载地址
最好下载-all.zip
2、导入jar包
下载文件——>apps——>打开随便一个war包
3、写action类(有3种方式)
(1)普通类
package hello;
public class Hello {
public String execute()
{
return "ok";
}
}
(2)实现接口
package hello;
import com.opensymphony.xwork2.Action;
public class Hello implements Action {
public String execute()
{
return "ok";
}
}
(3)继承类(也实现了Action接口,常用)
package hello;
import com.opensymphony.xwork2.ActionSupport;
public class Hello extends ActionSupport {
public String execute()
{
return "ok";
}
}
4、struts配置文件(必须再src下,名称为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>
<!-- 常量设置,这个设置可以解决Post乱码问题 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- name随便取,不能重名;
extends="struts-default" 固定内容,识别action;
namespace名称空间,默认是/ -->
<package name="package1" extends="struts-default" namespace="/">
<!-- 识别网址中的hello,与name的值相同则配对成功
class是action类的全路径
method识别类中的execute方法 -->
<action name="hello" class="hello.Hello" method="execute">
<!-- 与类返回的字符串配对,相同则跳转到这个页面 -->
<result name="ok">/hello.jsp</result>
</action>
</package>
</struts>
5、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts2</display-name>
<!-- 过滤器 -->
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
6、访问
localhost:8080/项目名/hello