action中的执行方法
1. Action中默认的执行方法是execute方法
2. 在struts.xml为Action配置method属性
3. DMI(Dynamic Method Invocation - 动态方法调用)
4. 使用通配符(wildcard mappings)映射方式
5. 提交按钮指定提交方法
下面将分别介绍这几种方法,并附上自己测试过程的配置和结果的截图,希望对初学者能够有所帮助
在我的测试过程中,项目名称为mySSH
1. Action中默认的执行方法是execute()方法
这个方法执行请求,然后转向其他的页面,这是常规的做法
1). struts.xml
<action name="login"class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
<action name="register" class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/register.jsp</result>
</action>
2). JSP pages
login.jsp: http://localhost:8080/mySSH/method/login.jsp
<s:form method="post"action="login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="登录"></s:submit>
</s:form>
register.jsp: http://localhost:8080/mySSH/method/register.jsp
<s:form method="post"action="register">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="注册"></s:submit>
</s:form>
3). Action:com.zhb.myssh.struts.method.action.ActionMethodAction
@Override
public String execute() throws Exception {
ActionContext context = ActionContext.getContext();
// get current action name
String actionName =context.getActionInvocation().getProxy().getActionName();
System.out.println("actionName: " + actionName);
if ("login".equals(actionName)) {
if ("admin".equals(this.username) && "123".equals(this.password)) {
message = "登录成功, 欢迎 " + username;
if (context.getSession().get("uName") != null) {
message = this.username + ": 您已经登录过了!";
}else {
context.getSession().put("uName", this.username);
}
return SUCCESS;
}else {
message = "登录失败, 用户名或密码错误!";
return INPUT;
}
} else if ("register".equals(actionName)) {
//将用户名,密码添加到数据库中......
System.out.println("Register new user!");
message = "恭喜你,注册成功!";
return SUCCESS;
}
return SUCCESS;
}
请参考附件图片execute_XXX.png
2. 在struts.xml为Action配置method属性
1). struts.xml
<action name="login" class="com.zhb.myssh.struts.method.action.ActionMethodAction"method="login">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
<action name="register" class="com.zhb.myssh.struts.method.action.ActionMethodAction"method="register">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
2). JSP pages
login.jsp: http://localhost:8080/mySSH/method/login.jsp
<s:form method="post"action="login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="登录"></s:submit>
</s:form>
register.jsp: http://localhost:8080/mySSH/method/register.jsp
<s:form method="post"action="register">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="注册"></s:submit>
</s:form>
3). Action:com.zhb.myssh.struts.method.action.ActionMethodAction
public String login() throws Exception {
if ("admin".equals(this.username) && "123".equals(this.password)) {
message = "登录成功, 欢迎 " + username;
......
return SUCCESS;
} else {
message = "登录失败, 用户名或密码错误!";
return INPUT;
}
}
public String register() throws Exception {
// 将用户名,密码添加到数据库中......
... ...
message = "恭喜你,注册成功!";
return SUCCESS;
}
结果图同execute方法执行
3. DMI(Dynamic Method Invocation - 动态方法调用)
1). struts.xml
<action name="login" class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
<action name="register" class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/register.jsp</result>
</action>
2). JSP page
login.jsp: http://localhost:8080/mySSH/method/login.jsp
<s:form method="post"action="login!login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码"></s:textfield>
<s:submit value="登录"></s:submit>
</s:form>
register.jsp: http://localhost:8080/mySSH/method/register.jsp
<s:form method="post"action="register!register">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码"></s:textfield>
<s:submit value="注册"></s:submit>
</s:form>
3). Action:com.zhb.myssh.struts.method.action.ActionMethodAction
public String login() throws Exception {
if ("admin".equals(this.username) && "123".equals(this.password)) {
message = "登录成功, 欢迎 " + username;
......
return SUCCESS;
} else {
message = "登录失败, 用户名或密码错误!";
return INPUT;
}
}
public String register() throws Exception {
// 将用户名,密码添加到数据库中......
... ...
message = "恭喜你,注册成功!";
return SUCCESS;
}
请参考附件图片DMI_XXX.png
4. 使用通配符(wildcard mappings)映射方式:
在struts.xml文件中配置<action…>元素时,它的name、class、method属性都可支持通配符,这种通配符的方式是另一种形式的动态方法调用。
当我们使用通配符定义Action的name属性时,相当于用一个元素action定义了多个逻辑Action:
通配符支持:
<action name="*" class="com.zhb.myssh.struts.method.action.{1}Action" method="login"></action>
<action name="*_*" class="com.zhb.myssh.struts.method.action.{1}Action" method="{2}"></action>
1). struts.xml
<action name="user_*" class="com.zhb.myssh.struts.method.action.ActionMethodAction"method="{1}">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
如上,<actionname="user_*">定义一系列请求URL是user开头的Action, 同时method属性值为一个表达式{1},表示它的值是action name属性值中第一个*的值, 这样就可以动态执行请求的方法。
2). JSP page
login.jsp: http://localhost:8080/mySSH/method/login.jsp 用户请求URL为user_login.action时,将调用到UserAction类的login方法
<s:form method="post"action="user_login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="登录"></s:submit>
</s:form>
register.jsp: http://localhost:8080/mySSH/method/register.jsp 用户请求URL为user_regist.action时,将调用到UserAction类的regist方法
<s:form method="post"action="user_register">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码" ></s:textfield>
<s:submit value="注册"></s:submit>
</s:form>
3). Action:com.zhb.myssh.struts.method.action.ActionMethodAction
public String login() throws Exception {
if ("admin".equals(this.username) && "123".equals(this.password)) {
message = "登录成功, 欢迎 " + username;
......
return SUCCESS;
} else {
message = "登录失败, 用户名或密码错误!";
return INPUT;
}
}
public String register() throws Exception {
// 将用户名,密码添加到数据库中......
... ...
message = "恭喜你,注册成功!";
return SUCCESS;
}
请参考附件图片wildcard_XXX.png
5. 提交按钮指定提交方法
普通的提交按钮我们会这么写: <s:submit value="提交"></s:submit>
当我们想提交到我们指定的方法时我们可以在这个标签里添加一个method属性指定要提交的方法
1). struts.xml
<action name="login"class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/login.jsp</result>
</action>
<action name="register" class="com.zhb.myssh.struts.method.action.ActionMethodAction">
<result name="success">/method/success.jsp</result>
<result name="error">/method/error.jsp</result>
<result name="input">/method/register.jsp</result>
</action>
2). JSP page
login.jsp: http://localhost:8080/mySSH/method/login.jsp
<s:form method="post"action="login">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码"></s:textfield>
<s:submit value="登录" method="login"></s:submit>
</s:form>
register.jsp: http://localhost:8080/mySSH/method/register.jsp
<s:form method="post"action="register">
<s:textfield name="username"label="用户名"></s:textfield>
<s:textfield name="password"label="密 码"></s:textfield>
<s:submit value="注册" method="register"></s:submit>
</s:form>
3). Action: com.zhb.myssh.struts.method.action.ActionMethodAction
public String login() throws Exception {
if ("admin".equals(this.username) && "123".equals(this.password)) {
message = "登录成功, 欢迎 " + username;
......
return SUCCESS;
} else {
message = "登录失败, 用户名或密码错误!";
return INPUT;
}
}
public String register() throws Exception {
// 将用户名,密码添加到数据库中......
... ...
message = "恭喜你,注册成功!";
return SUCCESS;
}
同execute图片