【问题标题】:Set and Get sseions using thymleaf使用 thymeleaf 设置和获取会话
【发布时间】:2016-05-13 14:06:19
【问题描述】:

我见过this,我正在尝试在页面中设置一个会话,并使用发布请求将其传递给一个方法并打印它。所以这里是 3 个步骤:

第 1 步:在首页设置会话:

@RequestMapping(method=GET, path="/ShowRequestedPlantsInPO")    //PS5
  public String ShowRequestedPlantsInPO(Model model,HttpSession session){
    session.setAttribute("mySessionAttribute", "someValue");
    return "dashboard/orders/ShowPOs";
}

第 2 步:在页面中获取会话

<form style='float:left; padding:5px; height:0px' th:object="${p}"
  th:method="post"
  th:action="@{/dashboard/testSession/th:text=${session.mySessionAttribute} }"
>                       
  <button class="btn btn-default btn-xs" type="submit">
    Test Session
  </button>
</form>

如您所见,我尝试使用 th:text=${session.mySessionAttribute} 访问我的会话

第三步:获取前一个形式的目标方法中session的值:

@RequestMapping(method=POST, path="/testSession/{email:.+}")
public String testSession(@PathVariable("email") String email) throws Exception {
  System.out.println("Invoice is sent to..................."+email);
  return "dashboard/orders/Invoice";
}

问题是它不打印会话值“SomeValue” 它唯一打印的是: th:text=${session.mySessionAttribute}

【问题讨论】:

  • @JB Nizet 你能帮我解决这个问题吗?
  • 这个语法th:action="@{/dashboard/testSession/th:text=${session.mySessionAttribute}是完全错误的。

标签: java spring spring-boot thymeleaf


【解决方案1】:

您稍微混淆了 Thymeleaf 语法​​。将表单标签th:action属性更改为:

<form style='float:left; padding:5px; height:0px' th:object="${p}" th:method="post"
 th:action="@{'/dashboard/testSession/'+${session.mySessionAttribute} }">
    <button class="btn btn-default btn-xs" type="submit">Test Session</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-04
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    相关资源
    最近更新 更多