【问题标题】:Themeleaf and submit form POSTThymeleaf 并提交表单 POST
【发布时间】:2020-03-12 09:20:27
【问题描述】:

专家,

我正在努力从 Themeleaf 调用控制器。

我的主题代码如下:

<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

我做错了什么?

【问题讨论】:

  • 能否请您添加返回包含此表单的初始视图的 GET 控制器方法?

标签: spring spring-boot spring-mvc spring-data-jpa


【解决方案1】:

问题在于您如何填充 ModelAndView 实例。只有当您的两个if 子句匹配时,您才能使用modelAndView.setViewName("paymentProcess"); 设置视图的名称。这意味着对于某些执行(不匹配您的两个条件),您根本不设置视图的名称,并且 Spring MVC 不知道要渲染并返回给用户的视图。

要解决此问题,请确保您始终设置默认/fallabck 视图以在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;

}

【讨论】:

  • 谢谢,但我怀疑这是问题所在。在我的例子中,/order 控制器不仅仅被调用。
  • 我在本地对其进行了测试,并且没有始终设置视图名称,但我收到了与您完全相同的错误消息。可以试试吗?
  • 天哪!.. 这确实是问题所在。谢谢你。
猜你喜欢
  • 2019-10-09
  • 2018-07-23
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多