为什么清除缓存不适用于所有浏览器?

时间:2015-06-10 10:45:43

标签: jsf caching browser

我有一个JSF应用程序,你输入2个数字(index.xhtml),当你点击"添加"按钮这些数字被添加,结果显示在新页面(resultado.xhtml)。

我想要做的是当你插入2个号码时,点击"添加"按钮并转到resultado.xhtml,如果您返回index.xhtml,在浏览器中单击“返回”按钮,则inputText字段必须为空且不包含先前插入的值。问题是它适用于Firefox,但不适用于Chromium和Konqueror浏览器。当我使用这个浏览器并按下后退按钮时,插入的数字在inputText中。怎么能让它适用于所有浏览器?

我已按照此链接实现了一个servlet过滤器来清除缓存:Prevent user from seeing previously visited secured page after logout

的index.xhtml

    <h:head>
        <h:outputStylesheet library="css" name="styles.css"  />
    </h:head>
    <h:body>
        <h:form>   

            <!--Number 1 -->
            <h:inputText id="num1" label="num1" required="true" size="5" maxlength="5" 
                    styleClass="#{component.valid ? '' : 'validation-failed'}"
                    value="#{sumaManagedBean.number1}"
                    requiredMessage="You must enter a value"/>
            </h:inputText>
            <h:message for="num1" />


            <!--Number 2-->
            <h:inputText id="num2" label="num2" required="true" size="5" maxlength="5"
                     styleClass="#{component.valid ? '' : 'validation-failed'}"
                     value="#{sumaManagedBean.number2}"
                     requiredMessage="You must enter a value">         
            </h:inputText>
            <h:message for="num2" />

                <h:commandButton value="Add" action="#{sumaManagedBean.direccionPagina()}"/>
        </h:form>


</h:body>  

resultado.xhtml

<h:head>
    <h:outputStylesheet library="css" name="styles.css"  />
</h:head>
<h:body>

<h:outputText value="Add correct" rendered="#{sumaManagedBean.insertCorrecto}" styleClass="message"/> 
    Result:
    <h:outputText value="#{sumaManagedBean.calcularResultado()}" /> 
</h:body>

sumaManagedBean.java。我的ManagedBean范围是RequestScope。

package controllers;

//Imports

    @ManagedBean
    @RequestScoped
    public class SumaManagedBean implements Serializable
    {

        boolean m_binsertCorrecto; 

        int number1;
        int number2;

        public SumaManagedBean() {      

        }


        //Getters and Setters
        public int getNumber1() {
            return number1;
        }

        public void setNumber1(int number1) {
            this.number1 = number1;
        }

        public int getNumber2() {
            return number2;
        }

        public void setNumber2(int number2) {
            this.number2 = number2;
        }


        //
        public boolean isInsertCorrecto() 
        {
            return m_binsertCorrecto;
        }

        public void setInsertCorrecto(boolean bInsertCorrecto) 
        {
            this.m_binsertCorrecto = bInsertCorrecto;
        } 



        public int calcularResultado()
        {
            int resultado;

            resultado = number1 + number2;

            return resultado;
        }

        public String direccionPagina()
        {
            String direccion = "/resultado";

            setInsertCorrecto(true);

            return direccion;
        }

    }

NoCacheFilter.java

package filters;

import java.io.IOException;
import javax.faces.application.ResourceHandler;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebFilter("/*") //Apply a filter for all URLs
public class NoCacheFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        if (!request.getRequestURI().startsWith(request.getContextPath() + ResourceHandler.RESOURCE_IDENTIFIER)) { // Skip JSF resources (CSS/JS/Images/etc)
            response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
            response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
            response.setDateHeader("Expires", 0); // Proxies.
        }

        chain.doFilter(req, res);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }
    @Override
    public void destroy() {
    }
}

0 个答案:

没有答案