【问题标题】:spring boot- thymeleaf pass data html to controllerspring boot-thymeleaf 将数据 html 传递给控制器
【发布时间】:2017-06-28 14:13:03
【问题描述】:

我尝试将数据 html 传递给控制器​​,但出现这样的错误。我的 GET 方法可以正常工作,但 POST 方法不起作用

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'post' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:144) ~[spring-webmvc-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:401) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:328) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.spring4.util.FieldUtils.getBindStatus(FieldUtils.java:294) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.spring4.processor.attr.AbstractSpringFieldAttrProcessor.processAttribute(AbstractSpringFieldAttrProcessor.java:98) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.processor.attr.AbstractAttrProcessor.doProcess(AbstractAttrProcessor.java:87) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.processor.AbstractProcessor.process(AbstractProcessor.java:212) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.dom.Node.applyNextProcessor(Node.java:1017) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:972) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.dom.NestableNode.computeNextChild(NestableNode.java:695) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.dom.NestableNode.doAdditionalProcess(NestableNode.java:668) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.dom.Node.processNode(Node.java:990) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

这是我的html表单

 <form autocomplete="off" action="#" th:action="@{/(${titleId})}"
              th:object="${post}" method="post" class="form-horizontal"
              role="form">
            <h2>Formu doldur aramıza katıl..</h2>
            <div class="form-group">
                <div class="col-sm-9">
                    <input type="text" th:field="*{postBody}" placeholder="yorumla"
                           class="form-control" />
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-9">
                    <button type="submit" class="btn btn-primary btn-block">Kayıt Ol</button>
                </div>
            </div>
        </form>

我的控制器方法终于来了

@RequestMapping(value = "/{id}", method = RequestMethod.POST)
    public ModelAndView savePost(@Valid Post post,@PathVariable("id")Long id){

        ModelAndView modelAndView =new ModelAndView();

        Title title = titleService.getTitleById(id);

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        User user = userService.findUserByEmail(auth.getName());

        Date date = new Date();

        Post post1 = new Post();

        post1.setPostBody(post.getPostBody());
        post1.setPostDate(date);
        post1.setPostSender(user);
        post1.setPostTitle(title);

        postService.savePost(post1);
        modelAndView.addObject("post",post1);
        modelAndView.setViewName("homePage2");

        return modelAndView;
    }

我不明白为什么我会出错。

【问题讨论】:

    标签: spring spring-mvc spring-boot thymeleaf


    【解决方案1】:

    将@ModelAttribute 添加到您的控制器

    public ModelAndView savePost(@Valid @ModelAttribute("post") Post post,@PathVariable("id")Long id)
    

    并检查您是否在 GET 上的模型中添加了 Post 实例。

    @RequestMapping(method = RequestMethod.GET)
    public String postPage(Model model) {
         model.addAttribute("post", new Post());
         return "login";
    }
    

    【讨论】:

      【解决方案2】:

      根据您的堆栈跟踪:

      java.lang.IllegalStateException: 既不是 BindingResult 也不是普通的 bean 名称“post”的目标对象可用作请求属性

      您的问题在于:@Valid Post post 在您的处理程序方法上。似乎由于某种原因,Spring 未能将您发布的表单中的数据绑定到该对象中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-14
        • 2020-04-28
        • 2021-07-06
        • 1970-01-01
        • 2019-07-05
        • 2018-11-02
        • 1970-01-01
        • 2020-02-09
        相关资源
        最近更新 更多