马文斯
我正在努力从Themeleaf调用一个控制器。
我的like代码如下所示:
<form action="#" th:action="@{/order}" modelAttribute="order" th:object="${order}" method="POST">
<div class="div">
<h5>Amount</h5>
<input type="text" class="input" th:field="*{amountValue}">
</div>
<input type="submit" class="btn" value="Process Payment">
</form>我的控制器代码是:
@RequestMapping(value = "/order", method = RequestMethod.POST)
public ModelAndView processOrder(@ModelAttribute Order order) {
ModelAndView modelAndView = new ModelAndView();
String accessToken = token();
String paymentURL = null;
if (accessToken != null) {
paymentURL = placeOrder(accessToken, order);
if (paymentURL != null) {
modelAndView.addObject("orderReferenceNumber", paymentURL.substring(paymentURL.indexOf("=") + 1));
modelAndView.addObject("paymentURL", paymentURL + "&slim=true");
modelAndView.setViewName("paymentProcess");
return modelAndView;
}
}
return modelAndView;
}我的Get方法是
@RequestMapping(value = "/index", method = RequestMethod.POST)
public ModelAndView doLogin(@RequestParam(value = "username", required = true) String username,
@RequestParam(value = "password", required = true) String password) {
ModelAndView modelAndView = new ModelAndView();
if (username != null && password != null) {
if (username.equalsIgnoreCase("one") && password.equalsIgnoreCase("one")) {
modelAndView.addObject("order", new Order());
modelAndView.setViewName("index");
return modelAndView;
}
}
modelAndView.setViewName("welcome");
return modelAndView;
}单击按钮时出错
Error resolving template [order], template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template [order], template might not exist or might not be accessible by any of the configured Template Resolvers我做错了什么?
发布于 2020-03-13 02:25:27
问题来自于您如何填充ModelAndView实例。只有当两个if子句匹配时,才可以使用modelAndView.setViewName("paymentProcess");设置视图的名称。这意味着对于某些执行(不匹配两个条件),根本不需要设置视图的名称,Spring MVC不知道要呈现哪个视图并返回给用户。
要解决此问题,请确保始终将默认/ if视图设置为在两个条件都不是true的情况下返回。您的代码可能会覆盖此视图名称,但至少在每种情况下都有一个视图可供后备:
@RequestMapping(value = "/order", method = RequestMethod.POST)
public ModelAndView processOrder(@ModelAttribute Order order) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("yourDefaultViewName"); // this is the important line
String accessToken = token();
String paymentURL = null;
if (accessToken != null) {
paymentURL = placeOrder(accessToken, order);
if (paymentURL != null) {
modelAndView.addObject("orderReferenceNumber", paymentURL.substring(paymentURL.indexOf("=") + 1));
modelAndView.addObject("paymentURL", paymentURL + "&slim=true");
modelAndView.setViewName("paymentProcess");
return modelAndView;
}
}
return modelAndView;
}https://stackoverflow.com/questions/60650919
复制相似问题