【问题标题】:Can't get values from jsf page无法从 jsf 页面获取值
【发布时间】:2017-08-17 19:42:56
【问题描述】:

我的托管 bean 从 jsf 页面中的简单表单获取空值。实际上,我有一个相当发达的界面,但即使使用简单的表格,问题也会发生。

(JSF 2.1.0,java 编译器 1.6,primefaces 6.0 和 tomcat7)

这似乎是一个重复的问题。但是,5天前我正在寻找这个奇怪问题的解决方案。我找到了很多解决方案,但它对我不起作用。也许我不太理解他们作为 BalusC (commandButton/commandLink/ajax action/listener method not invoked or input value not updated) 的答案。

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://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:c="http://java.sun.com/jsp/jstl/core"> 
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>    
<h:body>
    <h:form>
            <h:inputText value="#{notification.typeSelected}"></h:inputText>
            <p:commandButton value="Enregistrer"
                actionListener="#{notificationBean.filtrer}" process="@this"
                style="float:right">
            </p:commandButton>
        </h:form>
</h:body>
</html>

托管 bean:

package presentation;
import java.util.ArrayList;
import java.util.List;    
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
//import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;    
import org.apache.log4j.Logger;    
import entities.Contrainte;
import metier.ContrainteMetier;
import metier.ContrainteMetierImp;    
@ManagedBean
@SessionScoped
public class NotificationBean implements java.io.Serializable{
    private static final long serialVersionUID = 1L;

    public static Logger log = Logger.getLogger(ProjetMODBean.class);

    private ContrainteMetier contrainteM = new ContrainteMetierImp();
    private List<Contrainte> contrainteL;
    private List<Contrainte> filtreL;
    private List<SelectItem> classificationL;
    private String typeSelected;
    //Constructeurs
    public NotificationBean() {
    }
    public NotificationBean(ContrainteMetier contrainteM, List<Contrainte> contrainteL) {
        super();
        this.contrainteM = contrainteM;
        this.contrainteL = contrainteL;
    }

    @PostConstruct
    public void init() {

        contrainteL = contrainteM.findAll();
        classificationL = new ArrayList<SelectItem>();

        for(Contrainte c:contrainteL)
        {//Attention, il ne faut pas dupliquer les types dans la liste déroulante           
            classificationL.add(new SelectItem(c.getClassification()));
        }
        log.info(" dans init je filtre selon: " + getTypeSelected());


    }


    //Méthodes
    public void filtrer(ActionEvent e)
    {
        setTypeSelected(typeSelected);
        log.info("je filtre selon: " + getTypeSelected());      
    }

控制台输出:

2017-03-24 18:06:43 INFO  ProjetMODBean:57 -  dans init je filtre selon: null
2017-03-24 18:06:43 INFO  ProjetMODBean:76 - je filtre selon: null  

我试图纠正我的错误,但我漏掉了一个细节。

【问题讨论】:

  • 访问 postConstruct 中未注入的字段很奇怪。我希望是 NPE,或者你以错误的方式使用 bean

标签: maven tomcat jsf primefaces managed-bean


【解决方案1】:

恕我直言,您的问题是 process="@this" 将其更改为 process="@form" (这是默认值,因此只有在您有充分理由时才更改它)或将 id 添加到输入并使用 process="@this idOfInputField"

question

请用英文完整发布问题。 我将假设 //Méthodes 表示字段的 getter/setter(JSF 要求输入的 getter/setter)。

也为服务使用注入。 将方法头从 public void filtrer(ActionEvent e) 更改为 public void filtrer()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    相关资源
    最近更新 更多