当我们在登录或者是注册时需要对用户输入的数据验证,以前都是浏览器传送数据到后台,后台对数据进行校验,如果有错误就带着错误信息转发带登录或者注册页面,
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">登 录</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()方法。