【问题标题】:Spring MVC - Model Attribute parameter not refreshing on GETSpring MVC - 模型属性参数在 GET 上不刷新
【发布时间】:2013-03-17 16:55:07
【问题描述】:

我有一个 SpringController 获取一个名为“start”的 RequestParameter,用于搜索/休眠分页。如果参数为空,只需从零开始并使用 JavaScript var 'start = {start + 10}' 渲染一个 JSP,因此如果用户单击“下一个 10”按钮,控制器会得到一个 'start = 10' .然后,控制器应该渲染一个带有'start = {start + 10} = 20的JSP,但无论我通过控制器多少次,它仍然是10。 我在服务器端和客户端都做了添加。同样的事情发生:什么都没有。 这是控制器方法:

@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";
}

这是 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
        });
    });
});

现在好,我也将这些行添加到我的 servlet-context 中,因为我怀疑它与某些部分(浏览器、Tomcat)正在执行的某些缓存有关...:

<!-- 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>

就是这样。当我点击“next_20”按钮时,我的控制器被调用,一个“start”参数被传递(10)并且应该在模型中被替换为“20”,但是渲染的页面仍然有一个“10”。

我做错了什么?

干杯

【问题讨论】:

    标签: javascript jsp caching spring-mvc modelattribute


    【解决方案1】:

    您对 jq.get 的调用是一个 ajax 调用,它不会刷新页面,因此 ${start} jstl 表达式不会再次被评估。您可以将st 存储在ajax 调用之外,从ajax 调用返回start 而不是“customers”,并在调用返回时更新st

    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;
            }
        );
    });
    

    【讨论】:

    • 谢谢,朋友。我是 jQuery 和 AJAX 的新手,我没有注意到即使获取方法请求也不会刷新页面......所以这根本不符合我的要求,因为我需要刷新“客户”对象列表。我最终使用 PO JavaScript 函数调用做了类似onlick="location.href='customers?start=..." 的事情。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2011-11-25
    • 2013-10-09
    • 1970-01-01
    相关资源
    最近更新 更多