viewParam值未在ViewScoped bean中设置 [英] viewParam value not set in ViewScoped bean

查看:67
本文介绍了viewParam值未在ViewScoped bean中设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,可能是另一个非常基本的问题。在我的ViewScoped bean中,'viewParam'看起来像是在设置,但是当我使用它时,值为null。我在setter中设置了一个断点(setEventId()),它获取了值,但是在我的preRenderView指定的方法中,它已经消失了,所以我无法加载我想要检索的Event对象。

Sorry, probably another really basic question. In my ViewScoped bean, a 'viewParam' looks like it's getting set, but when I come to use it, the value is null. I put a breakpoint in the setter (setEventId()) and it gets the value, but in the method specified by my preRenderView, it's gone, so I can't load the Event object I am trying to retrieve.

当我的bean是RequestScoped时,这工作正常,但我发现在POST和后续验证错误时,我的所有细节都丢失了,并且读到ViewScoped是通往解决这个问题。

This was working fine when my bean was RequestScoped, but I found that on a POST and subsequent validation error, all my details were lost and read that ViewScoped was the way to get around this problem.

我已经升级到Mojarra 2.1.7因为我认为它可能是一个错误,并且确实在他们的JIRA中列出了一个关键错误,在2.1中修复。 7,但我在Glassfish日志中验证它使用的是新版本,我仍然遇到同样的问题: http://java.net/jira/browse/JAVASERVERFACES-2266

I have upgraded to Mojarra 2.1.7 because I thought it might be a bug, and indeed there is a 'critical bug' listed in their JIRA, fixed in 2.1.7, but I verified in the Glassfish logs that it was using the newer version, and I still get the same problem: http://java.net/jira/browse/JAVASERVERFACES-2266

请帮忙,这是我的豆子(我试过有没有'ManagedProperty'注释)

Please help, here's my bean (I have tried with and without the 'ManagedProperty' annotation)

@ViewScoped
@Named
public class EventController extends AbstractController {

    private static final Logger logger = Logger.getLogger("EventController");

    /**
     * Request param managed property
     */
    @ManagedProperty(value="#{param.eventId}")
    private Long eventId;                

    private Event event = new Event();

    /**
     * The event dao
     */
    @Inject
    private EventDao eventDao;    

    /**
     * Load the event (requires eventId has a value)
     * @return 
     */
    public void loadEvent() {
        event = eventDao.find(eventId);
    }    

    /**
     * @return the eventId
     */
    public Long getEventId() {
        return eventId;
    }

    /**
     * @param eventId the eventId to set
     */
    public void setEventId(Long eventId) {
        this.eventId = eventId;
    }   
}

以下是我在''中构建链接的方法listEvents'page

Here's how I'm constructing the link in the 'listEvents' page

<h:link value="Full details" outcome="/calendar/viewEvent" includeViewParams="true">
<f:param name="eventId" value="#{calendarController.event.eventId}" />
</h:link>

这里是需要eventId属性的页面

And here's the page that needs the eventId property

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:c="http://java.sun.com/jsp/jstl/core">

    <body>

        <ui:composition template="/WEB-INF/templates/standardTemplate.xhtml">

    <f:metadata>
        <f:viewParam name="eventId" value="#{eventController.eventId}"/>
        <f:event type="preRenderView" listener="#{eventController.loadEvent}" />
    </f:metadata>            

            <ui:define name="content">      

                <h1>Event details for: #{eventController.event.title}</h1>
                <h:form>
                    <p:messages/>                                               

                    <p:panelGrid style="margin-top:20px">  
                        <f:facet name="header">  
                            <p:row>  
                                <p:column colspan="4">Event details</p:column>  
                            </p:row>  
                        </f:facet>  

                        <p:row>  
                            <p:column>
                                Title
                            </p:column>  
                            <p:column colspan="3">
                                <p:inputText value="#{eventController.event.title}" size="49"/>
                                <h:inputHidden id="eventId" value="#{eventController.event.eventId}"/>
                            </p:column>   
                        </p:row>                                                                                                   
                </h:form>                                
            </ui:define>
        </ui:composition>            
    </body>
</html>


推荐答案

您正在通过CDI管理bean,而不是JSF。 JSF @ViewScoped 注释仅适用于JSF @ManagedBean ,而不适用于CDI @Named 。在CDI @Named 上,您只能使用CDI范围,而不能使用JSF范围。与CDI最接近的是 @ConversationScoped 。但是你要用一些额外的样板代码来管理对话的开始和结束。

You're managing the bean by CDI, not by JSF. The JSF @ViewScoped annotation works on JSF @ManagedBean only, not on CDI @Named. On CDI @Named you can only use the CDI scopes, not the JSF scopes. The closest what CDI offers is the @ConversationScoped. But you've to manage the start and end of the conversation yourself with some additional boilerplate code.

同样的故事适用于JSF @ManagedProperty 注释。它仅适用于JSF @ManagedBean ,而不适用于CDI @Named 。对于CDI,您应该使用 @Inject 自定义HTTP param注释

The same story applies to JSF @ManagedProperty annotation. It works in JSF @ManagedBean only, not on CDI @Named. For CDI you should use @Inject or a custom HTTP param annotation.

JSF问题2266 无关到这一切。

JSF issue 2266 is unrelated to this all.

这篇关于viewParam值未在ViewScoped bean中设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆