【问题标题】:CommandLink doesn't work. Tried everythingCommandLink 不起作用。尝试了一切
【发布时间】:2014-08-18 03:30:58
【问题描述】:

这是我在网站上的第一个问题 :) 但首先,抱歉我的英语不好,我正在学习 :) LZ,我需要你的帮助。我被 JSF 中的一个应用程序阻止了。

我有这个

<?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:ui="http://xmlns.jcp.org/jsf/facelets"      
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">

    <body>

        <ui:composition template="./defaultTemplate.xhtml">
            <ui:define name="content">

                    <h1 style="margin-bottom: 0px">#{msg.default_2}</h1>
                    <br/>
                    <ul style="padding-left: 0px">

                        <ui:repeat value="#{categoryMB.categories}" var="categorie">              
                                <h:outputLabel value=" -==- " style="color: #FF8620; font-size: 10px; padding-left: 0px"></h:outputLabel>
                                <h:form>
                                <h:commandLink value="#{categorie.categoryname}" action="#{categoryMB.getItemsByCat(categorie.categoryid.id)}"/> 
                                </h:form>
                        </ui:repeat>

                    </ul>


                    <ui:repeat value="#{categoryMB.listItems}" var="item">
                        <div class="itemCategory">
                            <h:graphicImage class="item-image" url="#{item.urlimage}"/> 
                            <h:outputLabel value="#{item.price} €" class="prix"></h:outputLabel>
                            <br/>
                            <h2><h:outputLabel value="#{item.name}"></h:outputLabel></h2>
                            <br/>
                            <h:form>

                                <h:commandLink value="#{msg.default_14}" action="#{itemMB.linkItem(item.id)}"
></h:commandLink>

                            </h:form>
                        </div>
                    </ui:repeat>

            </ui:define>


        </ui:composition>

    </body> </html>

一切都很好,除了第二个 commandLink ! 我无法执行该操作。我总是回到同一页... 我尽我所能,我在网站上阅读了所有关于该主题的主题,但我找不到解决方案。拜托,我在问你,帮帮我。我要疯了。

我的 categoryMB bean:

package managedBean;

import entityBean.Item;
import entityBean.Translatecategory;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.view.ViewScoped;
import sessionBean.ItemFacadeLocal;
import sessionBean.TranslatecategoryFacadeLocal;

/**
 *
 * @author Aurélien
 */
@ManagedBean
@ViewScoped
public class CategoryMB {
    @EJB
    private ItemFacadeLocal itemFacade;
    @EJB
    private TranslatecategoryFacadeLocal translatecategoryFacade;


    @ManagedProperty("#{internationalizationMB}")
    private InternationalizationMB language;

    private List<Item> listItems;
    /**
     * Creates a new instance of CategoryMB
     */
    public CategoryMB() {
    }

    public List<Translatecategory> getCategories () {     
        return translatecategoryFacade.findByLanguage(language.getLocale().getLanguage());       
    }

    public void getItemsByCat (int idCat) {
        setListItems(itemFacade.findByCat(idCat));
    }


    public InternationalizationMB getLanguage() {
        return language;
    }

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

    public List<Item> getListItems() {
        return listItems;
    }

    public void setListItems(List<Item> listItems) {
        this.listItems = listItems;
    }
}

还有我的 itemMB bean:

package managedBean;

import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;

@ManagedBean
@SessionScoped
public class ItemMB implements Serializable {

    private int idItem;
    /**
     * Creates a new instance of ItemMB
     */
    public ItemMB() {
    }

    public int getIdItem() {
        return idItem;
    }

    public void setIdItem(int idItem) {
        this.idItem = idItem;
    }

    public String linkItem(int id)
    {
        setIdItem(id);
        return "item";
    }

}

【问题讨论】:

    标签: jsf


    【解决方案1】:

    您将 CDI @SessionScoped 与 JSF 2.x @SessionScoped 混合使用。这在您的导入中注明:

    import javax.enterprise.context.SessionScoped;
    
    @ManagedBean
    @SessionScoped
    public class ItemMB implements Serializable {
        //...
    }
    

    这使您的托管 bean 具有默认范围,在 JSF 2 中为 @RequestScoped,因此您的托管 bean 将在每次请求时重新创建。

    将您的导入修复为:

    import javax.faces.bean.SessionScoped;
    

    如果您碰巧使用 JSF 2.2.x,请开始使用 CDI 1.1。对托管 bean 使用 @Named,对 @ViewScoped 使用 javax.faces.view.ViewScoped

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 2015-09-23
      • 2020-01-30
      • 2012-02-09
      • 2019-09-19
      相关资源
      最近更新 更多