如何将Params传递给Thymeleaf Ajax Fragment

时间:2017-01-05 13:24:49

标签: ajax spring spring-mvc thymeleaf

我有一个Spring MVC控制器,它返回一个百万美元片段的名称来查看解析器bean。问题是这个片段需要一个url作为参数。在这里我把片段:

     <!-- A fragment with wrapper form for basic personal information fragment -->
 <th:block th:fragment="form-basic(url)">
    <form role="form" th:action="${url}" method="post" th:object="${user}">
         <th:block th:replace="admin/fragments/alerts::form-errors"></th:block>
         <th:block th:include="this::basic" th:remove="tag"/>
         <div class="margiv-top-10">
              <input type="submit"  class="btn green-haze" value="Save" th:value="#{admin.user.form.save}" />
              <input type="reset"  class="btn default" value="Reset" th:value="#{admin.user.form.reset}" />
        </div>
  </form>
</th:block>

我无法在不收到错误的情况下传递该参数。 控制器如下:

@RequestMapping(method = RequestMethod.GET)
public String show(@CurrentUser User user, Model model) {
   logger.info(user.toString());
   if(!model.containsAttribute(BINDING_RESULT_NAME)) {
       model.addAttribute(ATTRIBUTE_NAME, user);
   }
   model.addAttribute("url", "/admin/users/self/profile");
   return "admin/fragments/user/personal::form-basic({url})";
}

对于上面的示例,我收到以下错误:

06-Jan-2017 11:36:40.264 GRAVE [http-nio-8080-exec-9] org.apache.catalina.core.StandardWrapperValve.invoke El Servlet.service() para el servlet [dispatcher] en el contexto con ruta [/ejercicio3] lanzó la excepción [Request processing failed; nested exception is java.lang.IllegalArgumentException: Invalid template name specification: 'admin/fragments/user/personal::form-basic({url})'] con causa raíz
 java.lang.IllegalArgumentException: Invalid template name specification: 'admin/fragments/user/personal::form-basic({url})'
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:275)
    at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1257)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1037)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:980)

我做过这些测试:

 "admin/fragments/user/personal::form-basic('{url}')";
"admin/fragments/user/personal::form-basic(@{/admin/users/self/profile})";
"admin/fragments/user/personal::form-basic(/admin/users/self/profile)";
"admin/fragments/user/personal::form-basic('/admin/users/self/profile')";

总而言之,我得到了错误

2 个答案:

答案 0 :(得分:5)

从控制器到Thymeleaf片段有两种传递参数的方法。首先是通常的Spring方式 - 抛出模型:

@RequestMapping(method = RequestMethod.GET)
public String show(@CurrentUser User user, Model model) {
   model.addAttribute("url", "/admin/users/self/profile");
   return "admin/fragments/user/personal::form-basic";
}

够了。在这种情况下,您不需要指定任何片段参数(即使您拥有它)。

第二种方法是在片段名称中指定参数:

@RequestMapping(method = RequestMethod.GET)
public String show(@CurrentUser User user, Model model) {
   String url = "/admin/users/self/profile";
   return String.format("admin/fragments/user/personal::form-basic(url='%s')",url);
}

请注意,必须指定参数名称,并且字符串值必须放在单引号中。在这种情况下,您不需要将url变量添加到模型中。

答案 1 :(得分:1)

您需要做的就是将您的url参数包含为模型属性(就像您所做的那样)。无需以任何方式通知您的片段名称。只返回片段名称,因为它没有参数。

// rest of method body omitted
model.addAttribute("url", "/admin/users/self/profile");
return "admin/fragments/user/personal::form-basic";

顺便说一句。我看到你的网址导致其他一些端点。在这种情况下,您应该以{{1​​}}的方式在表单的@{...}属性中使用它。

th:action