struts标签错误,大家帮忙看看?
初学struts,做了个小例子,就出现了下面的错误,调了两天也没弄明白时怎么回事?
我的部分程序:(我估计可能出错的部分)
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request)
{
ActionErrors errors=new ActionErrors();
if(userName==null||userName.length()<1)
{
errors.add("userName",new ActionMessage("please check a name"));
}
return errors;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
UserLoginForm userLoginForm = (UserLoginForm) form;
ActionErrors errors=new ActionErrors();
MessageResources messages=getResources(request);
if(userLoginForm.getUserName().equalsIgnoreCase("eric")&&userLoginForm.getPassword().equals("1234"))
{
errors.add("userName",new ActionMessage("jsp.hello.eric"));
saveErrors(request,errors);
return mapping.findForward("success");
}
return mapping.findForward("failure");
}
jsp文件:
<html:form action="/userLogin">
password : <html:password property="password"/><html:errors property="password"/><br/>
userName : <html:text property="userName"/><html:errors property="userName"/><br/>
<html:submit/><html:cancel/>
</html:form>
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.lang.ClassCastException
org.apache.struts.taglib.html.ErrorsTag.doStartTag(ErrorsTag.java:215)
org.apache.jsp.form.userLogin_jsp._jspx_meth_html_errors_1(userLogin_jsp.java:200)
org.apache.jsp.form.userLogin_jsp._jspx_meth_html_form_0(userLogin_jsp.java:122)
org.apache.jsp.form.userLogin_jsp._jspService(userLogin_jsp.java:83)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)
org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)
org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
问题点数:20、回复次数:5Top
1 楼lip009(深蓝忧郁)回复于 2005-12-03 21:15:19 得分 9
类匹配异常,可能是这的错errors.add("userName",new ActionMessage("jsp.hello.eric"));
试一下改成这个行不行
errors.add("userName",new ActionError("jsp.hello.eric"));Top
2 楼down0011(down0011)回复于 2005-12-04 13:00:58 得分 11
或者定义ActionMessages errors = new ActionMessages;
errors.add("userName",new ActionMessage("jsp.hello.eric"));
Top
3 楼redline2005(红线)回复于 2005-12-04 18:41:25 得分 0
感谢两位高手,我把程序这样改了
ActionErrors errors=new ActionErrors();
errors.add("userName",new ActionError("jsp.hello.eric")); 调试成功,谢谢两位。
不过我还有些疑问,请教你们:
1.ActionErrors 继承 ActionMessages
而ActionMessage 与ActionMessages 是聚合的关系
所以ActionMessage与ActionErrors也应该是聚合的关系
按此推理
ActionErrors errors=new ActionErrors();
errors.add("userName",new ActionMessage("jsp.hello.eric"));
这句应该没错,况且 孙卫琴的那本书上 也是这样用的,她那个例子可以运行,
而我的就为什么调试不行呢?
2.
如果我按照 down0011(down0011) 的修改:
ActionMessages errors=new ActionMessages();
errors.add("userName",new ActionMessage("jsp.hello.eric"));
saveErrors(request,errors); // 这句会出错 因为它的第二个参数必须是ActionErrors类型的,danTop
4 楼redline2005(红线)回复于 2005-12-04 18:42:40 得分 0
但是即使这样改了saveErrors(request,(ActionErrors)errors);
调试也不能通过。Top
5 楼down0011(down0011)回复于 2005-12-05 14:52:20 得分 0
saveErrors(javax.servlet.http.HttpServletRequest request, ActionMessages errors)
应该是可以的
saveErrors(javax.servlet.http.HttpServletRequest request, ActionErrors errors)
已经不推荐使用了。
http://struts.apache.org/struts-doc-1.2.7/api/org/apache/struts/action/Action.html
Top




