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>密 码:</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>
- Struts2的Action类详解(深入表单登录并验证)
- struts2 表单验证 - action类中的validate()函数
- Struts1学习笔记(4)-----用Action表单创建登陆程序并简单的验证
- Struts2 的表单验证
- Struts2 的表单验证
- struts2表单验证的fielderror不显示的问题(从一个Action chain调用另一个Action)
- Struts的Action表单验证
- Struts2 In Action读书笔记一------------简单的表单验证和属性注入
- struts2服务器端的表单验证
- Struts2的表单数据验证
- 表单的验证详解
- struts2获取当前方法的Action的Url 登录验证拦截器实现
- Struts2中ajax结合action实现表单时时验证
- struts2标签 表单验证错误,再次提交不能进入action
- 表单验证(登录/注册)
- Struts2基础(Action类应用详解)
- struts2 Action获取表单传值(属性,类))
- struts2 登录(无验证)
- 存储过程和函数的区别
- 洛谷 1417
- Anaconda的使用
- c#在DataGridView中插入一列
- 学习资源整理分享
- Struts2的Action类详解(深入表单登录并验证)
- android fastboot模式下载以及出现的问题
- eclipse代码迁移到Android Studio
- Eclipse 注释设置
- redis学习
- [模板] 数论
- Windows 10 File Explorer打不开的问题解决方案
- DokuWiki 安装
- Flink流处理之窗口算子分析