无法将从控制器传递到 thymeleaf html 的模型属性传递回另一个控制器 [英] Not able to pass model Attribute passed from controller to thymeleaf html back to another controller

查看:51
本文介绍了无法将从控制器传递到 thymeleaf html 的模型属性传递回另一个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将modelAttributeauthRequest"从控制器传递到dashboard.html".从dashboard.html"我想在单击查看卡片"按钮时调用另一个服务.还想将modelAttributeauthRequest"传递回视图卡控制器,以便我将在viewCards.html"中获得modelAttributeauthRequest".我尝试给 th:href 和 form-action 但无法在视图卡控制器中获得authRequest"的值,因此在viewCards.html"中.如何将modelAttributeauthRequest"传递给viewCards控制器?尝试传递 modelAttribute 如下

I have passed a modelAttribute "authRequest" from controller to "dashboard.html". From "dashboard.html" I want to call another service on clicking "view Cards" button. Also want to pass modelAttribute "authRequest" back to view cards controller, so that I will get the modelAttribute "authRequest" in "viewCards.html". I tried giving th:href and form-action but not able to get value of "authRequest" in view cards controller and hence in "viewCards.html". How to pass the modelAttribute "authRequest" to viewCards controller? Tried to pass modelAttribute as below

<form action="#" th:action="@{/viewCards}" th:object="${authRequest}" method="post">
    <input type="submit" value="View cards" /></form>

也试过href

<a th:href="@{/viewCards}" th:object="${authRequest}">view Cards</a>

dashboard.html - 在这里我可以获得 ${authRequest.userName} 的价值"

dashboard.html -Here I am able to get value for ${authRequest.userName}"

<form action="#" th:action="@{/viewCards}" th:object="${authRequest}" method="post">
    <input type="submit" value="View cards" />
</form>

 <p th:text="'User Name:   ' + ${authRequest.userName}" />

viewCards.html - 无法获得 ${authRequest.userName} 的价值

viewCards.html -Not able to get value for ${authRequest.userName}

<script type="text/javascript" th:src="@{/webjars/jquery/1.9.1/jquery.min.js/}"></script>
 <script th:inline="javascript">
    var user= [[${authRequest.userName}]];
 </script>
  <script type="text/javascript"  th:src="@{/vCards.js}"></script>

 <p th:text="'User Name:   ' + ${authRequest.userName}" />

控制器

@RequestMapping(value= "/virtualWallet/login" , method= RequestMethod.POST)
public String getAuthResponse(
        @ModelAttribute AuthRequest authRequest,
        Model model
        ){

            String resp = authService.getAuthResponse(authRequest);

            if(resp.equals("Success")) {
                return "dashboard";

            }
            return null;
        }

@RequestMapping(value= "/viewCards" , method = RequestMethod.POST)
public String viewCards(
        @ModelAttribute AuthRequest authRequest
        ) {
            return "viewCards";}

期望在viewCards.html"中获得authRequest"值.我越来越空了.

Expected to get "authRequest" value in "viewCards.html". I am getting null.

推荐答案

你的第一个方法几乎是正确的.

Your first approach was almost right.

<form th:action="@{/viewCards}" th:object="${authRequest}" method="post">
    <input type="text" th:value="*{authRequestAttr1}" th:field="*{authRequestAttr1}" hidden/>
    <!-- Add the rest of authRequest attributes here in the same fashion -->

    <button type="submit"></button>
</form>

对于要提交的 th:object 中的对象,您需要具有包含该对象中属性值的输入.您还需要将它们与 th:field 绑定.

For the object in th:object to be submitted you need to have inputs that contain the values of the attributes in that object. You also need to bind them with th:field.

由于您只想将该对象传递给控制器​​,因此在示例中我隐藏了输入,但这不是必需的.

Since you just want to pass that object to a controller, in the example I've hidden the inputs, but that is not neccesary.

这篇关于无法将从控制器传递到 thymeleaf html 的模型属性传递回另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆