Struts对输入数据的校验
当我们在登录或者是注册时需要对用户输入的数据验证,以前都是浏览器传送数据到后台,后台对数据进行校验,如果有错误就带着错误信息转发带登录或者注册页面,
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">登 录</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对输入数据的校验的更多相关文章
- Struts 2的数据校验
既然说到了Struts 2的数据校验,我们该怎么去实现呢?又是通过什么来实现呢? 就让我带着大家一起来走进Struts 2的数据校验吧. 首先我们会想到在Stuts 2的登录案例中我们定义了一个Act ...
- struts中的数据校验
1.struts中如何进行数据校验 在每一个Action类中,数据校验一般都写在业务方法中,比如login().register()等.struts提供了数据校验功能.每个继承自ActionSuppo ...
- struts中简单的校验
Struts中简单的校验 “计应134(实验班) 凌豪” Struts2校验简要说明:struts2中通常情况下,类型转换要在数据校验之前进行.类型转换其实也是基本的服务器端校验,合法数据必然可以通过 ...
- struts 简单前台用户名校验
一个jsp <?xml version="1.0" encoding="GB18030" ?> <%@ page language=" ...
- Struts 2 数据校验要用到的类和两种校验方式以及一些校验问题的解决
通过继承ActionSupport类来完成Action开发,ActionSupport类不仅对Action接口进行简单实现, 同时增加了验证.本地化等支持 .真实开发中自定义Action都需要继承该类 ...
- Struts2输入校验(编码方式)
struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: ...
- Struts2(五)数据校验
一.概述 在提交表单数据时,如果数据需要保存到数据库,空输入等可能会引发一些异常,为了避免引起用户的输入引起底层异常,通常在进行业务逻辑操作之前,先执行基本的数据校验. 下面通过两种方式来阐述Stru ...
- Struts2 更改校验配置文件位置
@(Java)[Struts|Interceptor] Struts2 更改校验配置文件位置 在Struts2中提供的拦截器校验ValidationInterceptor,该校验器中默认的配置文件位于 ...
- 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)(国家级奖项获奖作品升级版,四版累计印刷27次发行量超10万册的轻量级Jav ...
随机推荐
- LeetCode 476. Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- Hololens生成与安装(旁加载)应用
Hololens生成应用的几种方式: 一:HoloToolkit编辑器生成appx应用 二:Vistul Studio 2015 创建应用 旁加载概述: 你可以将应用旁加载到你的设备,而无需将它们提交 ...
- 7.MyBatis延时加载
1.创建javaWeb项目MyBatis_Lazy并在WebRoot下的WEB-INF下的lib下添加如下jar文件 cglib-nodep-2.1_3.jar log4j-1.2.17.jar my ...
- Vimperator技巧
Vimperator技巧 什么是Vimperator?Firefox的一个插件,模拟vim操作. 1. 用]]打开"下一页"链接,[[打开"上一页"Vimper ...
- C# 语言规范_版本5.0 (第15章 委托)
1. 委托 **(注:此章非常重要,特别是对于图形界面相关的区别于MFC和QT等的消息机制,委托是基石.) 委托是用来处理其他语言(如 C++.Pascal 和 Modula)需用函数指针来处理的情况 ...
- 使用vscode对c进行调试
最近在学习C语言.知道vscode支持对c语言的代码的调试,就想试试.然后找了教程: https://code.visualstudio.com/docs/languages/cpp http://w ...
- extjs 6.2 helloworld
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- @property、@synthesize和dynamic的用法
原文: http://blog.csdn.net/hherima/article/details/8622948 @代表“Objective-C”的标志,证明您正在使用Objective-C语言 O ...
- iOS 最新App提交上架流程及部分问题的解决方案2016.12.21,感谢原博主!!!
内容摘自http://www.cocoachina.com/bbs/3g/read.php?tid=330302,原博特别详细,下面我对部分地方进行了修改,主要是对在打包验证和上传的时候遇到的问题进行 ...
- 移动端audio自动播放问题
中秋临近,心血来潮想做个手机端贺卡,以前接触的移动端较少,虽然是个简单的贺卡,其实也蛮多坑的,简略说一下在制作贺卡的过程遇到的坑: 一:移动端的屏幕大小不能算作body的大小,因为手机浏览器头部都有网 ...