简体   繁体   English

JSF 2.1 SelectOneMenu自动切换到初始值

[英]JSF 2.1 SelectOneMenu toggling automatically to init values

I have 2 SelectOneMenu as follows in the index.xhtml. 我在index.xhtml中有2个SelectOneMenu,如下所示。 The menu1 essentially chooses a language(sp or en) and menu2 displays the possible serial numbers(0 to 3). menu1本质上选择一种语言(sp或en),而menu2显示可能的序列号(0至3)。 I have the init constructor(post constructor) which initialises the default values on the two Menus. 我有init构造函数(后构造函数),用于初始化两个菜单上的默认值。 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 :( 但是出于某种奇怪的原因,如果我为默认语言以外的语言选择了默认序列号以外的序列号,那么该语言将以某种方式重置为init default :(

<?xml version='1.0' encoding='UTF-8' ?>
<!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:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:f="http://java.sun.com/jsf/core">

    <h:head>
        <title>My page</title>
    </h:head>
    <h:body>
                            <div>
                                <h4>Change Existing Description</h4>
                            </div>
                            <h:form id="myForm">
                                    <h:panelGrid columns="4">
                                        <h:outputLabel value="Language:" />
                                        <h:selectOneMenu value="#{myBean.language}">
                                            <f:selectItems value="#{myBean.languages}" />
                                            <f:ajax listener="#{myBean.doUpdate}" render ="myForm" />
                                        </h:selectOneMenu>
                                        <h:outputLabel value="SerialID:" />
                                        <h:selectOneMenu value="#{myBean.serialID}">
                                            <f:selectItems value="#{myBean.serialIDs}" />
                                            <f:ajax listener="#{myBean.doUpdate}" render ="myForm" />
                                        </h:selectOneMenu>

                                    </h:panelGrid>
                                </h:form>
    </h:body>
</html>

Here is my Bean code. 这是我的Bean代码。 Where is the problem?? 问题出在哪儿?? please advise! 请指教!

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<String> languages;
    private String language;
    private int serialID;
    private List<Integer> 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<String> 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<Integer> getSerialIDs() {
        System.out.println("getSerialIDs language =  "+language );
        return serialIDs;
    }

    public List<String> getAllLanguages() {
        List<String> results = new ArrayList<String>();
        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<Integer> getSerialIDsFromOverview() {
        List<Integer> results = new ArrayList<Integer>();
        results.add(0);
        results.add(1);
        results.add(2);
        results.add(3);
        return results;
    }

}

UPDATES: 更新:

After taking suggestions from cubbuk , I sat down and corrected my code with @ViewScoped annotation and making the bean implement Serializable. 接受cubbuk的建议后,我坐下来用@ViewScoped批注更正了我的代码,并使Bean实现了Serializable。 THIS WORKS. 这项工作。 However, the next thing I had to do was include an @EJB annotation to call a stateless bean which calls the Entity manager to fetch the serialIDs from a database instead of "hardcoding" it. 但是,我接下来要做的是包含一个@EJB注释以调用无状态bean,该无状态bean调用实体管理器从数据库中获取serialID,而不是对其进行“硬编码”。 That is when I encounter the problem: Not serializable exception " java.io.NotSerializableException: bean.__EJB31_Generated__ . 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 :( 那就是我遇到的问题:不可序列化的异常“ java.io.NotSerializableException:bean .__ EJB31_Generated__ 。如何解决此问题?当我将myBean返回到RequestScope并删除Serializable时,我可以运行代码而不会出现问题,但是会出现切换菜单中的初始值:(

By the way I check this post: @EJB in @ViewScoped managed bean causes java.io.NotSerializableException and set my STATE SAVING METHOD to server but that gives me "empy response from server" pop up message :( Please help! 顺便说一下,我检查了这篇文章: @ViewScoped托管bean中的@EJB导致java.io.NotSerializableException并将我的状态保存方法设置为服务器,但是这给了我“服务器的空响应”弹出消息:(请帮助!

Since you are using @RequestScoped bean as your backing bean after each request your init method is getting called and your values are getting reset. 由于在每个请求之后都将@RequestScoped bean用作后备bean,因此将调用init方法,并且将重置您的值。 To avoid that you need to use @ViewScoped bean as your backing bean. 为了避免这种情况,您需要使用@ViewScoped bean作为支持bean。 I updated your bean accordingly note that your backing bean now implements Serializable interface. 我相应地更新了您的bean,请注意,您的支持bean现在实现了Serializable接口。 This is needed as this bean will be stored in your servlet and it needs to be flushed to disk if the content can not be hold in the memory. 这是必需的,因为此bean将存储在您的servlet中,并且如果内容不能保存在内存中,则需要将其刷新到磁盘。 For learning the details of @ViewScoped beans please check the following post: 要了解@ViewScoped bean的详细信息,请查看以下文章:

http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html http://balusc.blogspot.com/2010/06/benefits-and-pitfalls-of-viewscoped.html

Apart from these, for naming conventions I renamed your getAllLanguages and getSerialIDsFromOverview methods to initAllLanguages and initSerialIds as methods starting with get and set can be confusing because they are mostly used for getters and setters. 除此之外,对于命名约定,我将getAllLanguagesgetSerialIDsFromOverview方法重命名为initAllLanguagesinitSerialIds因为以get和set开头的方法可能会造成混淆,因为它们主要用于getter和setter。

Lastly when you use f:ajax command by default the UIInput the ajax command is bind to is rendered and executed. 最后,默认情况下,当您使用f:ajax命令时,将呈现并执行绑定到ajax命令的UIInput Since you don't refresh the h:selectOneMenu menus according to the values of each other you don't need to render the whole form. 由于您无需根据彼此的值刷新h:selectOneMenu菜单,因此无需呈现整个表单。 The following will be enough for this case: 对于这种情况,以下内容就足够了:

<h:form id="myForm">
                                    <h:panelGrid columns="4">
                                        <h:outputLabel value="Language:" />
                                        <h:selectOneMenu value="#{myBean.language}">
                                            <f:selectItems value="#{myBean.languages}" />
                                            <f:ajax listener="#{myBean.doUpdate}"/>
                                        </h:selectOneMenu>
                                        <h:outputLabel value="SerialID:" />
                                        <h:selectOneMenu value="#{myBean.serialID}">
                                            <f:selectItems value="#{myBean.serialIDs}" />
                                            <f:ajax listener="#{myBean.doUpdate}"/>
                                        </h:selectOneMenu>

                                    </h:panelGrid>
                                </h:form>

@ManagedBean
@ViewScoped
public class MyBean implements Serializable
{

    public static final int PERMISSIONS = 2;
    private List<String> languages;
    private String language;
    private int serialID;
    private List<Integer> serialIDs;

    /**
     * init() method for initializing the bean. Is called after constuction.
     */
    @PostConstruct
    protected void init()
    {
        //public MyBean () {
        System.out.println("lang: " + language);
        System.out.println("serialId: " + serialID);
        System.out.println("init is called");
        initAllLanguages();
        initSerialIds();
        language = "en"; //defaultLanguage 
        serialID = 3;
    }

    public List<String> getLanguages()
    {
        return languages;
    }

    public int getPERMISSIONS()
    {
        return PERMISSIONS;
    }

    public String getLanguage()
    {
        return language;
    }

    public void setLanguage(String language)
    {
        this.language = language;
    }

    public int getSerialID()
    {
        return serialID;
    }

    public void setSerialID(int serialID)
    {
        this.serialID = serialID;
    }

    public List<Integer> getSerialIDs()
    {
        return serialIDs;
    }

    private void initAllLanguages()
    {
        languages = new ArrayList<String>();
        languages.add("sp");
        languages.add("en");
    }

    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 void initSerialIds()
    {
        serialIDs = new ArrayList<Integer>();
        serialIDs.add(0);
        serialIDs.add(1);
        serialIDs.add(2);
        serialIDs.add(3);
    }
}

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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