Refreshing a wicket Panel in browser refresh

2019-05-26 04:31发布

问题:

Im working on a pay-role system, and once the user refreshes the browser i need to refresh the statistics available in that page (the stats should be taken from the DB and display). But right now it doesn't work properly, cos in page refresh the java code doesn't get invoked but loads the cached page with the previous data. I tried fixing it adding the below code but it didn't work as well.

@Override
protected void setHeaders(WebResponse response) { 
    response.setHeader("Cache-Control",
            "no-cache, max-age=0,must-revalidate, no-store");
}

Anyone knows the fix for this? Thanks!
This was the initial code

ManageCredits.html

 <wicket:head>
  <wicket:link>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
  </wicket:link>
</wicket:head>
<wicket:panel xmlns:wicket="http://wicket.apache.org/">
        <div class="offercount-container round-border">
        <div class="manage-credits-lbl"><span>Manage Credits</span></div>

                <div>
                <ul>
                     <li>Balance amout:<span wicket:id="balanceAmtLbl" >0</span></li>
                     <li>Number if transactions<span wicket:id="transactionLbl" >0</span></li>
                   </ul>
                </div>
        </div>
</wicket:panel>


ManageCredits.java

    import com.payrole.service.Offer;
import com.payrole.service.OfferService;
import com.payrole.service.ServiceLocator;
import com.payrole.wicket.PayroleLabel;
import java.text.Format;`enter code here`
import org.apache.wicket.markup.html.panel.Panel;

public class OfferFanSummaryPanel extends Panel{      
        private long balance;
        private long transactionCount;

        public OfferFanSummaryPanel(String id){
            super(id);
        }
        public OfferFanSummaryPanel(String id, Offer offer) {
                super(id);
                balance = (client.getBalance()==null) ? 0 : client.getBalance();
                transactionCount = (client.getTransactionCount()==null) ? 0 : client.getTransactionCount();
                initTransactionSummary();              
        }
        private void initTransactionSummary(){
                PayroleLabel balanceAmtLbl = new PayroleLabel("balanceAmtLbl", String.valueOf(balanceAmt), PayroleLabel.NUMBER);
                add(balanceAmtLbl);
                PayroleLabel transactionLbl = new PayroleLabel("transactionLbl", String.valueOf(transaction), PayroleLabel.NUMBER);
                add(transactionLbl);                
        }     
}

回答1:

You have to use dynamic models. E.g., if you use something like

add(new Label("name", Model.of(person.getName()));

you will always have the same string displayed: you provide the label with the result of person.getName(). Instead you have to use a model implementation that calculates the value for each render request, for example PropertyModel or AbstractReadOnlyModel with getObject() overridden.

Wicket creates the components on your page only once and with each request asks the components to render themselves. I guess you code does not get called because it is in the constructor or onInitialze() of the component (page, panel).

If this is not clear enough, please post some code on what you are doing.

Edit: As guessed, you are using static Models (models, that have a fixed value, as opposed to models that calculate their value on each render request). I don 't see in your code where emailFanCount and openCount come from, but assuming that these get calculated somehow. Try using a PropertyModel on either one of you Objects (client,..) or, add a public String getXXX() {} to you panel and construct your label similar to this:

Label label = new Label("someID", new PropertyModel(this, "XXX"));

where this is the Panel.

Have a look in in the Wicket Wiki for an explanation



回答2:

In your HTML, Add these lines in <head> tag :

  <meta http-equiv="cache-control" content="no-cache"/>
  <meta http-equiv="pragma" content="no-cache" />
  <meta http-equiv="expires" content="-1" />

This will force browser to retrieve details from server. You also may implement url rewriting to get data from server every time you refresh the page. Hope this helps.



回答3:

The use of meta tags don't ensure you that an intermediate proxy won't cache the page. Try to use the "Cache-Control" header in combination with "Expires" that is a HTTP 1.0 header and all the browsers and proxies support.

From Wikipedia:

This header field (Cache-Control) is part of HTTP version 1.1, and is ignored by some caches and browsers. It may be simulated by setting the Expires HTTP version 1.0 header field value to a time earlier than the response time.

Here you have a good tutorial about HTTP caching.