我是 struts 2 的新手。我只是尝试使用 validation.xml 文件验证表单,它工作正常。
但错误消息显示在文本字段的顶部。需要在测试字段的右侧用红色字体显示这些错误消息。
我的jsp代码(index.jsp)
<s:form action="addEmp" 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:textfield name="address" label="Address" size="20" />
<s:submit name="submit" label="Login" align="center" />
</s:form>
我的动作类(Employee.java):
package com.tutorialspoint.struts2;
import com.opensymphony.xwork2.ActionSupport;
public class Employee extends ActionSupport{
public String addEmployee()
{
return SUCCESS;
}
}
struts.xml 文件:
<action name="addEmp"
class="com.tutorialspoint.struts2.Employee"
method="addEmployee" >
<result name="input">/index.jsp</result>
<result name="success">/success.jsp</result>
</action>
和 Employee-validation.xml 文件:
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>
The name is required.
</message>
</field-validator>
</field>
<field name="gender">
<field-validator type="int">
<param name="min">1</param>
<message>
Please select a value
</message>
</field-validator>
</field>
<field name="email">
<field-validator type="requiredstring">
<message>
The email is required.
</message>
</field-validator>
<field-validator type="email">
<message>
Please enter a valid email ID
</message>
</field-validator>
</field>
请帮我这样做..提前谢谢。