hello.no.username.error
是对应文件
ApplicationResources_ch.properties
中的一项的。这个文件在strus-config.xml中配置的
<!-- ========== Message Resources Definitions =========================== --> <message-resources parameter="ApplicationResources_ch.properties"/> <message-resources parameter="AlternateApplicationResources"
key="alternate">
</message-resources>
是对应文件
ApplicationResources_ch.properties
中的一项的。这个文件在strus-config.xml中配置的
<!-- ========== Message Resources Definitions =========================== --> <message-resources parameter="ApplicationResources_ch.properties"/> <message-resources parameter="AlternateApplicationResources"
key="alternate">
</message-resources>
解决方案 »
- 有谁看过 JPetStore 的ibatis 6.0版本和 Spring 的实现没
- Tomcat问题
- struts2中使用session,浏览器禁用了cookie,如何实现URL重写
- Javamail 问题 关于 javax.mail.SendFailedException: Invalid Addresses
- 用spring 的validator做数据校验,发生错误,请指点!
- 请教struts的一个问题
- 数据库
- 菜菜菜,一个很菜的问题,EE这里的高手回答一下吧,谢谢
- thinging in java 中的一个StreamTokenizer的问题?
- 高手快来捡分了!--小问题--只有localHome的EJB,客户端程序如何查找到LocalHome???
- eclipse2.1.3,jboss3.2.5(tomcat5.0.26)整合resin
- 如何用cvs访问开源项目
errors.add("username",new ActionMessage("hello.no.username.error"));
resouce文件
hello.baduser.monster=you cannot talk with Monster!!!
hello.no.username.error=please intput user name.
hello.hello=show hello
jsp
<logic:present name="personbean" scope="request">
<bean:write name="personbean" property="userName" />
</logic:present>
<html:form action="/hello" focus="userName">
<html:text property="userName"/>
<br>
<html:submit property="submit" value="submit" />
<html:reset />
</html:form>
</body>
这里的hello.no.username.error只是做为从ApplicationResources_ch.properties文件取值的标志,
只要能对应就行了。
<%@ page language="java"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html locale="true">
<head>
<title>JSP for helloForm form</title>
<html:base/>
</head>
<body>
<html:errors/><p>
<logic:present name="personbean" scope="request">
<bean:write name="personbean" property="userName" />
</logic:present>
<html:form action="/hello" focus="userName">
<html:text property="userName"/>
<br>
<html:submit property="submit" value="submit" />
<html:reset />
</html:form>
</body>
</html:html>
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
# Project P/helloWeb
hello.baduser.monster=you cannot talk with Monster!!!
hello.no.username.error=please intput user name.
hello.hello=show hello
package com.yourcompany.struts.form;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;
/**
* MyEclipse Struts
* Creation date: 10-12-2004
*
* XDoclet definition:
* @struts:form name="helloForm"
*/
public class HelloForm extends ActionForm { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods private String userName = null; public String getUserName() {
return (this.userName);
} public void setUserName(String userName) {
this.userName = userName;
}
/**
* Reset all properties to their default values.
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.userName = null;
} /**
* Validate the properties posted in this request. If validation errors are
* found, return an <code>ActionErrors</code> object containing the errors.
* If no validation errors occur, return <code>null</code> or an empty
* <code>ActionErrors</code> object.
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error")); return errors;
}
}
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error")); return errors;
}
改成 public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionError("hello.no.username.error")); return errors;
}
改了也不行。
errors.add("username",new ActionMessage("hello.no.username.error"));
这里的username是什么地方设置的??我看了别人的成功代码,仿作到这里出问题,
STRUTS架构和ressouce文件使用都没问题,struts1.1
<bean:write name="message"/>
</html:messages>
<html:form action="/hello" focus="userName">
<html:text property="userName"/>
<html:submit property="submit" value="submit" />
<html:reset />和errors.add("userName",new ActionMessage("hello.no.username.error"));,其中username还是userName效果都一样,不行.
老大帮忙分析一下原因.我做了也头晕,不知道其所以然.而且,其他人的成功代码用username也通过的!!
这个相当于用一个名为"username"的键去映射当用户名为空时的出错信息,这样在页面上显示出错信息时我们就可以指定自己要显示的对应字段的出错信息,而且位置比较灵活,如你有多个表单域
html:text property="userName"/><html:errors property="username"/>
html:text property="userId"/><html:errors property="userid"/>
这样如果出错,出错信息显示在相应的字段旁边...
而如果你想显示在 ActionErrors中保存的所有出错信息时,则只需<html:errors/>这一句。