Use Struts2 to realize the verification function

The built-in interceptor of Struts2 provides us with a series of functions, including the verification function. The specific steps to use Struts2 to complete the verification function are as follows (take registration as an example):
Step 1: Write the jsp page register.jsp:
<s:form action="registerAction4">
      <hr />
      <s:actionerror />
      <s :textfield name="email" label="Email" style="width:200px" />
      <s:textfield name="loginName" label="login name" style="width:200px" />
      <s:textfield name ="showName" label="display name" style="width:200px" />
      <s:password name="password" label="password" style="width:200px" />
      <s:password name="confirmPassword " label="Confirm Password" style="width:200px" />


      <s:textfield name="salary" label="月薪" style="width:200px" />
      <s:textfield name="blog" label="博客地址" style="width:200px" />
      <s:textfield name="idcard" label="身份证" style="width:200px" />
      <s:submit value="注册" style="width:80px" />
    </s:form>

第二步:写action部分代码:

package com.hp.lwq.action;

import java.util.Date;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction4 extends ActionSupport {
private static final long serialVersionUID = 1L;

private String email;
private String loginName;
private String showName;
private String password;
private String confirmPassword;
private Date birthday;
private Short age;
private Double salary;
private String blog;
private String idcard;


public String getEmail() {
return this.email;
}

public void setEmail(String email) {
this.email = email;
}

public String getLoginName() {
return this.loginName;
}

public void setLoginName(String loginName) {
this.loginName = loginName;
}

public String getShowName() {
return this.showName;
}

public void setShowName(String showName) {
this.showName = showName;
}

public String getPassword() {
return this.password;
}

public void setPassword(String password) {
this.password = password;
}

public String getConfirmPassword() {
return this.confirmPassword;
}

public void setConfirmPassword(String confirmPassword) {
this.confirmPassword = confirmPassword;
}

public Date getBirthday() {
return this.birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public Short getAge() {
return this.age;
}

public void setAge(Short age) {
this.age = age;
}

public Double getSalary() {
return this.salary;
}

public void setSalary(Double salary) {
this.salary = salary;
}

public String getBlog() {
return this.blog;
}

public void setBlog(String blog) {
this.blog = blog;
}



public String getIdcard() {
return idcard;
}

public void setIdcard(String idcard) {
this.idcard = idcard;
}

@Override
public String execute() throws Exception {
// 其他业务逻辑代码
if(doSomething()) {
ActionContext.getContext().put("tip", "注册成功!");
return SUCCESS;
} else {
ActionContext.getContext().put("tip", "Registration failed!");
return ERROR;
}
}

// Process other business logic code
private Boolean doSomething() {
return true;
}
}


Third Step: Write an xml file whose name corresponds to the action class RegisterAction4-validation.xml, and configure the intercepted fields in the xml file:
<validators>
<!-- Validation of email -->
<field name="email" >
<field-validator type="email">

<message>The format of the entered email is incorrect</message>
</field-validator>
</field>

<!-- Validation of loginName-->
<field name= "loginName">
<field-validator type=" requiredstring">
<param name="trim">true</param>
<message>Login username cannot be empty</message>
</field-validator>

</field>

<!-- Validation for showName-->
<field name="showName">
<field-validator type=" requiredstring">
<param name="trim">true</param>
<message>Display name cannot be empty</message>
</field-validator>
<field-validator type="stringlength">
<param name="maxLength ">15</param>
<param name="minLength">3</param>
<param name="trim">true</param>
<message>The displayed name must be between 3 and 15</message>
</field-validator>
</field>

<! -- 对password的验证 -->
<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>密碼不能為空</message>
</field-validator>

<field-validator type="stringlength">
<param name="maxLength">15</param>
<param name="minLength">6</param>
<param name="trim">true</param>
<message>密码必须在6~15之间</message>
</field-validator>

</field>

<!-- 对confirmPassword的验证 -->
<field name="confirmPassword">
<field-validator type="fieldexpression">
<param name="expression">password==confirmPassword</param>
<message>密码和确认密码必须一致</message>
</field-validator>
</field>


<!-- 对 birthday的验证 -->
<field name="birthday">
<field-validator type="date">
<param name="max">2015-10-30</param>
<message>出生日期必须在2015-10-30之前</message>
</field-validator>
</field>

<!-- 对age校验 -->
<field name="age">
<field-validator type="short">
<param name="min">1</param>
<param name="max">150</param>
<param name="trim">true</param>
<message>年龄必须在1~150之间</message>
</field-validator>
</field>


<!-- 对salary的验证 -->
<field name="salary">
<field-validator type="conversion">
<param name="repopulateField"></param> <!-- Validation for blog--> </field> </field-validator>
<message>Incorrect salary value entered</message>




<field name="blog">
<field-validator type="url">
<message>The entered blog address is incorrect</message>
</field-validator>
</field>
<!-- ID verification -->
<field name="idcard">
<field-validator type="idcard">
<message>The ID number entered is incorrect</message>
</field-validator>
</field>

</validators>
Note : In the last field, another action needs to be written for the verification of the ID card (because this is a custom verification), the specific code is as follows:
package com.hp.lwq.validators;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class IDCardFieldValidator extends FieldValidatorSupport {

@Override
public void validate(Object arg0) throws ValidationException {
//Get the name of the field
String fieldName=getFieldName();
//Get the value of the field according to the name of the field
String fieldValue=(String) getFieldValue(fieldName, arg0);
//If input If the string is empty, return it directly, do not go in to verify
if(fieldName==null || fieldValue.length()==0){
return ;
}

//If the length of the string is not 18, there is an error in the verification
if(fieldValue.length() !=18){
addFieldError(fieldName, arg0);
return ;
}
//The first 17 information of the ID card
String idCardInfo=fieldValue.substring(0, 17);
//Get the 18th information of the ID card, this One bit is the check code of the ID card
String idCardCheckCode=fieldValue.substring(17, 18);
int []factor={7,8,9,10,5,8,4,2,1,6,3,7 ,9,10,5,8,4,2};
//check code
String verifyCode="10X98765432";
int sum=0;
try {
for(int i=0;i<17;i++){
sum+=Integer.parseInt(idCardInfo.substring(i, i+1))*factor[i] ;
}
} catch (Exception e) {
addFieldError(fieldName, arg0);
}
//According to the first 17 digits of identity information and the weight of each bit and the verification code, calculate the verification code of the ID card information
String computeIDCardCheckCode=verifyCode.substring (sum%11, sum%11+1);
//If the obtained check code is different from the check code of the ID card information entered by the user, the check fails
if(!idCardCheckCode.equalsIgnoreCase(computeIDCardCheckCode)){
addFieldError( fieldName, arg0);
}
}

}

Step 4: Because custom validation is used, create a validators.xml file with the default content of struts2 validation, but you need to add your own validation:

<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"/>
    <validator name="idcard" class="com.hp.lwq.validators.IDCardFieldValidator"/>
   
</validators>




第五步:配置struts.xml文件:

<action name="registerAction4" class="com.hp.lwq.action.RegisterAction4">
<result name="input">/register/register.jsp</result>
<result name="success">/register/registerResult.jsp</result>
<result name="error">/register/registerResult.jsp</result>
</action>

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326781362&siteId=291194637