1

戻るボタンに問題があり、リクエスト スコープ Bean の JSF の動的ドロップダウンにデータが保持されません。

ドロップダウン1で選択されたものに基づいてドロップダウン2が動的である2つのドロップダウンを持つフォームがあります。以下は、これらのドロップダウンの私のコードです。

<h:selectOneMenu id="group" label="group" value="#{queryBacking.groupInternalId}">
    <f:ajax event="valueChange" render="membership" />
    <f:selectItems value="#{supportBean.groupInstitutions}" var="group" itemValue="#{group.institutionInternalId}" itemLabel="#{group.institutionName}" />
</h:selectOneMenu>

<h:selectOneMenu id="membership" label="Membership" value="#{queryBacking.institutionInternalId}">
    <f:selectItem itemLabel="Select One" itemValue="0" />
    <f:selectItems value="#{queryBacking.groupMembershipInstitutions}" var="institution" itemValue="#{institution.institutionInternalId}" itemLabel="#{institution.institutionShortName}" />
</h:selectOneMenu>

フォームを送信してから戻るボタンをクリックすると、dropdown2 に値が含まれないことを除いて、私のコードはうまく機能します。この問題を解決するにはどうすればよいですか?

4

2 に答える 2

5

ブラウザの戻るボタンのことですか?

ブラウザはおそらくブラウザのキャッシュからページをロードします。したがって、フィルターを使用してキャッシュを無効にする必要があります。

public class NoCacheFilter implements Filter {
    private FilterConfig config;

    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httpReq = (HttpServletRequest) request;
        HttpServletResponse httpRes = (HttpServletResponse) response;

        if (!httpReq.getRequestURI().startsWith(
                httpReq.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { 

            httpRes.setHeader("Cache-Control",
                    "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            httpRes.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            httpRes.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        config = null;
    }

    @Override
    public void init(FilterConfig config) throws ServletException {
        this.config = config;
    }
}

そして、これをweb.xmlに追加します。

<filter>
  <filter-name>NoCacheFilter</filter-name>
  <filter-class>yourpackage.NoCacheFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>NoCacheFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

フィルタリングするページを指定できます<url-pattern> </url-pattern>

于 2012-05-09T20:26:44.727 に答える
0

Bean コンストラクターでページの値を初期化できます。

TestBean クラス

@ManagedBean
@ViewScope
public class TestBean {
    private String name;

    public TestBean() {
        //initialize the name attribute

        //recover the value from session
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
        name = session.getAttribute("name");
        if (name == null) {
            name = "Luiggi";
        }
    }

    public String someAction() {
        //save the value in session
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
        session.setAttribute("name", name);
        return "Test";
    }

    //getters and setters...
}

Test.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">
    <h:body>
        <h:outputText value="Hello " />
        <h:outputText value="#{testBean.name}" />
        <h:form>
            <h:outputText value="Write your name: " />
            <h:inputText value="#{testBean.name}" />
            <br />
            <h:commandButton value="Change name" action="#{testBean.someAction}" />
        </h:form>
    </h:body>
</ui:composition>

Text.xhtml に移動する前にセッション属性を削除する例を追加する

SomeBean クラス

@ManagedBean
@RequestScope
public class SomeBean {
    public SomeBean() {
    }
    public String gotoTest() {
        //removes an item from session
        HttpSession session = (HttpSession)FacesContext.getCurrentInstance()
            .getExternalContext().getSession(false);
        session.removeAttribute("name");
        return "Test";
    }
}

SomeBean.xhtml

<ui:composition
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html">
    <h:body>
        <h:form>
            <!-- Every time you navigate through here, your "name" 
                 session attribute will be removed. When you hit the back
                 button to get Test.xhtml you will see the "name"
                 session attribute that is actually stored. -->
            <h:commandButton value="Go to Test" action="#{someBean.gotoTest}" />
        </h:form>
    </h:body>
</ui:composition>
于 2012-05-09T17:08:45.190 に答える