有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

h:inputText的java值在ManagedBean中为空

我在JSF中有以下代码

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

在实体类中,我声明

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在{}中有效。我的bean的范围是@ViewAccessScoped


共 (3) 个答案

  1. # 1 楼答案

    你需要使用^{}。比如:

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

    Here就是一个例子

    编辑:

    以下是我所做的:

    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;
        }   
    }
    
  2. # 2 楼答案

    我已经解决了这个问题,问题是另一个表单元素中出现了Converter错误,导致表单提交停止。我使用了Converter类来解决这个问题

    谢谢