本文介绍了selectOneRadio组件和Long值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我应该创建一个调查应用程序,并且答案的值是一个Long,值从1到6.我尝试了许多不同的方法,但结果是每次:无法将java.lang.String强制转换为java.lang.Long

Well, I should create an survey application amd the value of the answers is a Long with value from 1 to 6.I tried many different things but the result is every time:java.lang.String cannot be cast to java.lang.Long

目前,这是我的代码:

java类:

...
private HashMap<Questions,Long> itemsHash= new HashMap<Questions,Long>();
private static Map<Long,Long> valutazioni;
static{
    valutazioni = new LinkedHashMap<Long,Long>();
    valutazioni.put(new Long("1"), new Long("1"));
    valutazioni.put(new Long("2"), new Long("2"));
    valutazioni.put(new Long("3"), new Long("3"));
    valutazioni.put(new Long("4"), new Long("4"));
    valutazioni.put(new Long("5"), new Long("5"));
    valutazioni.put(new Long("6"), new Long("6"));
}
public Map getValutazioni()
{
    return valutazioni;
}
...

public String prepareCreate() {
    itemsHash=new HashMap<Questions,Long>();
    //-- retrieve all questions from db
    List<Questions> qList= getQEjb().findByQuery("select q from Question q order by q.description");
    Iterator<Questions> iter=qList.iterator();
    Questions q;
    //--- initiliazing itemsHash with 1 as default value
    while (iter.hasNext())
    {
        q=iter.next();
        itemsHash.put(q,(new Long("1")));
    }

    selectedItemIndex = -1;
    return "Create";
}

public String create() {
    try {
        List<Questions> qList= getQEjb().findByQuery("select q from Questions q order by q.description");

        Iterator<Questions> iter=qList.iterator();
        Questions q;
        while (iter.hasNext())
        {
            q=iter.next();
            current=new Answers();
            current.setAllievo(findAllievo);
            current.setData(findData);
            current.setIdQuestion(q);
            current.setLivello(itemsHash.get(q));

            getFacade().create(current);
        }
        JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ProgettiQualificaCreated"));
        return pqc.prepareList();
    } catch (Exception e) {
        JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        e.printStackTrace();
        return "List";
    }
}

xhtml页面是:

<h:outputLabel value="#{bundle.AnswersLabel_data}" for="data" />
<h:inputText id="data" value="#{answersController.findData}" title="#{bundle.AnswersTitle_data}" >
    <f:convertDateTime pattern="dd/MM/yyyy HH:mm:ss" />
</h:inputText>
<h:outputLabel value="#{bundle.AnswersLabel_allievo}" for="allievo" />
<h:inputText id="allievo" value="#{answersController.findAllievo}" title="#{bundle.AnswersTitle_allievo}" />
<h:outputLabel value="#{bundle.AnswersLabel_numScheda}" for="numScheda" />
<h:inputText id="numScheda" value="#{answersController.findNumScheda}" title="#{bundle.AnswersTitle_numScheda}" required="true" requiredMessage="#{bundle.AnswersRequiredMessage_numScheda}"/>
<h:outputText value="#{bundle.AnswersLabel_idProgetto}"/>
<h:outputText value="#{answersController.findProgetto}" title="#{bundle.AnswersTitle_idProgetto}"/>
<h:dataTable value="#{questionsController.items}" var="car" rules="all" >
    <h:column>
        <f:facet name="header">
            <h:outputText value="#{bundle.AnswersTitle_idQuestion}"/>
        </f:facet>
        <h:outputText value="#{car.descrizione}"/>
    </h:column>
    <h:column>
        <f:facet name="header">
            <h:outputText value="#{bundle.AnswersTitle_livello}"/>
        </f:facet>
<h:selectOneRadio value="#{answersController.itemsHash[car]}" title="#{bundle.AnswersTitle_idQuestion}" >
 <f:selectItems value="#{answersController.getValutazioni()}" />

</h:selectOneRadio>

    </h:column>
</h:dataTable>

我的错误在哪里?

错误日志:

[#|2012-03-20T15:52:17.656+0100|SEVERE|glassfish3.1.1|javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=97;_ThreadName=Thread-2;|java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
    at it.cyborg.cdg.jsfClasses.AnswersController.create(AnswersController.java:187)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:43)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:56)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    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:1539)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at it.cyborg.cdg.jsfClasses.util.MultipartFilter.doFilter(MultipartFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    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 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    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)

我的应用程序在Glassfish 3.1上运行,并支持JSF2.0和PrimeFaces 3.2 RC1

My application runs on Glassfish 3.1 and support JSF2.0 and PrimeFaces 3.2 RC1

推荐答案

问题是您将通用Map绑定为<h:selectOneRadio>的值,如下所示

The problem is that you've bound a generic Map as value of <h:selectOneRadio> as follows

<h:selectOneRadio value="#{answersController.itemsHash[car]}">

但是JSF/EL不支持泛型类型.即,该信息在运行期间不可用.除非您明确指定转换器,否则EL不会在将其提交为Map值之前转换/强制提交的String值.

But JSF/EL doesn't support generic types. This information is namely not available during runtime. Unless you explicitly specify a converter, EL will not convert/coerce the submitted String value before setting it as Map value.

您需要在此处明确指定默认的JSF Long转换器javax.faces.Long:

You need to explicitly specify the default JSF Long converter javax.faces.Long here:

<h:selectOneRadio value="#{answersController.itemsHash[car]}" converter="javax.faces.Long">

这篇关于selectOneRadio组件和Long值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:51