目录结构
form的 validate="false"时服务端验证的结果,其中pass111是Login-validation.xml返回的结果。
Login.java
- Java code
package org.blackice.news;import com.opensymphony.xwork2.ActionSupport;public class Login extends ActionSupport { static final long serialVersionUID = 5465432167631354L; private String username; private String password; public String getUsername(){ return this.username; } public void setUsername(String username){ this.username = username; } public String getPassword(){ return this.password; } public void setPassword(String password){ this.password = password; } public String execute(){ if(this.getUsername()==null||this.getUsername().equals("")){ this.addFieldError("username", "请输入用户名"); return INPUT; } if(this.getPassword()==null||this.getPassword().equals("")){ this.addFieldError("password", "请输入密码"); return INPUT; } return SUCCESS; } public void validate(){ if(this.getUsername()==null||this.getUsername().equals("")){ this.addFieldError("username", "请输入用户名"); } if(this.getPassword()==null||this.getPassword().equals("")){ this.addFieldError("password", "请输入密码"); } }}
Login-validation.xml
- XML code
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"><validators> <field name="username"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>User</message> </field-validator> </field> <field name="password"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>pass</message> </field-validator> </field></validators>
struts.xml
- XML code
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN""http://struts.apache.org/dtds/struts-2.0.dtd"><struts><constant name="struts.custom.i18n.resources" value="messageResource"></constant><package name="org.blackice.news" extends="struts-default"> <action name="HelloWorld" class="org.blackice.news.HelloWorld"> <result>/HelloWorld.jsp</result> </action> <action name="Login" class="org.blackice.news.Login"> <result name="input">Login.jsp</result> <result name="success">index</result> </action></package></struts>
validators.xml
- XML code
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator Config 1.0//EN" "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd"><!-- START SNIPPET: validators-default --><validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/> <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/></validators>