.使用通配符 *
1创建项目
struts2101406
2添加支持包
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.15.1.jar
xwork-core-2.3.15.1.jar
3. 配置过滤器
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4. 创建包
cn.jbit.action
创建类控制器
hellowaction.java
/**
* 注册
* @return
*/
public String register(){
System.out.println("执行注册方法");
return "register";
}
/**
* 登录
* @return
*/
public String login(){
if ("jbit".equals(user.getUserName()) && "123".equals(user.getPassword())) {
return "login";
}
return ERROR;
}
5添加 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.devModel" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- user Action -->
<action name="userAction_*" class="cn.jbit.struts2101401.web.action.UserAction" method="{1}">
<result name="login">/WEB-INF/pages/{1}success.jsp</result>
<result name="register">/WEB-INF/pages/{1}success.jsp</result>
<result name="error">/WebRoot/WEB-INF/pages/error.jsp</result>
</action>
</package>
</struts>
6创建页面
error.jsp
loginsuccess.jsp
registersuccess.jsp
1创建项目
struts2101406
2添加支持包
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.15.1.jar
xwork-core-2.3.15.1.jar
3. 配置过滤器
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4. 创建包
cn.jbit.action
创建类控制器
hellowaction.java
/**
* 注册
* @return
*/
public String register(){
System.out.println("执行注册方法");
return "register";
}
/**
* 登录
* @return
*/
public String login(){
if ("jbit".equals(user.getUserName()) && "123".equals(user.getPassword())) {
return "login";
}
return ERROR;
}
5添加 struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.ui.theme" value="simple"></constant>
<constant name="struts.devModel" value="true"></constant>
<package name="default" namespace="/" extends="struts-default">
<!-- user Action -->
<action name="userAction_*" class="cn.jbit.struts2101401.web.action.UserAction" method="{1}">
<result name="login">/WEB-INF/pages/{1}success.jsp</result>
<result name="register">/WEB-INF/pages/{1}success.jsp</result>
<result name="error">/WebRoot/WEB-INF/pages/error.jsp</result>
</action>
</package>
</struts>
6创建页面
error.jsp
loginsuccess.jsp
registersuccess.jsp
7 .完美运行
本文转自 素颜猪 51CTO博客,原文链接:http://blog.51cto.com/suyanzhu/1564325