带有<f:viewparam>的参数和来自CDI的FacesConverter </f:viewparam>

时间:2012-04-08 08:17:59

标签: jsf glassfish facelets el cdi

我不清楚为什么我会得到这个特殊的例外,我认为这是一个红鲱鱼。底层问题可能与异常的最后一行有关: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());
    }
}

这是问题类,详细信息,特别是构造函数,可疑:

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,@ Name,所以不能使用@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相关。但是,也许这就是我遇到的确切困难?

1 个答案:

答案 0 :(得分:4)

转换器应该在<f:viewParam>中使用。

<f:viewParam name="id" value="#{detail.message}" converter="messageConverter" />

这基本上会将请求参数id转换为对象Message,然后将其设置在#{detail.message}后面的属性中。你应该只在Detail类中为它提供一个setter,我在你的bean中看不到任何一个。

您不应该在Detail类的构造函数中执行转换作业。您应该只有Message属性,而不是idnntp。删除整个构造函数。

@Named
public class Detail {

    private Message message; 

    // Getters+setters+actions
}