我不清楚为什么我会得到这个特殊的例外,我认为这是一个红鲱鱼。潜在问题可能与异常的最后一行有关: MessageConverter.getAsObject..0
实际上应该是 1501(消息 ID)。那么,为什么 MessageConverter.getAsObject 的 id 错误呢?
我认为这可能与 Detail 构造函数有关,它可能将错误的参数传递给 MessageConvert.getAsObject(),特别是 UIComponent 对象——可能该引用需要实例化。
相关EL:
<h:outputLink id="link1" value="detail.xhtml">
<f:param name="id" value="#{m.getMessageNumber()}" />
<h:outputText value="#{m.getMessageNumber()}" />
</h:outputLink>
glassfish 显示:
INFO: MessageConverter.getAsObject..1501
INFO: SingletonNNTP.forward..11
WARNING: 1501
java.lang.ArrayIndexOutOfBoundsException: 1501
at java.util.Arrays$ArrayList.get(Arrays.java:2866)
at net.bounceme.dur.nntp.SingletonNNTP.getMessage(SingletonNNTP.java:86)
at net.bounceme.dur.nntp.MessageConverter.getAsObject(MessageConverter.java:21)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:171)
at javax.faces.component.UIViewParameter.getConvertedValue(UIViewParameter.java:394)
at javax.faces.component.UIInput.validate(UIInput.java:960)
at javax.faces.component.UIInput.executeValidate(UIInput.java:1233)
at javax.faces.component.UIInput.processValidators(UIInput.java:698)
at javax.faces.component.UIViewParameter.processValidators(UIViewParameter.java:273)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1214)
at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1172)
at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1542)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:849)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:746)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1045)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:228)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
INFO: MessageConverter.getAsObject..0
INFO: SingletonNNTP.forward..11
MessageConverter,我认为这个类到目前为止还不错:
package net.bounceme.dur.nntp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.mail.Message;
@FacesConverter("messageConverter")
public class MessageConverter implements Converter {
private static final Logger logger = Logger.getLogger(MessageConverter.class.getName());
private static final Level level = Level.INFO;
private SingletonNNTP nntp = SingletonNNTP.INSTANCE;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
logger.log(level, "MessageConverter.getAsObject..{0}", value);
Message message = nntp.getMessage(Integer.parseInt(value));
return message;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Message message = (Message) value;
return String.valueOf(message.getMessageNumber());
}
}
这是问题类 Detail,构造函数尤其值得怀疑:
package net.bounceme.dur.nntp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.mail.Message;
@Named
public class Detail {
private static final Logger logger = Logger.getLogger(Detail.class.getName());
private static final Level level = Level.INFO;
private int id = 0;
private Message message = null;
private SingletonNNTP nntp = SingletonNNTP.INSTANCE;
public Detail() {
MessageConverter mc = new MessageConverter();
FacesContext fc = FacesContext.getCurrentInstance();
String value = String.valueOf(id);
UIComponent ui = null;
Object obj = mc.getAsObject(fc, ui, value);
message = (Message) obj;
}
public int forward() {
logger.log(level, "Detail.forward..{0}", id);
id = id + 1;
logger.log(level, "..Detail.forward {0}", id);
return id;
}
public int back() {
logger.log(level, "Detail.back..{0}", id);
id = id - 1;
logger.log(level, "..Detail.back {0}", id);
return id;
}
public Message getMessage() {
return message;
}
public String getContent() throws Exception {
return message.getContent().toString();
}
}
参考:
viewparam-vs-managedpropertyvalue-param-id似乎不太适用,因为我正在使用 CDI、@Named,因此无法使用 @ManagedProperty,这是“真正的”问题。我想我没有看到 CDI 的好处,因为 @ManagedProperty 看起来非常好。
ProcessingGETRequestParameters可能有一个关键段落:
是的,它是 @ManagedBean 而不是 @FacesConverter!多么尴尬,但不可能在@FacesConverter 中注入@EJB 来完成数据库交互工作。当您使用 CDI @Inject 注入属性时,同样的问题也会出现,您需要使用 @Named 而不是 @FacesConverter。Java EE/JSF/CDI 人员正在为未来的 JSF 2.2 版本开发此功能,另请参阅 JSF 规范问题 763。如果您确实需要将其设置为 @FacesConverter(例如,为了利用 forClass 属性) ,那么您总是可以从 JNDI 手动获取 EJB。另见下一章。
就我的目的而言,这可能与 EJB 相关,因此可能有点偏离主题。但是,也许这就是我遇到的确切困难?