问题描述
LoginActionpublic class LoginAction extends ActionSupport {private UserService userService;private User user;public User getUser() {return user;}public void setUser(User user) {this.user = user;}...login.jsp<body style="text-align: center;"><s:form action="Login!login" validate="true"><s:textfield name="user.admin_name" label="name" /><s:password name="user.admin_key" label="pwd" /><s:submit value="login" /></s:form></body>LoginAction-validation.xml<validators><field name="user.admin_name"><field-validator type="requiredstring"><message> This string is required </message></field-validator></field><field name="user.admin_key"><field-validator type="requiredstring"><message> This string is required </message></field-validator></field></validators>这样会报错,不知道怎么验证User这个对象 问题补充:<div class="quote_title">renpeng301 写道</div><div class="quote_div"><pre name="code" class="java">你得问题是不是 name="user.admin_name" 这样带对象名的 字段无法验证?</pre></div><br /><br />是的,就是这个问题,不知道该怎么处理
解决方案
<validators> <field name="user"> <field-validator type="visitor"> <message></message> </field> <field name="admin_name"> <field-validator type="requiredstring"> <message> This string is required </message> </field-validator> </field> <field name="admin_key"> <field-validator type="requiredstring"> <message> This string is required </message> </field-validator> </field> </validators> 就是这样 刚才在这里敲 没注意放置位置
解决方案二:
<field name="user"> <field-validator type="visitor"> <message></message> </field> 这个放到 <vaildators> 节点里
解决方案三:
使用 visitor校验器<field name="user"> <field-validator type="visitor"> <message></message></field><validators> <field name="admin_name"> <field-validator type="requiredstring"> <message> This string is required </message> </field-validator> </field> <field name="admin_key"> <field-validator type="requiredstring"> <message> This string is required </message> </field-validator> </field> </validators>
解决方案四:
你得问题是不是 name="user.admin_name" 这样带对象名的 字段无法验证?