一、基本配置
1.创建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">
<?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="hello" namespace="/" extends="struts-default">
<action name="HelloAction" class="com.lanou3g.hello.HelloAction" method="hello">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
</struts>
public class HelloAction {
public String hello() {
System.out.println("Hello Struts!");
return "success";
}
}
二、常量配置,标签
<?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>
<constant name="struts.i18n.encoding=UTF-8" value="UTF-8"></constant>
<constant name="struts.action.extension" value="action,,"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="def" namespace="/def" extends="struts-default">
<action name="Demo01Action" class="com.lanou3g.def.Demo01Action" method="defaultFun">
<result name="success" type="dispatcher">/hello.jsp</result>
</action>
</package>
</struts>
package com.lanou3g.def;
public class Demo01Action {
public String defaultFun() {
System.out.println("Demo01Action");
return "success";
}
}
三、常量配置,动态方法标签
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="dynamic" namespace="/dynamic" extends="struts-default">
<action name="Demo02Action_*" class="com.lanou.dynamic.Demo02Action" method="{1}" >
<result name="success" >/hello.jsp</result>
</action>
</package>
</struts>
public class Demo02Action {
public String add() {
System.out.println("增加方法");
return "success";
}
public String delete() {
System.out.println("删除方法");
return "success";
}
public String update() {
System.out.println("修改方法");
return "success";
}
public String find() {
System.out.println("查找方法");
return "success";
}
}
四、web.xml中filter配置文件(核心)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>sh-struts-01</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>