Struts2 简单的注册验证

          今天又来和大家分享今天的学习成果,希望大家多多批评。

          其实Struts2注册验证的方法有很多,今天我给大家介绍的是继承Action Support类的方法;ActionSupport类本身实现了Action接口,所以继承ActionSupport类就相当于实现了Action接口。除此之外,ActionSupport类还实现了其它几个接口,来为程序员提供更多使用的功能,这些接口和Struts2的一些其他特性相结合,可以实现基本的数据验证功能和国际化。接口如下所示:

com.opensymphony.xwork2.Validateable;  //提供validate()方法来为Action增加验证的功能
com.opensymphony.xwork2.Validateaware; //提供方法来保存和恢复action或field级的错误信息
com.opensymphony.xwork2.TextProvider;  //提供获取本地信息文本的功能
com.opensymphony.xwork2.LocaleProvider;//提供getLocale()方法来获取本地消息

1.新建HelloWorldAction类进行基本的数据验证   

要实现数据验证的功能,只需要在Action类中覆盖实现validate方法即可;在validate方法内部,对请求传递过来的数据进行校验,如果不满足要求,那么添加例外信息到父类用于存放例外的集合中。

 

package cn.javass.hello.struts2impl.action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    public void validate(){  
        if(account==null || account.trim().length()==0){  
            this.addFieldError("account", "账号不可以为空");  
        }  
        if(password==null || password.trim().length()==0){  
            this.addFieldError("password", "密码不可以为空");  
        }
        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
            this.addFieldError("password", "密码长度至少为6位");  
        }  
    }  
    /** 
     * 示例方法,表示可以执行业务逻辑处理的方法, 
     */  
    public void businessExecute(){  
        System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);  
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSubmitFlag() {
        return submitFlag;
    }
    public void setSubmitFlag(String submitFlag) {
        this.submitFlag = submitFlag;
    }  
    
}  

2.修改struts.xml中的Action配置

这里我们就发现了,validate方法是没有返回值的,这时就需要在struts.xml中的Action配置里面,添加一个名称为input的result配置,也就是说,如果validate方法中,有数据没有通过验证,那么会自动跳转回到该action中名称为input的result所配置的页面。

图片中选中的带代码便是我们需要添加的代码了 完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" 
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="default" extends="struts-default" >
	
		<action name="register"
		        class="com.hnpi.action.RegisterAction" method="execute">
		       <result name="success">/index.jsp</result>
		</action>
		<action name="helloworldAction" 
		        class="cn.javass.hello.struts2impl.action.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/login.jsp</result>   
        </action>  
		
	</package>
	
</struts>

3.添加新的显示页面

创建jsp页面的方法参考我的上一个博客

login.jsp页面代码:

<%@ page language="java" contentType="text/html; charset=utf-8"  
    pageEncoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html; utf-8">  
<title>Insert title here</title>  
	<style type="text/css">
		ul,li {
		    list-style-type:none;
		    margin:0px;
		    float:left;
		}
	</style>
</head>  
<body>  
   
<form action="helloworldAction" method="post"> 
    <input type="hidden" name="submitFlag" value="login"/>  
    <div> 
        <font color=red><s:fielderror fieldName="account"/></font>
        <br/>
          账号:<input type="text" name="account">
    </div>
    <div>
        <font color=red><s:fielderror fieldName="password"/></font>
        <br/>
            密码:<input type="password" name="password">
    </div>
    <input type="submit" value="提交">  
</form>  
  
</body>  
</html>

welocome.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>
    
    <title>欢迎页面</title>

  </head>
  
  <body>
       登录成功!
  </body>
</html>

最后个大家放两张效果图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值