概述
可以将一个Action类定义成多个逻辑Action即Action的多个处理逻辑方法都能都能映射成一个逻辑Action,前提是这些方法具有相似的方法签名。 方法签名:方法形参列表为空,方法返回值为String。
<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>
处理逻辑通过method方法指定;
如果class方法被省略,这默认使用ActionSupport作为处理类。
如果method方法被省略,这默认使用execute方法处理请求。
下面为详细的代码:
<?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>
<?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>
<%@ 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>
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;
}
}
<%@ 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属性所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复