Struts2的Action類詳解(深入表單登入並驗證)
為了讓使用者實現更加規範地開發Action,Struts2提供了一個Action介面,該介面定義了Struts2的Action(業務邏輯控制器)的規範,
下面是Action介面的程式碼:
public interface Action{
public static final String SUCCESS="success";
public static final String NONE="none";
public static final String ERROR="error";
public static final String INPUT="input";
public static final String LOGIN="login";
public String execute()throws Exception;
}
而且:Struts2還提供了Action介面的實現子類,ActionSupport類,以後想要使用Servlet的API中的(HttpSession,HttpServletRequest,ServletContext)
標準的登入頁面(由Action的子類實現驗證)
1;登入頁面(jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'input.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="input.action" method="post">
<table border="1" align="center">
<tr>
<td colspan="2" align="center">登入</td>
</tr>
<tr>
<td>使用者名稱:</td>
<td ><input type="text" name="username"/></td>
</tr>
<tr>
<td>密 碼:</td>
<td ><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2" align="left"><input type="submit" value="登入"/></td>
</tr>
</table>
</form>
</body>
</html>
2:xml進行過濾
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>StrutsTest1</display-name>
<!-- 配置struts2的過濾器 -->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3;struts.xml(進行控制提交給不同Action子類)
<?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>
<package name="struts2login" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="success" >/result.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="input" class="com.test.action.InputAction">
<result name="success" >/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
4:Action類(業務邏輯Action)繼承 ActionSupport,負責後臺驗證
package com.test.action;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class InputAction extends ActionSupport{
private static final long serialVersionUID = 1L;
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 execute() throws Exception {
ActionContext ac=ActionContext.getContext();
if("admin".equals(username)&&"123456".equals(password))
{
ac.put("success","登陸成功");
return Action.SUCCESS;
}
else
{
ac.put("error", "使用者名稱錯誤或密碼錯誤");
return Action.ERROR;
}
}
}
5:驗證成功返回的頁面(jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>登陸成功!</h2>
使用者名稱:${requestScope.username }<br>
密碼:${requestScope.password }<br>
</body>
</html>
6:驗證失敗返回的頁面(jsp)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'result.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h2>登陸失敗!</h2><hr/>
<a href="input.jsp">重新登入</a>
</body>
</html>