validate()函数好像没有错误。
请确认你的struts-config.xml的配置文件里,配置好了与使用哪个资源文件。
请确认你的struts-config.xml的配置文件里,配置好了与使用哪个资源文件。
解决方案 »
- Spring 入门时遇到的问题
- 初次使用无状态sessionbean的问题
- javamail smtp连接缓冲池
- 再strut+hibernate框架下servlet如何调用hibernate的Session
- 求教在jboss中客户端如果是单个类的main函数,如何配置使之正确运行?
- Tomcat用web.xml配置Servlet的目录路径问题?
- 怎样在jsp页面中区分显示器的分辨率是多少呢?????在线等你。。。。。!!!
- 有没有人用VJ++编程,请来指点一二
- oracle.jdbc.OracleResultSet问题:在线等待,请指教!谢谢
- 求助!hql的别名问题
- 如何把数据库的数据读到 XML中,显示相应的xml格式
- 关于struts+hibernate+spring项目的团队合作的问题?
资源文件也没有问题.
errors.add("username",error);
用new ActionMessage ,不用new ActionError
import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;/**
* MyEclipse Struts
* Creation date: 03-29-2006
*
* XDoclet definition:
* @struts.form name="helloForm"
*/
public final class HelloForm extends ActionForm { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
*
*/
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Method validate
* @param mapping
* @param request
* @return ActionErrors
*/
private String userName=null;
public String getUserName(){
return this.userName;
}
public void setUserName(String username){
this.userName=username;
}
public void reset(ActionMapping mapping, HttpServletRequest request) { // TODO Auto-generated method stub
this.userName=null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) { // TODO Auto-generated method stub
ActionError error=new ActionError("hello.dont.take.to.monster");
ActionErrors errors=new ActionErrors();
// ActionMessage am=new ActionMessage("hello.no.username.error");
if ((userName==null)||(userName.length()<-1)){
errors.add("username",error);
// errors.add("username",am);
//return errors;
}
return errors;
} /**
* Method reset
* @param mapping
* @param request
*/
}我的jsp页面:<%@ page language="java"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<html:html locale="true">
<head>
<title><bean:message key="hello.jsp.title"/></title>
<html:base/>
</head>
<body>
hello ,everyone:test1
<H2><bean:message key="hello.jsp.page.heading"/></H2><p>
<html:errors property="username"/><p>
<logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName"/><p>
</h2>
</logic:present>
<html:form action="/hello.do" scope="userName">
<bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/> <br>
<html:submit/><html:reset/>
</html:form>
</body>
</html:html>我的truts-config.xml文件:<form-beans >
<form-bean name="helloForm" type="com.yourcompany.struts.form.HelloForm" /> </form-beans> <global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="helloForm"
input="/form/hello.jsp"
name="helloForm"
path="/hello"
scope="request"
type="com.yourcompany.struts.action.HelloAction" >
<forward name="SayHello" path="/form/hello.jsp"/>
</action>
</action-mappings> <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>我的HelloAction.java:package com.yourcompany.struts.action;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionError;
//import org.apache.struts.action.ActionMessage;
import com.yourcompany.struts.form.HelloForm;/**
* MyEclipse Struts
* Creation date: 03-29-2006
*
* XDoclet definition:
* @struts.action path="/hello" name="helloForm" input="/form/hello.jsp" scope="request" validate="true"
*/
public class HelloAction extends Action { // --------------------------------------------------------- Instance Variables // --------------------------------------------------------- Methods /**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
HelloForm helloForm = (HelloForm) form;
String username=helloForm.getUserName();
String badUserName="monster";
ActionError error=new ActionError("hello.dont.take.to.monster");
ActionErrors errors=new ActionErrors();
if((username.equalsIgnoreCase(badUserName))){
// errors.add("username",new ActionMessage("hello.dont.take.to.monster"));
errors.add("username",error);
this.saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
//return (mapping.findForward("SayHello"));
} // TODO Auto-generated method stub
PersonBean pb=new PersonBean();
pb.setUserName(username);
pb.SaveToPersistentStore();
request.setAttribute(Constants.PERSON_KEY, pb);
return (mapping.findForward("SayHello"));
}}
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;
}
改成这样看看呢!