不使用spring web flow ajax更新会话属性

时间:2013-11-27 06:56:25

标签: spring-webflow thymeleaf

我使用带有百叶窗式布局的弹簧网流。我的模型属性正在成功更新,但会话属性部门未使用/Spring.addDecoration更新(新Spring.AjaxEventDecoration /

这是我的百万美元布局,我使用spring.addDecoration ....

<div id="breadcrumb" th:fragment="breadcrumb"> <label th:text="${session.department}"></label></div>    
<!--  <div id="breadcrumb" th:fragment="breadcrumb"> <label th:text="${session.department}"></label></div> --> 

<!-- <div id="data" style="font-weight: bold;font-size:20px" th:fragment="data">
    Server Time-stamp : <label th:text="${date}"></label>
</div> -->



<form id="triggerform" method="post" action="" style="border: red solid 1px;">
<input type="text" value="20" name="departmentId"/>
    <input type="submit" id="doUpdate" name="_eventId_updateData"
        value="Refresh" />
</form>


<script type="text/javascript">
    Spring.addDecoration(new Spring.AjaxEventDecoration({
        formId : 'triggerform',
        elementId : 'doUpdate',
        event : 'onclick',
        params : {
            fragments : "[//div[@id='data']],[//div[@id='breadcrumb']]"
        }
    }));

</script>

这是我的控制器: -

package com.sds.main;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class WelcomeController
{

  @RequestMapping(value = "/")
  public void root(HttpSession session)
  {
    session.setAttribute("breadcrumb","pta ni kyn ni chalda");
  }

  @RequestMapping(value = "/home")
  public String intro(@RequestParam(required = false) Integer departmentId, Model model, HttpServletRequest request)
  {
    setBasicParams(departmentId,model,request);
    return "home";
  }

  public void setBasicParams(Integer departmentId, Model model, HttpServletRequest request)
  {
    HttpSession session = request.getSession();
    System.out.println("--Dept id--" + departmentId);
    Integer department = 1000;
    if (departmentId != null)
    {
      System.out.println("--Dept insinde--" + departmentId);
      department = departmentId;
    }
    // model.addAttribute("department",department);
    session.setAttribute("department",department);
    model.addAttribute("date",new Date().toString());
  }

  @RequestMapping(value = "/ajax")
  public String ajax(@RequestParam(required = false) Integer departmentId, Model model, HttpServletRequest request)
  {
    setBasicParams(departmentId,model,request);
    model.addAttribute("test",Math.random());
    HttpSession session = request.getSession();
    session.setAttribute("breadcrumb","pta ni kyn ni chalda233");
    model.addAttribute("check",UUID.randomUUID().toString());
    return "ajax";

  }

}

这是我的ajax页面,由/ ajax url。

返回
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"  xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
     lang="en"  layout:decorator="home">

<head>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- This <head> section is only used for static prototyping purposes. At runtime, -->
<!-- Tiles will only use the body fragment defined with tiles:fragment="content",  -->
<!-- as specified at the corresponding views.xml file.                             -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

<title>Spring + Webflow + Thymeleaf + Ajax Fragment Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />



<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->
<!-- Some styles and scripts are served from spring-js-resources-{ver}.jar at -->
<!-- runtime. Therefore not available for static prototyping. See the         -->
<!-- layouts/standard.html template file for detail.                          -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  -->

</head>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- START of the content to be included in the execution result.  -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- Only the markup inside this <body> would be required in this  -->
<!-- template if no static prototyping was intended.               -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<body>
<div layout:fragment="content" >
kfdjdsklfjlkdsfjsdlkfj
<!-- <div id="breadcrumb" th:fragment="breadcrumb"> <label th:text="${session.department}"></label></div> -->
<!-- <div id="breadcrumb" th:fragment="breadcrumb"> <label th:text="${session.department}"></label></div> -->
<div id="data">
    <div th:text="${test}">hey guys how r u?</div>
    <div th:text="${check}">hey guys how r u?</div></div>
    </div>
</body>

<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- END of the content to be included in the execution result -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->

</html>

现在当我点击刷新按钮时,ajax页面的数据正在更新,但布局页面(home.html)上的会话属性部门没有得到更新,但如果我将此会话属性放在ajax页面中,那么它的获取更新不会知道为什么。这是发生在bcoz我从/ ajax url返回ajax页面但会话是全局的,应该反映在所有页面。

请尽快回复。 提前完成。

1 个答案:

答案 0 :(得分:0)

我得到了问题的解决方案: -

我们不需要在这样的参数中给出任何片段: -

<script type="text/javascript">
    Spring.addDecoration(new Spring.AjaxEventDecoration({
        formId : 'triggerform',
        elementId : 'doUpdate',
        event : 'onclick',
        params : {
            fragments : ""
        }
    }));

</script>

或者可以没有像这样的parms属性: -

<script type="text/javascript">
    Spring.addDecoration(new Spring.AjaxEventDecoration({
        formId : 'triggerform',
        elementId : 'doUpdate',
        event : 'onclick'
    }));

</script>

我做了很多研究,但没有得到任何解决方案。所以,我随机尝试了这一点。