在JSF中使用输入值中的不同方法重用表单

在JSF中使用输入值中的不同方法重用表单,jsf,Jsf,我学习JSF2.3。我有两个非常相似的表单页面,一个用于创建项目,另一个用于编辑项目。我希望使用一个文件(form.xhtml)来避免重复我自己,因此这里有我的create-form.xhtml: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <

我学习JSF2.3。我有两个非常相似的表单页面,一个用于创建项目,另一个用于编辑项目。我希望使用一个文件(form.xhtml)来避免重复我自己,因此这里有我的create-form.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:jsf="http://xmlns.jcp.org/jsf"    
    xmlns:p="http://xmlns.jcp.org/jsf/passthrough"
    xmlns:f="http://xmlns.jcp.org/jsf/core"
    xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
    xmlns:c="http://xmlns.jcp.org/jsp/jstl/core">
    
<h:form id="add-form" prependId="false">
 
        <div ><h4>#{requestTools.createFormLabels()[0]}</h4></div>
        <p class="form-group">
            <h:inputTextarea id="todo-text" 
                             class="form-control" 
                             title="#{msg.todoText}" 
                             rows="3" 
                             p:placeholder="#{msg.todoText}" 
                             
                             value="#{newTodoItem.text}"                            
                             
                             required="true" 
                             requiredMessage="#{msg.messageTodoText}">
            </h:inputTextarea>
        </p>
        <p class="form-group">
            <h:outputLabel for="datetime" value="#{msg.deadlineLabel}"/>
            <input type="datetime-local" 
                   class="form-control" 
                   name="datetime" 
                   id="datetime"
                   jsf:id="datetime" 
                   
                   jsf:value="#{newTodoItem.userInputDate}"
                   
                   required="true" 
                   requiredMessage="#{msg.messageDeadline}">                        
                <f:convertDateTime for="datetime" timeStyle="full" 
                                   pattern="yyyy-MM-dd'T'HH:mm"
                                   type="both"/>
            </input>
        </p>   
        <p class="form-group">
            <h:outputLabel for="author" value="#{msg.authorLabel}"/>
            <h:selectOneMenu class="author"
                              id="author"
                              
                              value ="#{newTodoItem.author}"
                              
                              required="true" requiredMessage="Author is required">
                <f:selectItem itemValue="Ivo" itemLabel="Ivo"/>
                <f:selectItem itemValue="Olinka" itemLabel="Olinka"/>
            </h:selectOneMenu>
        </p>
        <hr/>
        <p class="form-group">
            <h:commandButton class="btn btn-sm btn-light" 
                            value = "#{requestTools.createFormLabels()[1]}" 
                            action = "#{todoItemBean.createNewTask(newTodoItem)}"/>
        </p>
    </h:form>
</ui:composition>

#{requestTools.createFormLabels()[0]}


编辑表单.xhtml
应该几乎相同。不同之处在于,输入值应该是例如
{todoItemBean.newItem.text}
,而不是
{newTodoItem.text}

我试了一些
。。。条件(
requestTools#getSegments(i)
返回uri的第i段),但这不起作用,因为差异在
标记内。要使用
,您可能需要创建标记文件或(正如您已经提到的)复合组件(cc)。但我建议将cc放在表单中,而不是将表单放在cc中。这可能会有帮助:@Selaron谢谢你的提示,我猜cc对于两种表单来说都有点复杂,所以我会看看标记文件,我以前没有想到过它。