0

验证在页面加载时发生。错误消息显示为页面加载。

我的 index.jsp 看起来像

<body>
Employee Information : 
   <s:form action="empinfo" method="post">
      <s:textfield name="name" label="Name" size="20" />
      <s:textfield name="age" label="Age" size="20" />
      <s:select name="gender" headerKey="0" headerValue="Select Gender"
     label="gender" list="#{'1':'Male','2':'Female'}" />

      <s:textfield name="email" label="Email" size="20" />
      <s:textfield name="phone" label="Phone_Number" size="20" />
      <s:submit name="submit" label="Login" align="center" />  
   </s:form>
</body>

动作类(Employee.java)

public String execute() 
   {
       return SUCCESS;
   }

验证文件 Employee-validation.xml:

 <field name="name">
      <field-validator type="requiredstring">
         <message>
            The name is required.
         </message>
      </field-validator>
   </field>

   <field name="age">
      <field-validator type="required">
         <message>
            The age is required.
         </message>
      </field-validator>
   </field>

struts.xml 文件为:

<action name="empinfo" 
         class="com.tutorialspoint.struts2.Employee"
         method="execute">
         <result name="init">/index.jsp</result>   
         <result name="input">/index.jsp</result>
         <result name="success">/Edit.jsp</result>
      </action>

单击提交后,我应该怎么做才能验证表单?struts.xml 文件是否需要为此进行任何配置?

4

1 回答 1

0

在动作类中编写验证代码。您正在使用 struts2,因此您可以像下面那样执行此操作。

<action name="empinfo" class="com.tutorialspoint.struts2.Employee" method="formSubmit">
   <result name="init">/index.jsp</result>   
   <result name="input">/index.jsp</result>
   <result name="success">/Edit.jsp</result>
 </action>

在 Employee.java 中

  public String validateFormSubmit() 
  {
    if(age.equals(null)){
     //display msg feild is required
    }

    if(name.equals(null)){
     //display msg feild is required
    }
  }

public String formSubmit() 
   {
       return SUCCESS;
   }
于 2013-03-14T13:03:13.530 回答