Struts2学习笔记 - Action篇<定义逻辑Action>

时间:2023-01-05 08:21:17

有三种方法可以使一个Action处理多个请求

  1. 动态方法调用DMI
  2. 定义逻辑Acton
  3. 在配置文件中使用通配符

这文章就谈论一下定义逻辑Action

这里主要关注的是struts.xml配置文件,一般情况下,我们会这样编写一个Action,当请求的名字为login的时候,就会调用LoginAction的类里面的execute方法

<action name="login" class="struts2.action.LoginAction">
<result name="success">/success.jsp</result>
<result name="input">/login2.jsp</result>
</action>

在这里我们只需要多个一个method的属性就能实现处理多请求,如下图,这样我们就可以在实现类DMIAction中分别请求两个不同的方法login和register实现不同的逻辑

<action name="D_Login" class="struts2.action.DMIAction" method="login">
<result name="success">RegisterSuccess.jsp</result>
</action> <action name="D_Register" class="struts2.action.DMIAction" method="register">
<result name="success">RegisterSuccess.jsp</result>
</action>

下面是完整例子

1.两个页面分别请求两个不同的请求,D_Login和D_Register

//DMI_login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>请输入</h4>
<s:form action="D_Login">
<s:textfield name="username" label="User Name" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
</s:form> </body>
</html>
//DMI_register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>注册</h4>
<s:form action="D_Register">
<s:textfield name="username" label="User Name" />
<s:password name="password" label="Password" />
<s:submit value="Register" />
</s:form>
</body>
</html>

2.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.enable.DynamicMethodInvocation" value="true"/>
<package name="default" extends="struts-default">
<action name="D_Login" class="struts2.action.DMIAction" method="login">
<result name="success">RegisterSuccess.jsp</result>
</action> <action name="D_Register" class="struts2.action.DMIAction" method="register">
<result name="success">RegisterSuccess.jsp</result>
</action> </package>
</struts>

3.Action类

package struts2.action;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

public class DMIAction extends ActionSupport {

    /**
*
*/
private static final long serialVersionUID = 1L; public String login() {
return SUCCESS;
} public String register() {
return SUCCESS;
} 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;
} private Map sessionMap;
private Map applicationMap;
public void setSession(Map sessionMap) {
this.sessionMap = sessionMap;
} public void setApplication(Map applicationMap) {
this.applicationMap = applicationMap;
}
}