如何将参数传递给Thymeleaf Ajax Fragment [英] How pass params to Thymeleaf Ajax Fragment
问题描述
我有一个Spring MVC控制器,该控制器返回百里香叶片段的名称以查看解析器bean.问题在于此片段需要使用url作为参数.在这里我放了片段:
I have a Spring MVC controller that returns the name of a thymeleaf fragment to view resolver bean. The problem is that this fragment needs a url as a parameter. Here I put the fragment:
<!-- 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>
我无法在没有错误的情况下传递该参数.控制器如下:
I can not get a way to pass that parameter without getting an error. The controller is as follows:
@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})";
}
对于上面的示例,我收到以下错误:
For the above example I get the following error:
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')";
总的来说我会出错
推荐答案
您有两种方法将参数从控制器传递到Thymeleaf片段.首先是通常的Spring方法-抛出模型:
You have two ways for pass parameter from controller to Thymeleaf fragment. First is the usual Spring way - throw the model:
@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";
}
足够了.在这种情况下,您无需指定任何片段参数(即使您有).
That's enough. In this case you don't need specify any fragment parameters (even if you have it).
第二种方法是在片段名称中指定参数:
Second way is specify parameters in fragment name:
@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
变量.
Note, that name of parameter must be specified, and the string value must be placed in single quotes. In this case you don't need add url
variable into model.
这篇关于如何将参数传递给Thymeleaf Ajax Fragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!