开发者

Using the same page for creating and editing in JSF 1.2 (Facelets)

Suppose I've got a XHTML page that contains a form for creating a use开发者_如何转开发r. There are all necessary fields and a "Save" button. Now in other place of my app there is an option to edit user informations. I would like to reuse the same page for performing this operation. But how? How can I pass user information to that page and how can I change "Save" action to navigate to page that I want? I'm looking for a clean solution (best practice?), not any dirty solution.


You could put the input fields in a Facelet tag file. Basic kickoff example:

/tags/user.xhtml

<ui:composition 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:outputLabel for="name" value="Name" />
    <h:inputText id="name" value="#{user.name}" />
    <h:message for="name" />
    <br />
    <h:outputLabel for="email" value="Email" />
    <h:inputText id="email" value="#{user.email}" />
    <h:message for="email" />
    <br />
    <h:outputLabel for="city" value="City" />
    <h:inputText id="city" value="#{user.city}" />
    <h:message for="city" />
</ui:composition>

Define it in /META-INF/user.taglib.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
    "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
    "http://java.sun.com/dtd/facelet-taglib_1_0.dtd">

<facelet-taglib>
    <namespace>http://example.com/jsf/facelets</namespace>
    <tag>
        <tag-name>user</tag-name>
        <source>tags/user.xhtml</source>
    </tag>
</facelet-taglib>

which you register in /WEB-INF/web.xml as follows

<context-param>
    <param-name>facelets.LIBRARIES</param-name>
    <param-value>/META-INF/user.taglib.xml</param-value>
</context-param>

(note, when you have multiple, use semicolon ; to separate them)

Finally just declare it in your main page templates.

<ui:composition 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:my="http://example.com/jsf/facelets"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:form>
        <my:user user="#{register.user}" />
        <h:commandButton value="Create" action="#{register.create}" />
    </h:form>
</ui:composition>

and

<ui:composition 
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:my="http://example.com/jsf/facelets"
    xmlns:ui="http://java.sun.com/jsf/facelets"
>
    <h:form>
        <my:user user="#{editor.user}" />
        <h:commandButton value="Save" action="#{editor.save}" />
    </h:form>
</ui:composition>

The user="#{whatever}" attribute of <my:user> becomes the #{user} in the tag file (i.e. it's available by the attribute name as key).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜