我是靠谱客的博主 懵懂牛排,这篇文章主要介绍指定method属性,现在分享给大家,希望可以做个参考。

可以将一个Action类定义成多个逻辑Action即Action的多个处理逻辑方法都能都能映射成一个逻辑Action,前提是这些方法具有相似的方法签名。 方法签名:方法形参列表为空,方法返回值为String。

复制代码
1
2
3
4
5
6
7
8
9
10
11
<package name="' namespace="" extends=""> <action name="login" class="Demo.LoginAction" method="login"> <result name="success">/WEB-INF/content/welcome.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action> <action name="regiest" class="Demo.LoginAction" method="regiest"> <result name="success">/WEB-INF/content/welcome.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action> </package>
复制代码
1
2
3
4
处理逻辑通过method方法指定; 如果class方法被省略,这默认使用ActionSupport作为处理类。 如果method方法被省略,这默认使用execute方法处理请求。

下面为详细的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>method</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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> <constant name="struts.devMode" value="true"/> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <package name="Demo" namespace="/" extends="struts-default"> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> <action name="login" class="Demo.LoginAction" method="login"> <result name="success">/WEB-INF/content/welcome.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action> <action name="regiest" class="Demo.LoginAction" method="regiest"> <result name="success">/WEB-INF/content/welcome.jsp</result> <result name="error">/WEB-INF/content/error.jsp</result> </action> </package> </struts>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<%@ page language="java" contentType="text/html; charset=UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>登录页面</title> </head> <body> <form action="login" method="post"> 用户名<input type="text" name="username"/><br/> 密 码<input type="text" name="password"/><br/> <input type="submit" value="登录" onclick="this.form.action='login';"/> <input type="submit" value="注册" onclick="regiest()"/> </form> </body> <script type="text/javascript"> function regiest(){ var tagForm = document.forms[0]; tagForm.action="regiest"; } </script> </html>
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package Demo; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String regiest()throws Exception{ ActionContext.getContext().getSession().put("username", getUsername()); ActionContext.getContext().getSession().put("password", getPassword()); addActionMessage("恭喜您,"+getUsername()+",您已经注册成功!"); return SUCCESS; } public String login(){ String user = (String)ActionContext.getContext().getSession().get("username"); String pass = (String)ActionContext.getContext().getSession().get("password"); if(getUsername().equals(user)&&getPassword().equals(pass)){ addActionMessage("欢迎,"+user+",您已经登录成功!"); return SUCCESS; } return ERROR; } }
复制代码
1

<%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html> <head> <title>成功页面</title> </head> <body> <s:actionmessage/> </body> </html> ``` ``` <%@ page language="java" contentType="text/html; charset=UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>错误页面</title> </head> <body> ``` 对不起,您不能登录! </body> </html> ```

转载于:https://my.oschina.net/tyILOVE/blog/880343

最后

以上就是懵懂牛排最近收集整理的关于指定method属性的全部内容,更多相关指定method属性内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部