Does the conversation not automatically spread when you submit the form?

I have the following fake support for bean talk:

@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 types of facelets:

 <!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!!! , what is right.

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

INFO: Creating a TestConversation bean !!!
INFO: Call commandLinkAction

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

Quote from docs :

The long-term conversation context associated with the request that the JSF view displays automatically extends to any person request (JSF form view) that is taken from this displayed page.

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

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

+1
source share
2 answers

Based on the previous answer, this is definitely because the TestConversation bean is not created until it is too late for the form to automatically include cid .

In this case, you initialize the data for the view, so it's best to place it in the preRenderView event listener.

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

Put this at an early stage of your layout, for example in f:metadata (as is often used in conjunction with f:viewParam ) and delete the @PostConstruct annotation. This makes the init call explicit, rather than relying on it as a side effect of the built bean, because it was referenced in an EL expression.

+2
source

I think the problem seems that the conversation did not start when the <h:form> component was processed, so there was no cid for the first time in the form action.

The second time you click on the link, testConversation.commandLinkAction , access to testConversation started a conversation before processing <h:form>

try the following change

If you put #{testConversation} before <h:form> example works fine, as the conversion starts before processing <h:form>

Hope this helps.

+3
source

Source: https://habr.com/ru/post/1390213/


All Articles