Domanda

I've got a SpringController getting a RequestParameter named 'start' for search/hibernate pagination purposes. If the parameter is null, just start from zero and render a JSP with a JavaScript var 'start = {start + 10}', so if the user clicks on the "next 10" button, the controller gets a 'start = 10'. Then, the controller should render a JSP with 'start = {start + 10} = 20, but it remains 10 no matter how many times I go through the controller. I've made the addition both in server and client side. Same thing happens: Nothing. Here's the controller method:

@RequestMapping(value = "/customers", method = RequestMethod.GET)
public String customersView(@RequestParam(value = "start", required = false) Integer start, ModelMap model) {
    if (start == null || start <= 0) start = 0;

    List<Customer> customers = customerService.findAllCustomers(start, pagesize);      
    CustomersForm customersForm = new CustomersForm();
    customersForm.setCustomers(customers);
    start += 10; // I can see a "20" here, actually.
    model.clear();
    model.addAttribute("customersform", customersForm);
    model.addAttribute("start", start); //This should render a "20" on client side on the first call, but it doesn't.
    return "customers";
}

This is the relevant part of the JSP:

<script type="text/javascript">
jq(document).ready(function() {
    jq("#next_20").click(function() {
        var st = "${start}";  //This gets set OK only first time, i.e., a "10" is rendered.
        jq.get("customers",
        {
            start: st
        });
    });
});

OK now, I've also added these lines to my servlet-context, because I suspect it is related to some caching some part (browser, Tomcat) is doing...:

<!-- Disable web cache for GET requests -->
<mvc:interceptors>
    <bean id="webContentInterceptor"
        class="org.springframework.web.servlet.mvc.WebContentInterceptor">
        <property name="cacheSeconds" value="0" />
        <property name="useExpiresHeader" value="true" />
        <property name="useCacheControlHeader" value="true" />
        <property name="useCacheControlNoStore" value="true" />
    </bean>
</mvc:interceptors>

And that's it. When I click on the "next_20" button, my controller is called, a "start" parameter is passed (10) and should be replaced in the model with a "20", but the rendered page has still a "10" there.

What am I doing wrong?

Cheers

È stato utile?

Soluzione

Your call to jq.get is an ajax call which doesn't refresh the page so the ${start} jstl expression won't get evaluated again. You can store st outside the ajax call, return start from the ajax call instead of "customers", and update st when the call returns:

var st = "${start}";  //This gets set OK only first time, i.e., a "10" is rendered.

jq("#next_20").click(function() {

    jq.get("customers",
        {
            start: st
        },
        function(newStartValue) {
            st = newStartValue;
        }
    );
});
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top