如何在sturts2的迭代器标签内应用验证

时间:2018-10-18 19:45:26

标签: java struts2

我正在尝试在迭代器标签内的struts2中应用表单验证。 在下面的jsp页面中,当将帐号添加到搜索文本字段中时,iterator标记将加载该帐号的当前数据,在该数据中可以编辑详细信息。单击提交后的编辑后,我希望将验证应用于iterator标记内的文本字段,以便不会输入错误的键入数据。

*注意:无论哪种方式,ajax都不只是Struts2验证。

Jsp页面(edituserdetails.jsp):

                <s:form action="editUserDetails">
                    <h1><s:text name="Edit User Details"/></h1>
                    <h2><s:property value="successmsz"/></h2>
                    <s:textfield key="accountno_check" label="Enter Account No."/>
                    <s:submit key="submitreq" value="Search" cssClass="button_bt"/>

                    <s:iterator value="ubb">

                        <s:textfield key="accountno" label="Account No." readonly="true"/>
                        <s:textfield key="aadhar"  label="Enter Aadhar number"/>
                        <s:textfield key="pan" label="Enter PAN number"/>
                        <s:textfield key="name" label="Enter name"/>
                        <s:textfield key="age" label="Enter age" />
                        <s:radio key="gender" label="Gender" list="#{'Male':'Male'}"/>
                        <s:radio key="gender" label="Gender" list="#{'Female':'Female'}"/>
                        <s:radio key="gender" label="Gender" list="#{'Other':'Other'}"/>
                        <s:textfield key="mobile" label="Enter Mobile No."/>
                        <s:textfield key="landline" label="Enter Landline No."/>
                        <s:textfield key="father" label="Enter Father name"/>
                        <s:textfield key="address" label="Enter current Address"/>
                        <s:textfield key="p_address" label="Enter permanent Address"/>
                        <s:textfield key="country" label="Enter Country"/>

                        <s:select label="Select Account Type" headerKey="-1" headerValue="Account Type" 
                                  list="#{'Saving':'Saving','Current':'Current'}" key="account_type"/>
                        <br/>
                        <div style="width: 450px;">
                            <s:submit value="Submit" key="submitreq" cssClass="button_bt"/>
                        </div>

                    </s:iterator>
                </s:form>

基于操作支持的操作(EditUserDetails.java):

    public String execute() throws Exception {
        HibernateOperation op = new HibernateOperation();
        UserBean ub = new UserBean();
        if (submitreq.equalsIgnoreCase("submit")) {

            ub.setAccountno(accountno);
            ub.setAadhar(aadhar);
            ub.setPan(pan);
            ub.setName(name);
            ub.setAge(age);
            ub.setGender(gender);
            ub.setMobile(mobile);
            ub.setLandline(landline);
            ub.setFather(father);
            ub.setAddress(address);
            ub.setP_address(p_address);
            ub.setCountry(country);
            ub.setAccount_type(account_type);



            boolean b = op.updateUserDetails(ub);  // Updating details
            if (b) {
                ubb = op.getUserBalance(accountno);  //Getting user details for further output
                return LOGIN;
            } else {

                return ERROR;
            }
        }else
        {
            System.out.println("Into Searching of account number");
            ubb = op.getUserBalance(accountno_check);  //Searching account number details

            if (ubb.size() > 0) {
                return SUCCESS;
            } else {

                return NONE;
            }
        }
    }

    public void validate()
    {
        if(StringUtils.isEmpty(aadhar))
                addFieldError("aadhar", "Aadhar number cannot be blank");
    }

}

Struts2配置文件:

<action name="editUserDetails" class="example.EditUserDetails">
            <result name="success">edituserinfo.jsp</result>
            <result name="none">edituserinfo.jsp</result>
            <result name="error">employeecontrolpanel.jsp</result>
            <result name="login">userbalancesheet.jsp</result>
            <result name="input">edituserinfo.jsp</result>
</action>

0 个答案:

没有答案