一、 步骤
1. 在Gao_struts2/WebRoot新建useradd_success.jsp
其中<body>内容为:user add success! <br>
2. 新建java类
package action;
importcom.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport
{
public String add()
{
return SUCCESS;
}
}
3. 在Gao_struts2/src目录下,新建struts.xml
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="struts"namespace="/struts" extends="struts-default">
<action name="user*"class="action.UserAction" method="{1}">
<result>/user{1}_success.jsp</result>
</action>
</package>
</struts>
4. 修改web.xml
<filter>
<filter-name>struts</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>
struts-default.xml,struts-plugin.xml,
../conf/struts2.xml,struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
5. 运行
启动tomcat,访问http://localhost:2000/Gao_struts2/struts/useradd.action
页面显示user add success!
那么到这我们的流程就算结束了!
6. 总结
1) 根据我们访问的地址通过struts.xml找到对应的package,根据useradd.action找到对应的“user*”,其中method={1},{1}即*的内容:add,所以我们访问的是UserAction对应的add方法。
2) struts2不是一定要用execute方法的哦!
3) 根据struts.xml中的<result>/user{1}_success.jsp</result>
那么我们就可以得到我们返回的页面是useradd_success.jsp!