chardex
chardex

Reputation: 323

Jsf form action

I have simple JSF form:

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

Everything works, but when I'm trying to use this page with query string parameters (template.jsf?Id=5) and then sumbit command button -- page is redirected to template.jsf (witout query string parameter. And it is clear -- form action attribute always ="template.jsf", even query string parameters are passed). So I can't call submit method for TemplateBean with specified query string parameter.

Upvotes: 1

Views: 2117

Answers (1)

BalusC
BalusC

Reputation: 1108537

Add a property id to the template bean:

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

Instruct JSF in the faces-config to set it with #{param.id} during bean's creation:

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

Retain the property in subsequent request by passing it through as hidden input field in the same form:

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

Then you can access it the usual way in action method:

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

Upvotes: 2

Related Questions