【问题标题】:No Values in Getter and Setter Methods with Button in Template模板中带有按钮的 Getter 和 Setter 方法中没有值
【发布时间】:2014-03-14 08:43:30
【问题描述】:

我有一个模板 AdvanceTemplate.xhtml。还有一个 footer.xhtml。这个页脚有提交按钮。我有一个带有 inputText 的 store.xhtml 页面。 但是当我点击提交按钮时,我的视图类中的数据不会从输入字段中保存。 如果我将按钮放在放置 im inputText 的同一个 xhtml (store.xhtml) 页面中,我会得到数据。

当我在模板中有按钮时,我必须如何编写它才能从 inputText 字段中获取数据?

AdvanceTemplate.xhtml

 <!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">
    <head>
      <title><ui:insert name="title">Example</ui:insert></title>
    </head>

    <body>

    <div id="header">
        <ui:insert name="header">
            <ui:include src="/META-INF/templates/header.xhtml"/>
        </ui:insert>
    </div>

    <div id="information">
        <ui:insert name="information">
         <ui:include src="/META-INF/templates/information.xhtml"/>
        </ui:insert>
    </div>

    <div id="content">
        <ui:insert name="content">
        </ui:insert>
    </div>

    <div id="footer">
        <ui:insert name="footer">
            <ui:include src="/META-INF/templates/advanceFooter.xhtml"/>
        </ui:insert>
    </div>

    </body>
    </html>

advanceFooter.xhtml

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!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:h="http://java.sun.com/jsf/html"
          xmlns:f="http://java.sun.com/jsf/core">

    <h:head></h:head> 
    <body>
    <h:form>

        <h:panelGrid columns="2">   
                <h:commandButton id="submit" action="#{pageServiceBean.submit}" value="#{msgs.save}" />     
            <h:commandButton id="cancel" action="#{pageServiceBean.cancel}" value="#{msgs.cancel}" immediate="true"/>
        </h:panelGrid>

        <div style="background-color:navy;width:100%;color:white"></div>
    </h:form>

</body>
</html>

store.xhtml

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

    <ui:composition template="META-INF/templates/AdvanceTemplate.xhtml">
        <ui:define name="content">
         <h:form>    
             <h:panelGrid columns="2"> 




                      <h:outputText value="#{msgs.hostname}"></h:outputText>
                      <h:inputText id="idName" value="#{pageServiceBean.newData.name}"                      
                      </h:inputText>
                      <h:outputText value="#{msgs.type}"></h:outputText>
                      <h:inputText value="#{pageServiceBean.newData.type}"></h:inputText>                                



               </h:panelGrid> 

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

【问题讨论】:

  • 为什么需要页脚中的按钮?按钮的正确位置是在您要提交的表单中。
  • 我想用 java 脚本创建动态输入字段。并且认为将静态元素(如按钮)放在外面(如页脚和页眉)是个好主意。

标签: java html jsf jsf-2


【解决方案1】:

&lt;h:inputText&gt; 标签之间使用&lt;f:ajax/&gt;

<h:inputText id="idName" value="#{pageServiceBean.newData.name}">
    <f:ajax/>      
</h:inputText>

注意:我没有测试你的代码示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多