日期:2014-05-20  浏览次数:20824 次

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>


JSP:
HTML code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib  prefix="structs-html" uri="/WEB-INF/struts-html.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
  <body>
    <form action="main.do">
      username<input type="text"><structs-html:errors property="username"/><br>
      <input type="submit" value="提交">
    </form>
  </body>
</html>


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



------解决方案--------------------
来看看的