Struts2的Action类详解(深入表单登录并验证)

来源:互联网 发布:超星尔雅刷课软件 编辑:程序博客网 时间:2024/06/05 00:32

为了让用户实现更加规范地开发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>密&nbsp;码:</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>

0 0