2

私はこれらの簡単なページを持っています:

list.xhtml

<h:form id="form">  
    <h:dataTable value="#{testBean.model}" var="elem">
        <h:column>
            <f:facet name="header">code</f:facet>
            #{elem.code}
        </h:column>
        <h:column>
            <f:facet name="header">description</f:facet>
            #{elem.description}
        </h:column>
        <h:column>
            <f:facet name="header">action</f:facet>
            <h:commandButton action="#{testBean.edit(elem)}" value="edit"/>
        </h:column>
    </h:dataTable>
</h:form>

edit.xhtml

<h:form id="form">
    <h:panelGrid columns="2">
        <h:outputLabel value="code"/>
        <h:inputText value="#{testBean.selection.code}"/>

        <h:outputLabel value="description"/>
        <h:inputText value="#{testBean.selection.description}"/>
    </h:panelGrid>

    <h:commandButton action="#{testBean.update}" value="update"/>
</h:form>

そしてこの豆:

@ManagedBean
public class TestBean implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EJB
    private PersistenceService service;
    private Object selection;
    private List<UnitType> model;

    @PostConstruct
    public void init()
    {
        model = service.findAll(UnitType.class);
    }

    public String edit(Object object)
    {
        System.out.println(Tracer.current(object));
        setSelection(object);
        return "edit";
    }

    public String update()
    {
        System.out.println(Tracer.current(selection));
        return "list";
    }

    // getters and setters
}

そのため、テーブルがレンダリングされ、「編集」ボタンの1つをクリックすると、入力された入力を示す「edit.jsf」に移動しますが、「更新」ボタンをクリックすると、次のエラーが表示されます:

javax.el.PropertyNotFoundException: /test2/edit.xhtml @27,54 value="#{testBean.selection.code}": Target Unreachable, 'null' returned null

CRUD操作を管理するために@ViewScopedインターフェースを実装する方法を知っていることに注意してください。ただし、これはJSFライフサイクルをよりよく理解するために必要な概念の簡単な証明です。

だから私は "testBean" を @RequestScoped にしたい


更新しようとしていますがf:viewParam、まだ理解していません...

list.xhtml

<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>test list</title>
    </h:head>

    <h:body>
        <h:messages/>

        <h:form id="form">
            <h:dataTable value="#{testBean2.model}" rows="10" var="elem">
                <h:column>
                    <f:facet name="header">converterString</f:facet>
                    #{elem.converterString}
                </h:column>
                <h:column>
                    <f:facet name="header">first name</f:facet>
                    #{elem.firstName}
                </h:column>
                <h:column>
                    <f:facet name="header">last name</f:facet>
                    #{elem.lastName}
                </h:column>
                <h:column>
                    <f:facet name="header">action</f:facet>
                    <h:commandButton action="#{testBean2.edit}" value="edit">
                        <f:param name="entity" value="#{elem.converterString}"/>
                    </h:commandButton>
                    <h:commandButton action="#{testBean2.edit2}" value="edit2">
                        <f:param name="entity" value="#{elem.converterString}"/>
                    </h:commandButton>
                </h:column>
            </h:dataTable>
        </h:form>
    </h:body>
</html>

edit.xhtml

<?xml version="1.0" encoding="ISO-8859-1"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core">
    <f:metadata>
        <f:viewParam id="entityParam" name="entity" value="#{testBean2.selection}" converter="entityConverter" required="true"/>
    </f:metadata>

    <h:head>
        <title>test edit</title>
    </h:head>

    <h:body>
        <h:messages/>

        <h:form id="form">
            <h:panelGrid columns="2">
                <h:outputLabel value="selection"/>
                <h:outputText value="#{testBean2.selection.converterString}"/>

                <h:outputLabel value="firstName"/>
                <h:inputText value="#{testBean2.selection.firstName}"/>

                <h:outputLabel value="lastName"/>
                <h:inputText value="#{testBean2.selection.lastName}"/>
            </h:panelGrid>

            <h:commandButton action="#{testBean2.update}" value="update" ajax="false">
                <f:param name="entity" value="#{testBean2.selection.converterString}"/>
            </h:commandButton>
        </h:form>
    </h:body>
</html>

testBean2.java

@ManagedBean
public class TestBean2 implements Serializable
{
    private static final long serialVersionUID = 1L;

    @EJB
    private PersistenceService service;

    private Object selection;
    private List<Person> model;

    @PostConstruct
    public void init()
    {
        Tracer.out();
        model = service.queryAll(Person.class);
    }

    public String edit()
    {
        JsfUtils.addSuccessMessage("edited");
        return "edit";
    }

    public String edit2()
    {
        JsfUtils.addSuccessMessage("edited");
        return "edit?faces-redirect=true&amp;includeViewParams=true";
    }

    public void update()
    {
        Tracer.out(selection);
        JsfUtils.addSuccessMessage("updated");
    }

    // getters and setters
}

「編集」ボタンを押すとeditページに移動しますが、選択はnullでメッセージは表示されません。

「edit2」ボタンを押すとeditページに移動しますが、選択はnullで、必要なメッセージとURLが表示されますedit.jsf?entity=

私は何を間違っていますか?

4

3 に答える 3

1

私の理解では、2 番目のリクエストが testBean に来ると、選択オブジェクトは null になります。これをセッション Bean で実行すると、このエラーが発生しない場合があります。

于 2012-08-16T12:05:49.730 に答える
0

私の見方では、あなたが望むものを達成するためのきれいな方法はありません.

単純なリクエスト パラメータとして、リクエスト間で Id 属性を単純に渡すことをお勧めします。したがって、ポスト コンストラクターで、パラメーター値が設定されているかどうかを確認し、それに基づいて永続化レイヤーを調べます。

または、すでに持っているセットアップを使用して、以下をバインドします。

<h:inputText value="#{testBean.selection.code}"/>

コンポーネントをバッキング Bean に追加getValue()し、必要に応じてそれを呼び出します。いずれにせよ、まだだらしない。

他の方法を見つけた場合は、お知らせください。

于 2012-08-28T03:45:46.087 に答える