java类
package cn.hb.si;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
private HttpServletRequest request;
public String add(){
return "add";
}
public String update(){
return "update";
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("执行Action");
//返回string 可以理解为视图的路径
return SUCCESS;
}
}
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>
<!-- 引入其他的配置文件 可以添加多个,也可以添加包名-->
<include file="hello.xml"></include>
<!-- 配置编码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 修改后缀 后缀不写那么访问的时候就也不用写,如果写了那么访问也要写-->
<constant name="struts.action.extension" value="html"></constant>
</struts>
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" version="3.0">
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<!-- 配置后缀
<init-param>
<param-name>struts.action.extension</param-name>
<param-value>do</param-value>
</init-param>
-->
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
引用的hello.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">
<default-action-ref name="index"></default-action-ref>
<action name="index" >
<result>/error.jsp</result>
</action>
<action name="*_*_*" method="{2}" class="cn.hb.{3}.{1}Action">
<result>/result.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="update">/{2}.jsp</result>
</action>
</package>
</struts>
访问路径:localhost:8080/Struts2_1zixue/Hello_update_si,xml
配置后缀有三种方式:
第一种实在struts.xml中配置
第二种在web.xml中配置
第三种在