博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts对输入数据的校验
阅读量:4590 次
发布时间:2019-06-09

本文共 4294 字,大约阅读时间需要 14 分钟。

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

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" οnfοcus="change1(this)" autocomplete="off"/>
</label><br />
<label class="lab" id="lab2">
<span id="span2">用户密码</span>
<input type="password" name="userpass" class="input" id="input2" οnfοcus="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()方法。

 

转载于:https://www.cnblogs.com/dbqjava/p/4380693.html

你可能感兴趣的文章
9.17模拟赛2.0
查看>>
洛谷 P3225 [HNOI2012]矿场搭建
查看>>
orcad找不到dll
查看>>
各种排序算法的性能特点
查看>>
LET IT BE
查看>>
在线帮助你修改图片背景的工具 - Clipping Magic
查看>>
BizTalk动手实验(十三)EDI解决方案开发配置
查看>>
初学github
查看>>
iOS开发拓展篇—UIDynamic(重力行为+碰撞检测)
查看>>
extjs 下载文件 关键前后端代码
查看>>
.NET 4.0 兼容 .NET 2.0 的方法
查看>>
1001 Maximum Multiple(2018 Multi-University Training Contest 1)
查看>>
对Java对象的认识与理解
查看>>
python——父类与子类的一些说明
查看>>
2019年3月3日 2018-2019-2 20189205《移动平台应用开发实践》第二周作业
查看>>
MySQL 性能优化--优化数据库结构之优化数据类型
查看>>
软件工程之软件需求分析
查看>>
Electron简介和安装使用
查看>>
Improving Visual C++ Debugging with Better Data Display
查看>>
JDBC
查看>>