0

Good day

This issue has been resolved while creating a MCVE (thanks Kukeltje for making me do that :) )

See the answer posted below for a MCVE that works.

@SessionScoped
@Stateful
public class StoringUserInfo implements Serializable {

    private HashMap<String, String> userSettingMap;
    private String userName = "Test";

    public StoringUserInfo() {
        this.userSettingMap = new HashMap<>();
    }

    public void addSetting(String name, String setting) {
        userSettingMap.put(name, setting);
    }

    public String getSetting(String name) {
        return userSettingMap.get(name);
    }

    public HashMap<String, String> getUserSettingMap() {
        return userSettingMap;
    }

    public void setUserSettingMap(HashMap<String, String> userSettingMap) {
        this.userSettingMap = userSettingMap;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String projectName) {
        this.projectName = userName;
    }
}

Then for instance in one @ViewScoped @Named bean I want to do this:

@Named
@ViewScoped
public class NewProjectView implements Serializable {

    private static final long serialVersionUID = 1L;
    private String userName;
    @Inject private transient StoringUserInfo userInfo;

    public void submitForm(){
        userInfo.setUserName("NEW")
        // return to dashboard
        FacesContext.getCurrentInstance().getViewRoot().getViewMap().clear();
        PrimeFaces.current().ajax().update(":content", ":formHeader:mainMenu", ":formHeader:growl");
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

Then on another @ViewScoped @Named bean I want to retrieve the stored value as such (the try is suppress the error on app deployment due to the @PostConstruct getting nulls)

@Named
@ViewScoped
public class EditProjectView implements Serializable {

    private static final long serialVersionUID = 1L;
    private String userName;
    @Inject private transient StoringUserInfo userInfo;


    @PostConstruct
    public void init() {
        getValues()
    }

    private void getValues(){
    try {
        userName = userInfo.getUserName(); // this must display on form load
    } catch (Exception e) {
        e.printStackTrace();
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

}

The workflow inside the app ensures that the page that sets the values are visited before the page / pages that need the values. Debugging correctly shows this as well.

However, the @ViewScoped @Named beans always display null, they never reread the values from the @SesionScoped bean? What am I missing here?

Setting a default value to the userName String in the @SessionScoped @Stateful bean shows the name 'Test' on the second form.

However the updated value never gets read on form render / load. It thus seems that @PostConstruct only executes at app deployment time?

I also added this to my xhtml file:

<o:form id="editProjectForm">
        <f:metadata>
            <f:viewAction action="#{editProjectView.getProjectValues}"/>
            <p:ajax update="editProjectFieldSet"/>
        </f:metadata>
        <p:fieldset id="editProjectFieldSet" legend="#{editProjectView.userName}"/>

But it still does not re query the SessionScoped bean before render. It only shows the default init value of 'Test'

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Letholdrus
  • 1,261
  • 3
  • 20
  • 36
  • 1
    What does @Data do? What if you remove it? What if you remove transient? And are you sure the sessionscoped bean is created correctly? – Kukeltje Sep 11 '18 at 08:04
  • @Kukeltje Good day Kukeltje, Data is the Lombok annotation to add getters and setters.I removed transient but still no change. The debugger shows that the session bean gets set correctly and it stores the value in the hashmap correctly. It is just the JSF forms don't seem te be able to reread the values. How would one force a form to execute the PostConstruct as soon as it is shown on screen again? – Letholdrus Sep 11 '18 at 08:29
  • I also requested to remove the `@Data` just to make sure it messes with the injection. Use explicit getters and setters. Oh and btw, your CDI managedbeans have the same classname and hence cdi name. That cannot work. And the first one does not read the 'name' anywhere. Please make it a real [mcve] – Kukeltje Sep 11 '18 at 08:44
  • @Kukeltje Apologies, the similar naming was a typo. I corrected it. I also removed the Lombok annotation and added manual getters and setters. Still does not work. I tried using the '' metadata tag to get the values before the form renders, but no success. – Letholdrus Sep 11 '18 at 09:05
  • Do you have the 'correct' `@SessionScoped` annotation on the EJB? Could it be that by chance it behaves like a request scoped one? Try by e.g. setting the username to a default value in the EJB so you can see the basic features work – Kukeltje Sep 11 '18 at 09:08
  • @Kukeltje I set a default String in the '@SessionScoped' bean and it pulls through to the second view. Thus the default gets 'seen' on the '@PostConstruct' but never updated with the updated value on actual form render. – Letholdrus Sep 11 '18 at 09:26
  • Then start making a [mcve] (your ejb contains errors that make it fail to compile!!!) And _"but never updated with the updated"_ is vague. How and when is it updated? Theses are all all steps you can/should do upfront. It is called debugging and it helps narrow down the problem (instead of adding additional code) and prevents us 'wasting' time getting all this info by asking questions... Cheers – Kukeltje Sep 11 '18 at 09:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179817/discussion-between-letholdrus-and-kukeltje). – Letholdrus Sep 11 '18 at 13:36
  • Where have you imported your ViewScoped from? – kolossus Sep 11 '18 at 23:26
  • ViewScoped from OmniFaces was used. Changed to default JSF ViewScope and explicitly declared getters and setters. All works now. – Letholdrus Sep 12 '18 at 05:00

3 Answers3

1

The main difference between the two versions are:

 1. Replaced <o:form> with <h:form> 

Application is running on WildFly 14 with PrimeFaces 6.2.9 and OmniFaces 3.2

Letholdrus
  • 1,261
  • 3
  • 20
  • 36
  • Please add an explanation about the differences. To cumbersome for others to see what might have caused it. Then there is no need to post the full code in the answer – Kukeltje Sep 12 '18 at 05:52
  • @Kukeltje Done as requested. – Letholdrus Sep 12 '18 at 06:11
  • does doing 2 and not 1 still make it not work? Are **both** needed? – Kukeltje Sep 12 '18 at 08:28
  • @Kukeltje Tested it and only 1 is needed. – Letholdrus Sep 12 '18 at 13:45
  • 1
    Can you try with `` ? – Kukeltje Sep 12 '18 at 14:18
  • Then I'd like to see, in [mcve] flavour, the xhtml in your page. Your solution might merely be a workaround and the real issue is a combination of the new default functionality of `o:form` and #9 in https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value – Kukeltje Sep 13 '18 at 18:00
0

If you're using CDI, make sure to import javax.enterprise.context.sessionscoped instead of javax.faces.bean.sessionscoped.

trulley
  • 9
  • 2
-1

try this:

HttpSession session = request.getSession();

so then you can request a parameter with an name:

String email = (String) session.getAttribute("emailsession");