当我们在登录或者是注册时需要对用户输入的数据验证,以前都是浏览器传送数据到后台,后台对数据进行校验,如果有错误就带着错误信息转发带登录或者注册页面,

struts可以简便的对输入数据进行校验

首先我们先来建立一个input.jsp 用作登录页面 下面是源代码 js代码没有优化,若您感觉不爽的话希望您不吝赐教,感激不尽

<%@ 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 '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">

<style type="text/css">
*{
font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;
}
.input{
border-width : 0px;
outline: none;
width:280px;
margin-left: 10px;
height:36px;
color:#888;
font-size:18px;

}
.lab{
display : block;
height : 36px;
width: 300px;
border: solid 1px #ccc;
position:relative;
}
#login{

display : block;
position: absolute;
width : 302px;
height : 38px;
background-color: #1C86EE;
margin-top : -20px;
text-align: center;
line-height: 36px;
size:21px;
color: #FFF;
font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;
text-decoration: none;

}
#login:HOVER {

background: #1E90FF;
}

span{

position:absolute;
float:left;
line-height:40px;
left:12px;
color:#CDCDCD;
cursor:text;
font-family: "Arial","Microsoft YaHei","黑体","宋体",sans-serif;
font-size: 18px;
}
</style>

<script type="text/javascript">
//第一个输入框获得焦点
function change1(input1){
document.onkeydown = function(){

if(input1.value==""){
document.getElementById("span1").style.display="block";
}else{
document.getElementById("span1").style.display="none";
}
};
document.onkeyup = function(){

if(input1.value==""){
document.getElementById("span1").style.display="block";
}else{
document.getElementById("span1").style.display="none";
}
};
}

function change2(input1){
document.onkeydown = function(){

if(input1.value==""){
document.getElementById("span2").style.display="block";
}else{
document.getElementById("span2").style.display="none";
}
};
document.onkeyup = function(){

if(input1.value==""){
document.getElementById("span2").style.display="block";
}else{
document.getElementById("span2").style.display="none";
}
};
}

</script>
</head>

<body>
<div style="width:300px;height:200px;margin:50px auto;">
<form action="" method="" id="" name="">
<label class="lab" id="lab1">
<span id="span1">用户名/邮箱账号/手机号码</span>
<input type="text" name="username" class="input" id="input1" onfocus="change1(this)" autocomplete="off"/>
</label><br />
<label class="lab" id="lab2">
<span id="span2">用户密码</span>
<input type="password" name="userpass" class="input" id="input2" onfocus="change2(this)"/>
</label><br />
</form>
<a href="#" id="login">登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</a>
</div>

</body>
</html>

运行效果如下

下面我们就来建立action

package com.day06;

public class Validate {

private String username;
private String userpass;

public void setUsername(String username){
this.username = username;
}
public void setUserpass(String userpass){
this.userpass = userpass;
}
public String getUsername(){
return this.username;
}
public String getUserpass(){
return this.userpass;
}

public String login(){
return "success";
}
}

配置struts.xml

<package name="day06" namespace="/day06" extends="struts-default">
<action name="login" class="com.day06.Validate" method="login">
<result name="success">/success.jsp</result>
</action>
</package>

下面我们就来对输入进行校验

首先action类继承 ActionSupport 类重写 validate()方法

@Override
public void validate() {
if(username.trim().equals("")||username.trim()==null){
this.addFieldError("username", "用户名不能为空");
}
if(userpass.trim().equals("")||userpass.trim()==null){
this.addFieldError("userpass", "密码不能为空");
}
}

然后我们在struts,xml中加入input.jsp

<package name="day06" namespace="/day06" extends="struts-default">
<action name="login" class="com.day06.Validate" method="login">
<result name="success">/success.jsp</result>
<result name="input">/input.jsp</result>
</action>
</package>

Struts对指定的方法进行校验只需要改变validate()方法名 若要对execute()方法校验则 改为

validateExecute() 则只校验execute()方法。

Struts对输入数据的校验的更多相关文章

  1. Struts 2的数据校验

    既然说到了Struts 2的数据校验,我们该怎么去实现呢?又是通过什么来实现呢? 就让我带着大家一起来走进Struts 2的数据校验吧. 首先我们会想到在Stuts 2的登录案例中我们定义了一个Act ...

  2. struts中的数据校验

    1.struts中如何进行数据校验 在每一个Action类中,数据校验一般都写在业务方法中,比如login().register()等.struts提供了数据校验功能.每个继承自ActionSuppo ...

  3. struts中简单的校验

    Struts中简单的校验 “计应134(实验班) 凌豪” Struts2校验简要说明:struts2中通常情况下,类型转换要在数据校验之前进行.类型转换其实也是基本的服务器端校验,合法数据必然可以通过 ...

  4. struts 简单前台用户名校验

    一个jsp <?xml version="1.0" encoding="GB18030" ?> <%@ page language=" ...

  5. Struts 2 数据校验要用到的类和两种校验方式以及一些校验问题的解决

    通过继承ActionSupport类来完成Action开发,ActionSupport类不仅对Action接口进行简单实现, 同时增加了验证.本地化等支持 .真实开发中自定义Action都需要继承该类 ...

  6. Struts2输入校验(编码方式)

    struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: ...

  7. Struts2(五)数据校验

    一.概述 在提交表单数据时,如果数据需要保存到数据库,空输入等可能会引发一些异常,为了避免引起用户的输入引起底层异常,通常在进行业务逻辑操作之前,先执行基本的数据校验. 下面通过两种方式来阐述Stru ...

  8. Struts2 更改校验配置文件位置

    @(Java)[Struts|Interceptor] Struts2 更改校验配置文件位置 在Struts2中提供的拦截器校验ValidationInterceptor,该校验器中默认的配置文件位于 ...

  9. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)

    轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...

随机推荐

  1. 从A页面跳转到B页面,从B页面按浏览器自带按钮返回到A页面并且刷新页面--手机操作浏览器自带返回并自带刷新

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  2. deployd使用归纳

    deployd:一个生成后端数据的软件,简单的说就是大部分的前端不会后端,即使会也很难在深入到数据库进行设置一些前端所需数据的创建与查询的后端程序的书写,所以此时就是deployd大显身手的时候了. ...

  3. iOS二维码生成与识别

    在 IOS7 以前,在IOS中实现二维码和条形码扫描,有两大开源组件 ZBar 与 ZXing. 总结下各自的缺点: ZBar在扫描的灵敏度上,和内存的使用上相对于ZXing上都是较优的,但是对于 & ...

  4. Unity3DGUI:人物能量条

  5. ubuntu 14.04中文分词 结巴分词

    在自然语言处理中,很常见的是要对文本数据进行分词处理.博主是代码小白,目前只是用python作为数据预处理的工具,而按照结巴中文分词的导语:做最好的python中文分词组件“jieba”.因而博主也就 ...

  6. 安卓用canvas画曲线图

    1.新建一个常变量类Constant.java package com.rain.db; import android.graphics.Point; public class Constant { ...

  7. 6.MyBaits的分页和缓存查询

    1. 创建javaweb项目MyBaits_Page_CaChe 2.在项目的WebRoot下的WEB-INF下的lib文件下加入jar文件 log4j-1.2.17.jar mybatis-3.2. ...

  8. svn checkout操作

    svn checkout https://svn.com/svn/project 该操作从svn服务器上拉代码下来,并且建立本地和远端的文件对应,状态的关联. 1,和export的区别 svn检出操作 ...

  9. 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误

    修改Web.config,增加requestValidationMode="2.0"属性值 <system.web> <httpRuntime requestVa ...

  10. jQuery 源码学习(先放在这,未开始)

    希望对源码有一个框架上认识,对整体结构有一个理解. 对外只暴露出了一个变量,$/jQuery,这个变量指向一个函数 function(a,b){return new n.fn.init(a,b)}