不使用表达式在 JSF2 中发送带有 <ui:include src= "page.xhtml" > 的参数



我试图通过一些参数在后端与UI包括,但不是它不传递参数时,我使用表达式如value =" #{value}"。但是我能够发送参数与正常字符串,如value="value"。

<ui:include src="index_1.xhtml">
      <ui:param name="action1" value="#{o.columnId}" />
      <ui:param name="action2" value="#{o.columnType}" />
</ui:include> <br/>
但是我可以用 传递参数
<ui:include src="index_1.xhtml">
      <ui:param name="action1" value="Value1" />
      <ui:param name="action2" value="Value2" />
</ui:include> <br/>

Index.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>TODO supply a title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </h:head>
    <h:body>
        <h:form>
            <ui:repeat value="#{myBean.models}" var="o">
                <ui:include src="index_1.xhtml">
                    <ui:param name="action1" value="#{o.columnId}" />
                    <ui:param name="action2" value="#{o.columnType}" />
                </ui:include> <br/>
                <ui:include src="index_2.xhtml"> 
                    <ui:param name="action1" value="#{o.columnId}" />
                    <ui:param name="action2" value="#{o.columnType}" />
                </ui:include><br/>
                <ui:include src="index_3.xhtml">
                    <ui:param name="action1" value="#{o.columnId}" />
                    <ui:param name="action2" value="#{o.columnType}" />
                </ui:include><br/>
            </ui:repeat>
            <p:commandButton value="Submit" action="#{myBean.submitForm}" />
        </h:form>
    </h:body>
</html>

index_1.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>TODO supply a title</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </h:head>
    <h:body>
        <f:event listener="#{myBean.ajaxEventForObjectNo(action1, action2)}" type="preRenderView" />
        <h:outputLabel>Single Line Text</h:outputLabel>
        <p:inputText value="#{myBean.models[myBean.objectNo].columnId}" />
    </h:body>    
</html>

谢谢。

问题是,就像@XtremeBiker已经说过的,ui:includeui:repeat之前执行。更准确地说,ui:include视图构建时间执行,而ui:repeat视图渲染时间评估。

ui:repeatvar属性定义的变量o因此尚未初始化。因此计算结果为null

一个可能的替代方案是使用c:forEachc:forEach是一个JSTL标记处理程序,在视图构建时执行。然而,这就是@BalusC要说的:

<c:forEach>替换<ui:repeat>应该可以解决这个问题。然而,它可能有不受欢迎的副作用。很难事先判断,因为你们的具体功能需求还不完全清楚。

不太确定他说的是什么副作用。然而,我还没有问题,用<c:forEach>替换<ui:repeat>

这里也有一个关于这个主题的很好的阅读:JSF2 Facelets中的JSTL…有道理吗?

最新更新