Dwz + Struts2 完成input验证
来源:互联网 发布:景区票务系统源码 编辑:程序博客网 时间:2024/06/05 07:07
在做网页开发的时候,经常有这样的需求。就是用户在文本框中输入信息后,当焦点离开的时候,我们需要立刻对用户输入的信息进行有效性验证。那么,正常的情况下,要想实现这一功能,我们肯定是会用到ajax的,但是,如果你使用了DWZ,那么,要实现这一功能将不再那么繁琐。你不需要写任何ajax的代码,而仅仅需要在需要的验证的input标签上加入一个remote属性。下面给大家演示一个使用Dwz + Struts2 完成用户名验证的实例。
首先,看login.jsp页面代码
<%@page contentType="text/html; charset=utf-8"%><%@ include file="/page/common/common.jsp"%><div class="pageContent"><form class="pageForm required-validate"><div class="pageFormContent" layoutH="58"><div class="unit"><label>用户名:</label> <input type="text" id="userName" name="userName"value="" size="30" class="required " remote="" /></div><div class="unit"><label>密码:</label> <input type="text" name="password"value="" size="30" class="required" /></div></div></form></div><script defer>if($("#userName").attr("remote") != "undefined"){$("#userName").attr("remote","${contextPath }/user/validateUserAction.action?time="+ new Date().getTime());}</script>
解释一下,我为什么使用了最下面的javaScript代码。如果我直接直接把链接“${contextPath }/user/validateUserAction.action”写到input的remote属性中,那么会出现不提交的情况。我怀疑是缓存的事,不过没有去验证。当我在链接后面加上时间戳以后,这种情况就木有发生过。
Struts.XML 中代码
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts><package name="user" namespace="/user"extends="struts-default"><action name="validateUserAction" class="userAction" method="validateUser" ><result name="validate_false">/page/validate_false.html</result><result name="validate_true">/page/validate_true.html</result></action></package></struts>
PS:validate_false.html中只有false一个单词,同样validate_true.html里也只有true一个单词。
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:jee="http://www.springframework.org/schema/jee"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"><bean id="userAction" class="user.action.UserAction"scope="prototype"></bean></beans>
UserAction 代码
package user.action;import common.tool.web.BaseAction;public class UserAction extends BaseAction {private String userName="";private String password="";public String validateUser(){if("admin".equals(userName) ){return "validate_true";}return "validate_false";}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
效果图:
到此为止,验证的基本功能就完成了,不过,这样做还是有一点点缺陷的,就是不能自定义提示信息,这点还有待于完善。现在我还没有找实现自定义提示的方法。如果碰巧看博客的你知道如何实现,希望留下实现的方法。在此,提前谢谢了。
4 0
- Dwz + Struts2 完成input验证
- Struts2 验证错误 不想返回input
- struts2验证出错总是跳转到input
- struts2验证出错总是跳转到input
- Struts2中使用框架完成自动验证
- DWZ 验证 CLASS 规则
- DWZ 验证 class
- (复习)Struts2+MySQL完成登录和验证(一)
- DWZ验证表单规则一览
- DWZ表单验证规则一览表
- ]DWZ表单验证规则一览表
- DWZ表单自定义验证规则
- Struts2 input
- dwz javascript 修改 input的文本内容
- dwz javascript 修改 input的文本内容
- 完成验证
- 解决DWZ IE10 表单验证页面
- thinkphp dwz 的数据自动验证
- 83. 测试是软件开发的工程严谨度
- APEV
- ORACLE空间管理实验8:数据块格式分析--DUMP结合BBED等
- ORACLE空间管理实验1:探索LMT表空间管理下数据文件头的结构及位图中区的记录方式
- 使用CSS 客户化 SharePoint 2013 中的 promoted links 样式
- Dwz + Struts2 完成input验证
- html常用变量整理
- Chinese En/Decode for filesystem
- hashmap的内部实现机制
- NBUT 1537 Travel
- 编译并安装vim
- NO9.Java笔记【多线程】
- C/C++回调函数简要介绍
- android开发之多线程实现方法概述