带后退按钮的JSF问题

时间:2012-05-09 16:26:04

标签: java jsf

我遇到了后退按钮的问题,而不是将数据保存在请求范围的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不包含任何值。如何解决这个问题?

2 个答案:

答案 0 :(得分: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>

中指定要过滤的网页

答案 1 :(得分: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>