0

For what reason won't an injected secondary bean using the same scope contain the form data from the submit? Using an explicit method call with parameter from a (JSF) commandButton action..

I've resorted to including the same parameter in the calling bean, and then it works fine. But this is annoying as I'd prefer to A. not pass things around like crazy and B. reuse a particular set of settings in several places.

The environment running my application is a Wildfly 11, problem below occured on a Windows box but will run in production on Linux.

My webpage and relevant code from the beans in question:

<ui:composition template="/WEB-INF/template.xhtml" ...
                xmlns:h="http://xmlns.jcp.org/jsf/html">
...
<h:form>
<h:selectBooleanCheckbox id="box1" value="#{mainBean.someBoolean}"/>
<h:selectBooleanCheckbox id="box2" value="#{secondBean.someBoolean}"/>
<h:commandButton id="cmdButton" value="Btn Text" action="#{mainBean.actionMethod(sessionBean.value)}"/>
</h:form>
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;
...
@Named
@RequestScoped
public class MainBean implements Serializable {
    @Inject
    private SecondBean secondBean;

    private boolean someBoolean;
    // with getter and setter

    public void actionMethod(Object value) {
        // In here, someBoolean and value are properly set, while secondBean.isSomeBoolean() always returns default/false regardless of the checkbox on the page.
    }
...
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import java.io.Serializable;
...
@Named
@RequestScoped
public class SecondBean implements Serializable {
    private boolean someBoolean;
    // with getter and setter
...

Why is one bean properly populated with form data and not the other?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Cygone
  • 1
  • 1
  • In the action method the value cannot be set since it refers to a completely different bean that is not in the code. And what if you remove the template? And what if you run in development mode? Any errors/warnings/... And how do you know it is empty? Can you add 'sysouts' that can be used to demonstrate it? – Kukeltje Sep 04 '19 at 07:17
  • @Kukeltje within a request `MainBean.secondBean.isSomeBoolean()` and `#{secondBean.someBoolean}` should refer to the same field. – Selaron Sep 04 '19 at 07:37
  • @Selaron: Yes, I know, it most likely is, but it should be in the code. And the actionMethod takes a `sessionBean` that is not even there. – Kukeltje Sep 04 '19 at 08:05
  • I omitted the sessionBean code, but it's a value that's set previously, and available as that bean is sessionScoped. The thing is I expected the form submission to update values on all Named beans referred, regardless of they're the target for the action or not. I've never read that a form can only affect one bean? I've determined the (missing) value by inspection, both by remote-debug and by inspection in log statements (I've omitted more code as well for clarity). – Cygone Sep 04 '19 at 19:04

0 Answers0