使用xml进行struts验证

时间:2015-03-11 07:19:43

标签: java xml validation struts2

这是我的Action类

public class DoctorsController extends ActionSupport implements ModelDriven<Doctor> {

private DoctorDao docDao = new DoctorDaoImpl();
private List<Doctor> listDoc = new ArrayList<Doctor>();
 private Doctor doc = new Doctor();

public List<Doctor> getListDoc() {
    return listDoc;
}

public void setListDoc(List<Doctor> listDoc) {
    this.listDoc = listDoc;
}


public String excute() throws Exception {
    return SUCCESS;

}

public String getDoctorView() {

    return SUCCESS;
}

public Doctor getDoc() {
    return doc;
}

public void setDoc(Doctor doc) {
    this.doc = doc;
}



public String addDoctor() {

    this.docDao.addDoctor(this.doc);
    listDoc = docDao.listDoctor();
    return SUCCESS;

}

@Override
public Doctor getModel() {
    return doc;
}

public DoctorDao getDocDao() {
    return docDao;
}

public void setDocDao(DoctorDao docDao) {
    this.docDao = docDao;
}

}

这是我在数据库中坚持医生的表格

<html>
<head>
</head>
<body>
    <s:form action="saveDoctor" method="post">
        <table>  
            <s:push value="doc">
                <s:hidden name="id"  />

                <s:textfield name="firstName" label="First Name" />

                <s:textfield name="lastName" label="Last Name" />


                <s:textfield name="email" label="Email" />

                <s:textfield name="fees" label="Fees" />

        <s:select name="specialist" list="#@java.util.LinkedHashMap@{'Bone':'Bone','Heart':'Heart'}" />

                <s:textfield name="chamber.city" label="City" />

                 <s:textfield name="chamber.zipCode" label="ZipCode" />
                 <s:textfield name="chamber.roadNum" label="Road Num" />
                 <s:textfield name="chamber.houseNum" label="House Num" />




           <s:submit/>

            </table>     

            </s:push>
    </s:form>
  </body>
</html>

这是我的DoctorsController-validation.xml,用于验证医生的电子邮件

<validators>
 <field name="doc.email">
    <field-validator type="requiredstring">

        <message>Email can't be blank</message>

    </field-validator>

 </field>
</validators>

现在验证工作正常,因为如果&#34;电子邮件&#34;字段为空操作被重定向到输入表单但问题是我无法在输入表单中看到任何验证消息。我应该看到一条消息,例如&#34;电子邮件不能为空白&#34;但是我没有看到它。

1 个答案:

答案 0 :(得分:0)

试试这个

   <form name="doc">
       <field property="email" depends="required,email">
            <arg key="doc.email"/>
        </field>
        <field property="firstName" depends="required">
            <arg key="doc.firstName"/>
        </field>
    </form>

此代码适用于我。