Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 2.1 SelectOneMenu toggling automatically to init values
    primarykey
    data
    text
    <p>I have 2 SelectOneMenu as follows in the index.xhtml. The menu1 essentially chooses a language(sp or en) and menu2 displays the possible serial numbers(0 to 3). I have the init constructor(post constructor) which initialises the default values on the two Menus. However for some strange reason, if I select a serial number other than the default serial number for the language other than the default language, somehow the language gets reset to init default :(</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"&gt; &lt;h:head&gt; &lt;title&gt;My page&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; &lt;div&gt; &lt;h4&gt;Change Existing Description&lt;/h4&gt; &lt;/div&gt; &lt;h:form id="myForm"&gt; &lt;h:panelGrid columns="4"&gt; &lt;h:outputLabel value="Language:" /&gt; &lt;h:selectOneMenu value="#{myBean.language}"&gt; &lt;f:selectItems value="#{myBean.languages}" /&gt; &lt;f:ajax listener="#{myBean.doUpdate}" render ="myForm" /&gt; &lt;/h:selectOneMenu&gt; &lt;h:outputLabel value="SerialID:" /&gt; &lt;h:selectOneMenu value="#{myBean.serialID}"&gt; &lt;f:selectItems value="#{myBean.serialIDs}" /&gt; &lt;f:ajax listener="#{myBean.doUpdate}" render ="myForm" /&gt; &lt;/h:selectOneMenu&gt; &lt;/h:panelGrid&gt; &lt;/h:form&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p>Here is my Bean code. Where is the problem?? please advise!</p> <pre><code>package bean; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import javax.ejb.Stateful; import javax.enterprise.context.RequestScoped; import javax.faces.bean.ManagedBean; @ManagedBean(name = "myBean") //@Stateless @Stateful @RequestScoped public class MyBean { public static final int PERMISSIONS = 2; private List&lt;String&gt; languages; private String language; private int serialID; private List&lt;Integer&gt; serialIDs; /** * init() method for initializing the bean. Is called after constuction. */ @PostConstruct private void init() { //public MyBean () { languages = getAllLanguages(); language = "en"; //defaultLanguage serialID = 3; serialIDs = getSerialIDsFromOverview(); } public List&lt;String&gt; getLanguages() { System.out.println("getLanguages, language " +language); return languages; } public int getPERMISSIONS() { return PERMISSIONS; } public String getLanguage() { System.out.println("getLanguage " +language); return language; } public void setLanguage(String language) { System.out.println("setLanguage " +language); this.language = language; } public int getSerialID() { System.out.println("getSerialID " +serialID); return serialID; } public void setSerialID(int serialID) { System.out.println("setSerialID " +serialID); this.serialID = serialID; } public List&lt;Integer&gt; getSerialIDs() { System.out.println("getSerialIDs language = "+language ); return serialIDs; } public List&lt;String&gt; getAllLanguages() { List&lt;String&gt; results = new ArrayList&lt;String&gt;(); results.add("sp"); results.add("en"); if(results != null){ System.out.println("getting all languages"); } return results; } public void doUpdate() { System.out.println("doUpdate language " +language); System.out.println("doUpdate serialID " +serialID); } /** * Returns a list of all serialIDs present in the overview. * @return */ private List&lt;Integer&gt; getSerialIDsFromOverview() { List&lt;Integer&gt; results = new ArrayList&lt;Integer&gt;(); results.add(0); results.add(1); results.add(2); results.add(3); return results; } } </code></pre> <p>UPDATES:</p> <p>After taking suggestions from <strong>cubbuk</strong>, I sat down and corrected my code with @ViewScoped annotation and making the bean implement Serializable. THIS WORKS. However, the next thing I had to do was include an <strong>@EJB annotation</strong> to call a stateless bean which calls the Entity manager to fetch the serialIDs from a database instead of "hardcoding" it. That is when I encounter the problem: Not serializable exception "<strong>java.io.NotSerializableException: bean.__EJB31_Generated__</strong>. How do I solve this? When I made myBean back to RequestScope and remove Serializable, I could run the code without problems however there the toggling of the menu to init values :( </p> <p>By the way I check this post: <a href="https://stackoverflow.com/questions/3037722/ejb-in-viewscoped-managed-bean-causes-java-io-notserializableexception">@EJB in @ViewScoped managed bean causes java.io.NotSerializableException</a> and set my STATE SAVING METHOD to server but that gives me "empy response from server" pop up message :( Please help!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload