Jsf形成行动

时间:2010-09-23 13:39:01

标签: jsf myfaces tomahawk

我有简单的JSF表单:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:t="http://myfaces.apache.org/tomahawk" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
<ui:composition template="layout.jsp">
    <ui:define name="title">Редактирование шаблона</ui:define>
    <ui:define name="content">
        <t:div rendered="#{template.hasErrors}">
            #{template.errorText}
        </t:div>
        <t:div rendered="#{template.hasMessage}">
            #{template.messageText}
            <p>
                <a href="/templates.jsf">Все шаблоны</a>
            </p>
        </t:div>
        <t:div rendered="#{template.canEdit}">
            <h:form>
                Name: <h:inputText value="#{template.name}"/> <br/>
                Content Type: <h:inputText value="#{template.contentType}"/> <br/>
                Content: <h:inputTextarea value="#{template.content}"/> <br/>
                Description: <h:inputTextarea value="#{template.description}"/> <br/>
                <h:commandButton value="Сохранить" action="#{template.submit}">
                </h:commandButton>
            </h:form>
        </t:div>
    </ui:define>
</ui:composition>
</html>

一切正常,但是当我尝试将此页面与查询字符串参数(template.jsf?Id = 5)一起使用,然后使用sumbit命令按钮时,页面将重定向到template.jsf(无法查询字符串参数。很明显 - 表单操作属性always =“template.jsf”,甚至传递查询字符串参数)。 所以我不能用指定的查询字符串参数调用TemplateBean的submit方法。

1 个答案:

答案 0 :(得分:2)

将属性id添加到模板bean:

public class Template {
    private Long id; // +getter +setter
}

faces-config中指示JSF在bean创建过程中使用#{param.id}进行设置:

<managed-bean>
    <managed-bean-name>template</managed-bean-name>
    <managed-bean-class>com.example.Template</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>id</property-name>
        <value>#{param.id}</value>
    </managed-property>
</managed-bean>

通过以相同形式传递隐藏输入字段来保留后续请求中的属性:

<h:inputHidden value="#{template.id}" />

然后你可以按照通常的方式访问它:

public String submit() {
    System.out.println("id: " + this.id);
    return null;
}
相关问题