This is often the case when developing web pages. After the user enters the information in the text box, when the focus leaves, we need to immediately verify the validity of the information entered by the user. Under normal circumstances, to implement this function, we will certainly use ajax. However, if you use DWZ, it is no longer so cumbersome to implement this function. You don't need to write any ajax code, but you just need to add a remote attribute to the input tag for verification. The following shows an instance that uses Dwz + Struts2 for user name verification.
First, check the login. jsp page code.
<% @ Page contentType = "text/html; charset = UTF-8" %> <% @ include file = "/page/common. jsp "%> <div class =" pageContent "> <form class =" pageForm required-validate "> <div class =" pageFormContent "layoutH =" 58 "> <div class = "unit"> <label> User Name: </label> <input type = "text" id = "userName" name = "userName" value = "" size = "30" class = "required" remote = ""/> </div> <div class = "unit"> <label> password: </label> <input type = "text" name = "Password" value = "" size = "30" class = "required"/> </div> </form> </div> <script defer> if ($ ("# userName "). attr ("remote ")! = "Undefined") {$ ("# userName"). attr ("remote", "$ {contextPath}/user/validateUserAction. action? Time = "+ new Date (). getTime () ;}</script>
Explain why I used the bottom javaScript code. If I directly write the "$ {contextPath}/user/validateUserAction. action" link to the remote attribute of the input, no submission will occur. I suspect it is a cache issue, but I did not verify it. After I add a timestamp to the link, this happens.
Code in 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 only has one falseword. In the same sample, validate_true.html only has one word.
Code for configuring action
<?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 code
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;}}
:
So far, the basic functions of the verification have been completed. However, this is still a bit flawed, that is, the prompt information cannot be customized, which remains to be improved. Currently, I have not found a method to implement custom prompts. If you happen to be reading a blog, you know how to implement it and want to leave the implementation method behind. Thank you in advance.