在百里香中单击按钮时增加会话属性并调用URL

时间:2018-07-02 06:59:23

标签: spring-boot thymeleaf

我在春季启动时正在使用thymeleaf,并在索引页上读取此类会话属性

  <p th:text="${session.counter}" th:unless="${session == null}">[...]</p>

计数器来自以下功能

 @RequestMapping({"/"})
String index(HttpSessbelow request ion session) {
    session.setAttribute("counter", "0");
    return "index";
}

每当有人单击页面上的按钮时,我们应该能够增加计数器并在应用程序中调用url,我们如何实现

 <button onclick="/activate}">...</button> 


@RequestMapping({"/"})
String activate(HttpSession session) {
    if(session.getAttribute(counter) == 1){

    activate();
    return "thanksPage" ; 
    }

}

1 个答案:

答案 0 :(得分:0)

这就是我的结构方式。

@RequestMapping("/")
public String start(HttpSession session) {
    session.setAttribute("counter", 0);
    return "redirect:/current";
}

@RequestMapping("/current")
public String current() {
    int counter = (Integer) session.getAttribute("counter);

    if (counter == 1) {
        activate();
        return "thanksPage"; 
    } else {
        return "index";
    }
}

@RequestMapping("/increment")
public string increment(HttpSession session) {
    session.setAttribute("counter", ((Integer) session.getAttribute("counter)) + 1);
    return "redirect:/current";
}

该按钮应转到/increment

<button onclick="/increment">...</button>