页面加载时进行验证。错误消息显示为页面加载。
我的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>
Action class(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文件需要配置吗?
答案 0 :(得分: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;
}