2

私は、10桁の番号の携帯電話番号の検証を提供しようとしているStruts 2アプリケーションに取り組んでいます。

10 桁の数字のみを許可していますが、10 桁の数字を入力しても検証エラー メッセージが表示され、日時ピッカー DOJO タグの検証を実行できる購入方法があるかどうかも知りたいです。

Empaction-validation.xml

<!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 ="firstname">
     <field-validator type="requiredstring">
      <message key="requiredstring"/>
    </field-validator>
  </field>


       <field name ="email">
        <field-validator type = "email">
        <message>provide valid Email</message>

        </field-validator>

       </field> 

   <field name ="mobileno">

    <field-validator type="long">
            <param name="min">10</param>
            <param name="max">10</param>

            <message key="stringlength" />
    </field-validator>

</field>


</validators>

登録フォームは

<%@ page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/struts-dojo-tags" prefix="sx" %> 
 <html>
    <head>
       <sx:head/>
    <script type="text/javascript"  src ="script.js"></script>

    </head>  

      <body>
        <div align="center"> <h1 style="color: red">  ENPLOYEE REGISTRATION FORM</h1>
       <s:form  action="emplogin"  method="post" >

      <s:textfield name="firstname" label="Employee Firstname"/>
      <s:textfield name ="lastname" label ="Last name"/>  
      <s:textfield name ="id"  label="Id"/>
      <s:radio name ="gender"   list="{'male', 'female'}" label = "Gender"/>
    <sx:datetimepicker name="dob" displayFormat="dd-MMM-yyyy"  label="DOB"></sx:datetimepicker> 
      <s:radio  name ="maritalstatus" list="{'singale','married'}" label="Marital Status" />

      <s:textfield name ="email" label ="Email"/>
 <sx:datetimepicker name ="joiningdate" displayFormat="dd-MMM-yyyy" label="Joining Date"></sx:datetimepicker>

      <s:textfield name= "designation" label = "Designation"/>
      <s:textarea name ="address" label ="Address" />
      <s:textfield name = "country" label ="Country" />     
      <s:textfield name  ="state" label = "State" />
      <s:textfield name  ="city" label ="City"/> 
      <s:textfield name ="pincode" label ="Pincode"/>
      <s:textfield name ="mobileno" label="Mobile No"/>
      <s:select   name ="groups" list="{'group 1', 'group 2', 'group 3'}"  label ="Group"  cssStyle="{width:184px"/>
     <tr><td>&nbsp;</td></tr>
        <tr>
    <td>&nbsp;</td>
      <s:submit align="center"></s:submit>
      </s:form>
       </div>
    </body>
 </html>

私のBeanクラスでは、longタイプです

package model;


public class Empmodel {
private String firstname;
private String lastname;
private String id;
private String gender;
private String dob;
public String getDob() {
    return dob;
}
public void setDob(String dob) {
    this.dob = dob;
}
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int  pincode;
private long mobileno;
private String groups;
public String getFirstname() {
    return firstname;
}
public void setFirstname(String firstname) {
    this.firstname = firstname;
}
public String getLastname() {
    return lastname;
}
public void setLastname(String lastname) {
    this.lastname = lastname;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}



public String getMaritalstatus() {
    return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
    this.maritalstatus = maritalstatus;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getJoiningdate() {
    return joiningdate;
}
public void setJoiningdate(String joiningdate) {
    this.joiningdate = joiningdate;
}
public String getDesignation() {
    return designation;
}
public void setDesignation(String designation) {
    this.designation = designation;
}
public String getAddress() {
    return address;
}
public void setAddress(String address) {
    this.address = address;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getState() {
    return state;
}
public void setState(String state) {
    this.state = state;
}
public String getCity() {
    return city;
}
public void setCity(String city) {
    this.city = city;
}


public long getMobileno() {
    return mobileno;
}
public void setMobileno(long mobileno) {
    this.mobileno = mobileno;
}
public String getGroups() {
    return groups;
}
public void setGroups(String groups) {
    this.groups = groups;
}
public int getPincode() {
    return pincode;
}
public void setPincode(int pincode) {
    this.pincode = pincode;
}

}
4

1 に答える 1

0

バリデーターを使用するstringlenthと、文字列の長さを検証できます。stringlength バリデータを確認してください。

バリデーターを使用dateすると、日付形式を検証できます。日付バリデータを確認してください。

を使用して日時ピッカーを検証する場合は、 JODAなどの他の日時ライブラリを使用しておらず、タイムゾーンを気にしない場合validate()に、ピッカーから返された値を解析するロジックを使用する必要があります。日時のパーサーを作成する必要があります。SimpleDateFormatThreadLocal

編集:

バリデータ.xml

<?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">

<validators>
  <validator name="longlength" class="jspbean.struts.vaidators.LongLengthFieldValidator"/>
</validators>

LongLengthFieldValidator.java

public class LongLengthFieldValidator extends FieldValidatorSupport {

  private boolean doTrim = true;
  private int maxLength = -1;
  private int minLength = -1;


  public void setMaxLength(int maxLength) {
    this.maxLength = maxLength;
  }

  public int getMaxLength() {
    return maxLength;
  }

  public void setMinLength(int minLength) {
    this.minLength = minLength;
  }

  public int getMinLength() {
    return minLength;
  }

  public void setTrim(boolean trim) {
    doTrim = trim;
  }

  public boolean getTrim() {
    return doTrim;
  }

  public void validate(Object object) throws ValidationException {
    String fieldName = getFieldName();
    String val = getFieldValue(fieldName, object).toString();

    if (val == null || val.length() <= 0) {
      // use a required validator for these
      return;
    }
    if (doTrim) {
      val = val.trim();
      if (val.length() <= 0) {
        // use a required validator
        return;
      }
    }

    if ((minLength > -1) && (val.length() < minLength)) {
      addFieldError(fieldName, object);
    } else if ((maxLength > -1) && (val.length() > maxLength)) {
      addFieldError(fieldName, object);
    }
  }
}

XXX-検証.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>

  <validator type="longlength">
                <param name="fieldName">mobileno</param>
                <param name="minLength">10</param>
               <param name="maxLength">10</param>
               <param name="trim">true</param>
               <message>Your mobileno number needs to be 10 characters long</message>
  </validator>
</validators>
于 2013-02-27T10:49:50.037 に答える