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

Structs错误显示问题
我的页面有一个username控件.我想在validate验证失败后把错误打在username控件的右面.现在能打印出来.但是我资原文件中写的是{0} can not be empty. 我想要的结果是username can not be empty 可是显示的是{0} can not be empty 请问如何把{0}用username替换啊.昨天还会的.过一天就忘了.高手帮个忙.


ActionForm:
Java code
package c1;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class LoginActionForm extends ActionForm {

  private String username;
  
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }


  @Override
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    if ("".equals(this.getUsername()) || this.username == null) {
      ActionErrors ae = new ActionErrors();
      ActionMessage am = new ActionMessage("error.eng");
      ae.add("username", am);
      return ae;
    } else {
      return super.validate(mapping, request);
    }
  }
}



Structs_config.xml :
XML code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config SYSTEM "struts-config_1_1.dtd">
<struts-config>
  <form-beans>
    <form-bean name="login" type="c1.LoginActionForm"></form-bean>
  </form-beans>
  <action-mappings>
    <action path="/c1/main" name="login" type="c1.LoginAction" input="/c1/test1.jsp" validate="true">
      
    </action>
  </action-mappings>
  <message-resources parameter="resource"></message-resources>
</struts-config>



resource.properties:
error.eng = {0} can not empty


------解决方案--------------------
ActionMessage am = new ActionMessage("error.eng");
 应该写为:
 ActionMessage am = new ActionMessage("error.eng", username);

 要把占位符对应的username变量加进去才行。