我不清楚爲什麼我得到這個特殊的例外,這是我認爲的一個紅鯡魚。根本問題可能與例外的最後一行有關:MessageConverter.getAsObject..0
實際上應該是1501(消息ID)。那麼,爲什麼MessageConverter.getAsObject有錯誤的ID?參數與<f:viewParam>和來自CDI的FacesConverter
我認爲這可能與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());
}
}
這就是問題類,細節,與構造函數,特別是懷疑:
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注入屬性時,同樣的問題會出現,您需要使用@FacesConverter的@Named代替 。對於未來的JSF 2.2版本,Java EE/JSF/CDI傢伙正在爲此工作,另請參閱JSF規範問題763.如果您真的需要將其設置爲@FacesConverter(爲了利用 forClass屬性,例如),那麼您始終可以從JNDI手動抓取EJB。另見下一章。
這對我來說可能是因爲它與EJB有關而略有問題。但是,也許這就是我遇到的確切困難?
[參考](http://stackoverflow.com/questions/10058852/inject-to-pass-params-to-a-cdi-named-bean-via-url-gives-jboss-error- on-netbean/10087664#10087664)爲基礎問題的答案。請刪除此問題。 – Thufir 2012-04-10 11:17:24