문제

I have the following conversation scoped backing bean:

@Named
@ConversationScoped
public class TestConversation implements Serializable {

    private Logger logger = LoggerFactory.getLogger(TestConversation.class);

    private List<Integer> numbers;

    @Inject
    private Conversation conversation;

    @PostConstruct
    public void init() {
        logger.info("Creating TestConversation bean!!!");

        numbers = new ArrayList<Integer>();
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);
        numbers.add(6);

        conversation.begin();        
    }

    public void commandLinkAction() {
        logger.info("Invoking commandLinkAction");
    }

    public List<Integer> getNumbers() {
        return numbers;
    }
}

And the following facelets view:

<!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:f="http://java.sun.com/jsf/core">

    <h:head>
        <title>Testing Conversation</title>        
    </h:head>

    <h:body>
        <h:form>
            <h:dataTable value="#{testConversation.numbers}" var="num">
                <h:column>                    
                    <h:outputText value="#{num}"/>
                </h:column>
                <h:column>                    
                    <h:commandLink action="#{testConversation.commandLinkAction}">Trigger form submission</h:commandLink>
                </h:column>
            </h:dataTable>
        </h:form>
    </h:body>
</html>

When I enter the page I see INFO: Creating TestConversation bean!!! which is correct.

But then I click on the h:commandLink and I see:

INFO: Creating TestConversation bean!!!
INFO: Invoking commandLinkAction

The bean was created again, which means that the conversation was not propagated. I think this contradicts with the following:

Quote from docs:

The long-running conversation context associated with a request that renders a JSF view is automatically propagated to any faces request (JSF form submission) that originates from that rendered page.

If I add this <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/> then everything works fine. Do I have a misunderstanding?

P.S Without the f:param it works fine when I click on the commandLink for the second time, but not on the first time:(.

도움이 되었습니까?

해결책

Building on the previous answer, it's definitely because the TestConversation bean is not being constructed until it's already too late for the form to include the cid automatically.

In this case, you're initializing data for the view, so it's probably better to put it in a preRenderView event listener instead.

<f:event type="preRenderView" listener="#{testConversation.init}"/>

Put this early in your facelet template, such as in the f:metadata (as it's often used in conjunction with f:viewParam), and remove the @PostConstruct annotation. That makes the invocation of init explicit rather than relying on it being run as a side effect of the bean being constructed because it was referenced in an EL expression.

다른 팁

I guess, the problem seems like conversation was not started when the <h:form> component was processed so the form's action did not had cid in it for the first time..

For the second time, when you click on a link, testConversation.commandLinkAction, access to testConversation made the conversation to start before processing the <h:form>

try the below change

If you place #{testConversation} before <h:form> example works fine as converation is started before processing the <h:form>

Hope this helps..

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top