JSF 2.1 SelectOneMenu自动切换到init值 [英] JSF 2.1 SelectOneMenu toggling automatically to init values

查看:198
本文介绍了JSF 2.1 SelectOneMenu自动切换到init值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在index.xhtml中,我有2个SelectOneMenu,如下所示. menu1本质上选择一种语言(sp或en),而menu2显示可能的序列号(0至3).我有init构造函数(后构造函数),用于初始化两个菜单上的默认值.但是出于某种奇怪的原因,如果我为默认语言以外的其他语言选择了默认序列号以外的序列号,那么该语言将以某种方式重置为init default:(

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 :(

<?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>

这是我的Bean代码.问题出在哪儿??请指教!

Here is my Bean code. 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;
    }

}

更新:

cubbuk 获得建议后,我坐下来用@ViewScoped批注更正了我的代码,并使Bean实现了Serializable.这项工作.但是,我接下来要做的是包含一个 @EJB注释来调用无状态bean,该bean调用实体管理器从数据库中获取serialID,而不是对其进行硬编码".那就是我遇到的问题:不可序列化的异常" java.io.NotSerializableException:bean .__ EJB31_Generated __ .如何解决此问题?当我将myBean返回到RequestScope并删除Serializable时,我可以运行代码没有问题,但是菜单切换到初始值:(

After taking suggestions from cubbuk, 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 @EJB annotation 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 "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 :(

通过我检查此帖子的方式: @EJB in @ViewScoped托管bean导致java.io.NotSerializableException 并将我的状态保存方法设置为服务器,但这给了我来自服务器的空响应"弹出消息:( 请帮忙!

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!

推荐答案

由于在每次请求后都会调用@RequestScoped bean作为后备bean,因此将调用init方法,并且您的值也将被重置.为避免这种情况,您需要使用@ViewScoped bean作为后备bean.我相应地更新了您的bean,请注意,您的支持bean现在实现了Serializable接口.这是必需的,因为此bean将存储在您的servlet中,并且如果内容不能保存在内存中,则需要将其刷新到磁盘.要了解@ViewScoped bean的详细信息,请查看以下文章:

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. To avoid that you need to use @ViewScoped bean as your backing bean. I updated your bean accordingly note that your backing bean now implements Serializable interface. 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. For learning the details of @ViewScoped beans please check the following post:

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

除了这些,对于命名约定,我将您的getAllLanguagesgetSerialIDsFromOverview方法重命名为initAllLanguagesinitSerialIds,因为以get和set开头的方法可能会造成混淆,因为它们主要用于getter和setter.

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.

最后,当默认情况下使用f:ajax命令时,将呈现并执行与ajax命令绑定的UIInput.由于您不必根据彼此的值刷新h:selectOneMenu菜单,因此无需呈现整个表单.对于这种情况,下面的内容就足够了:

Lastly when you use f:ajax command by default the UIInput the ajax command is bind to is rendered and executed. 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. 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);
    }
}

欢呼

这篇关于JSF 2.1 SelectOneMenu自动切换到init值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆