Frage

I m using spring web flow with thymeleaf layouts. My model attributes are updating successfully but session attributes department not updating with /Spring.addDecoration(new Spring.AjaxEventDecoration/

This is my thymeleaf layout where i m using 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>

and this is my controller :-

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

  }

}

and this is my ajax page which is returned by /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>

now when i click on refresh button then data of ajax page is getting update but session attribute department on layout page (home.html) not getting update but if i put this session attribute in ajax page then its getting update don't know why. is this happening bcoz i return ajax page from /ajax url but session is global and should be reflect at all pages.

please reply soon. Thanx in advance.

War es hilfreich?

Lösung

I got the solution of my problem:-

We don't need to give any fragments in params like this:-

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

</script>

or can do without parms attribute like this:-

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

</script>

I did a lot research but didn't get any solution. so, i tried this randomly and did this.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top