日期:2014-05-17  浏览次数:20735 次

struts2之小问题?
package org.kingtoon.action;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.kingtoon.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterAction extends ActionSupport {



private User user;
@Override
public String execute() throws Exception {
  if(!(user.getPassword().equals(user.getRePassword()))){
  this.addFieldError("password", "请输入相同的密码");
  return "input";
  }
  else
  {
  HttpServletRequest request = ServletActionContext.getRequest ();
  request.setAttribute("user", user);
  return SUCCESS;
  }
 
}
public User getUser() {
  return user;
}
public void setUser(User user) {
  this.user = user;
}


配置验证文件RegisterAction-validation.xml

<validators>
<field name="user.userName">
  <!-- 验证字符串不能为空 -->
  <field-validator type="requiredstring">
  <!-- 去空格 -->
  <param name="trim">true</param>
  <!-- 错误提示信息 -->
  <message>用户名不能为空</message>
  </field-validator>
 
  <!-- 验证字符串长度 -->
  <field-validator type="stringlength">
  <param name="minLength">2</param>
  <param name="maxLength">20</param>
  <message>用户名长度应在2到18个字符间</message>
  </field-validator>
</field>

<field name="user.password">
  <field-validator type="requiredstring">
  <param name="trim">true</param>
  <message>密码不能为空</message>
  </field-validator>
 
  <field-validator type="stringlength">
  <param name="minLength">6</param>
  <param name="maxLength">18</param>
  <message>密码长度应在6到18个字符之间</message>
  </field-validator>
</field> 


从上面的代码,大家知道,我有一个action类:RegisterAction和一个xml文件:RegisterAction-validation.xml,这两个文件放在WEB-INF/classes下的同一个包中,这样注册页面调用“RegisterAction”就会自动调用“RegisterAction-validation.xml”了吗?我很好奇!可是我做了实验却不行呀!这是怎么回事呢?



------解决方案--------------------
至于你一定要知道:什么时候触发struts2去寻找 验证机制的配置文件...
-_-! 至于是不是struts2-corexxx.jar 里的 ConfigurationManager 
自己把包反编译一下 看看吧