java类
package cn.hb.si;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
ActionContext context = ActionContext.getContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
public String add() {
//System.out.println(request);
request.setAttribute("path", "update");
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;
}
}
struts2.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>
</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">
<param name="location">/${#request.path}.jsp</param>
</result>
<result name="update">/{2}.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="LoginAction" method="login" class="cn.hb.si.LoginAction">
<result>/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
访问地址:http://localhost:8080/Struts2_1zixue/Hello_add_si.action