1

我在使用后退按钮时遇到问题,没有将数据保存在请求范围 bean 上的 JSF 中的动态下拉列表中。

我有一个带有 2 个下拉列表的表单,其中 dropdown2 是基于在 dropdown1 中选择的动态的。下面是我的这些下拉列表的代码。

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

测试.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 回答