简体   繁体   中英

@ViewScoped JSF bean getting initialized multiple times

I'm using primefaces 4.0 and Mojarra 2.2.6

I'm opening a dialog box (modal) from a session scoped bean - in the dialogue I've a @ViewScoped bean initialized which retrieves few list of names (strings) from the session map etc.

Problem is that every time I submit by clicking on the save button, new viewScoped bean is created which is obviously not the behavior I want. I saw many other questions on the same topic answered and have also tried setting web.xml with:

<context-param> <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name> <param-value>false</param-value> </context-param>

But it still doesn't work. I also eliminated almost every tag except an inputfield and the submit button to eliminate any specific tag handler that may be causing it but still can't seem it to pinpoint why it's not working. Here is my sample xhtml page and viewScope bean save method:

session bean code :

            FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("NAMES", names);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("modal", true);
        map.put("draggable", false);
        map.put("resizable", false);
        map.put("dynamic", true);
        map.put("contentHeight", 500);
        RequestContext.getCurrentInstance().openDialog("SaveNames.xhtml", map, null );

XHTML : <ui:composition 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" xmlns:p="http://primefaces.org/ui" xmlns:fn="http://java.sun.com/jsp/jstl/functions" xmlns:o="http://omnifaces.org/ui" xmlns:of="http://omnifaces.org/functions"> <h:form id="myform" <p:inputText id="newFolderName" disabled="#{not mybean.list}" value="#{mybean.newName}" autocomplete="off"></p:inputText> <p:commandButton id="saveNewNameButton" type="push" value="Save" action="#{mybean.saveNewName}" icon="ui-icon-disk" oncomplete="exit()" style="float: right"> </p:commandButton> </h:form> view scope bean :

public void saveNewName()
{ //saving logic goes here
}

Ok, after whole day of troubleshooting found out really silly reason for this. Posting answer in case someone else may end up having same issue.

I was using the wrong @ViewScoped package annotation. Apparently there are two annotations, javax.faces.view.ViewScoped and javax.bean.ViewScoped -

The correct one to use is javax.bean.ViewScoped - however if you are not paying attention in STS/eclipse when it suggest packages in autocomplete that you may end picking up the wrong one which may cause this issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM