首页 > 解决方案 > 使用 jsf 标签发送参数

问题描述

   <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">
    <ui:composition>
    <h:form>
    <h:panelGrid columns="2">
    <h:outputText value="New categorie"></h:outputText>
    <h:inputText value="${categorieBean.categorie.name}"></h:inputText>
    <h:commandButton value="Add categorie" action="#{categorieBean.dodajKategoriju}">
    <f:ajax render="@form" execute="@form"></f:ajax>
    </h:commandButton>
    </h:panelGrid> 
    <h:dataTable value="${categorieBean.categories}" var="categor">
    <h:column>
    <f:facet name="header">Name</f:facet>
    <h:outputText value="${categor.name}"></h:outputText>
    </h:column>
    <h:column>
    <f:facet name="header">DELETE</f:facet>
    <h:commandButton value="Delete-${categor.id}" action="#{categorieBean.obrisiKategoriju()}" 
    style="width: 64px;background-color: green;color: white;border: none;height: 27px;border-radius: 
    5px;">
    <f:ajax render="@form" execute="@form"></f:ajax>
    <f:param value="${categor.id}" name="id"></f:param>
    </h:commandButton>
    </h:column>
    </h:dataTable>


    </h:form>
    </ui:composition>
    </html>

      @ManagedBean(name="categorieBean")
     @SessionScoped
     public class CategorieBean implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7281388938441323040L;
    private List<Categorie> categories=new ArrayList<Categorie>();
    private Categorie categorie=new Categorie();

    public CategorieBean() {
        // TODO Auto-generated constructor stub
    }

    public List<Categorie> getCategories() {
        categories=CategorieDAO.readAll();
        return categories;
    }

    public void setCategories(List<Categorie> categories) {
        this.categories = categories;
    }

    public Categorie getCategorie() {
        return categorie;
    }

    public void setCategorie(Categorie categorie) {
        this.categorie = categorie;
    }

    public String obrisiKategoriju() {
        System.out.println("hocu da obrisem kategoriju");
        HttpServletRequest req=(HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        System.out.println(req.getParameter("id"));
        Map<String,String> mapa=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
        for(Entry<String,String>pom:mapa.entrySet()) {
            System.out.println(pom.getKey()+" "+pom.getValue());
        }
        if(mapa.containsKey("id")) {
            System.out.println("Usao sam");
            int id=Integer.parseInt(mapa.get("id"));
            CategorieDAO.delete(id);
        }
        return "";
    }

    public String dodajKategoriju() {
        CategorieDAO.insert(categorie);
        return "";
    }

}

当我打印 requestparammap 的值时,我没有在方法 obrisiKategoriju() 中将 id 参数发送到 Bean 我只得到 j_idt6 j_idt6 j_idt6:j_idt9 j_idt6:j_idt11:0:j_idt17 Delete-1 javax.faces.ViewState 9066186310981453487:-225522103390500738 有人有想法吗?我有另一个 Bean,但它工作得很好,有了这些它没有,我尝试了一切

标签: javajavabeans

解决方案


推荐阅读