返回列表 发帖

[JSF和MyFaces] jsf :throw new RuntimeException

你好:我在看您的经典javaEE企业应用实战一书中,在第2章2.9节facesException 实例中遇到throw new RuntimeException问题(初学jsf),配置tomcat7.0,jdk 1.7,eclipseJ2ee.问题如下:public void setAge(int age) {



if(age > 100 || age < 0)

{


    throw new RuntimeException("年龄必须小于100,且大于0!");

}

this.age = age;

}
在tomcat7.0下运行:出现如下异常:(请帮忙看看,是哪错了?)
严重: Servlet.service() for servlet [Faces Servlet] in context with path [/MyJsf3] threw exception [org.apache.jasper.el.JspELException: /regist.jsp(76,3) '#{userBean.age}' Error writing 'age' on type org.UserBean] with root cause
java.lang.RuntimeException: 年龄必须小于100,且大于0!

at org.UserBean.setAge(UserBean.java:32)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at javax.el.BeanELResolver.setValue(BeanELResolver.java:138)

at com.sun.faces.el.DemuxCompositeELResolver._setValue(Unknown Source)

at com.sun.faces.el.DemuxCompositeELResolver.setValue(Unknown Source)

at org.apache.el.parser.AstValue.setValue(AstValue.java:218)

at org.apache.el.ValueExpressionImpl.setValue(ValueExpressionImpl.java:252)

at org.apache.jasper.el.JspValueExpression.setValue(JspValueExpression.java:89)

at javax.faces.component.UIInput.updateModel(Unknown Source)

at javax.faces.component.UIInput.processUpdates(Unknown Source)

at javax.faces.component.UIForm.processUpdates(Unknown Source)

at javax.faces.component.UIComponentBase.processUpdates(Unknown Source)

at javax.faces.component.UIViewRoot.processUpdates(Unknown Source)

at com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(Unknown Source)

at com.sun.faces.lifecycle.Phase.doPhase(Unknown Source)

at com.sun.faces.lifecycle.LifecycleImpl.execute(Unknown Source)

at javax.faces.webapp.FacesServlet.service(Unknown Source)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)

at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)

at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Unknown Source)

这个异常是我们主动抛出来的啊,
这个不算错误啊,相当于我这个应用要求人的年龄必须在0~100之间,但用户输入了一个非法年龄,我的程序故意引发一个异常来提醒用户啊。
生命没有假期!
如果生命只剩下最后一秒,你可会想到我?

TOP

谢谢您,这个我知道的,不过,按照书上的描述,应该是手工抛出异常,客户端的页面不应该是异常页面,而是正常的登录页面,输入非法年龄的信息应该在标签 <h:message for="age"/> 的右面输出才对呀。可是客户端输出的确是异常页面,不知道是为什么呀???

TOP

返回列表