簡體   English   中英

h:inputText的值在ManagedBean中為null

[英]Value of h:inputText is null in ManagedBean

我在JSF中有以下代碼

<h:outputLabel value="Date" for="rdate" />
   <h:inputText id="rdate" autocomplete="off" 
       value="#{myMB.abstractProject.joinedDate}">
   </h:inputText>

在Entity類中,我聲明為

private Date joinedDate; 

public Date getJoinedDate() {
    return joinedDate;
}

public void setJoinedDate(Date joinedDate) {
    this.joinedDate= joinedDate;
}

問題是,在ManagedBean中,我對以下內容變為null

System.out.println("date in save method "
+ abstractRequest.getJoinedDate());

這可能是什么原因? h:inputText<h:form> 我的bean的范圍是@ViewAccessScoped

你需要使用f:convertDateTime 喜歡:

<h:outputLabel value="Date" for="rdate" />
<h:inputText id="rdate" autocomplete="off" value="#{myMB.abstractProject.joinedDate}" label="Date">
   <f:convertDateTime pattern="dd-MM-yyyy" />
</h:inputText>

是一個例子。

編輯:

這是我做的:

xhtml:

<ui:composition 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" template="/WEB-INF/templates/globalTemplate.xhtml">

    <ui:define name="title">1532116</ui:define>
    <ui:define name="content">          
        <h:form>
            <h:outputLabel value="Date" for="date" />
            <h:inputText id="date" value="#{so15321163.date}" label="Date" required="true">
                <f:convertDateTime pattern="dd-MM-yyyy"/>
            </h:inputText>              
            <h:message for="date" style="color:red" />              
            <h:commandButton value="Submit" actionListener="#{so15321163.listener}"/>                   
        </h:form>
    </ui:define>    
</ui:composition>

托管bean:

package app.so.dev.web.controller;

import java.io.Serializable;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name="so15321163")
@ViewScoped // @SessionScoped
public class SO15321163 implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 8012804893825661900L;
    private Date date;

    @PostConstruct
    public void init() {

    }

    public void listener(ActionEvent event) {
        System.out.println(date);
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }   
}

不確定,但是如果你缺少@Named注釋,那么jsf bean無法正常工作。

以下是一些更詳細的解釋: https//cwiki.apache.org/confluence/display/EXTCDI/Conversations

我已經解決了這個問題,問題是在另一個表單元素中有一個Converter錯誤,它停止了表單提交。 我使用了Converter類來解決這個問題。

謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM