1

I am trying to build simple search tab with thymeleaf and Spring boot. Here is my codes and html files.

scCountry.html

  <form class="form-inline" action="#"  th:action="@{/search}" modelAttribute="sInput" th:object="${sInput}" method="post">
                <input class="form-control mr-sm-2" type="text" placeholder="Search" th:field="*{keyWord}">
                <input type="submit" value="Submit" />
      <!--      <button class="btn btn-success" type="Submit" value="Submit">Search</button> -->
            </form>

Controller.class

 @GetMapping(value="/search")
    public String SearchForm(Model model) {

         SearchInput sInput = new SearchInput();
        model.addAttribute("sInput",sInput);

        return "scCountry";
    }

    @PostMapping(value="/search")
    public String SearchResult(@ModelAttribute("sInput") SearchInput sInput, BindingResult bindingResult, Model model) {
        if (bindingResult.hasErrors()) {
            return "scCountry";
        }

        SearchVO searchVO = null;
        try {
            searchVO = webServices.searchAll(sInput.getKeyWord());
        } catch (TwitterException e) {
            e.printStackTrace();
        }

        model.addAttribute("searchVO",searchVO);

        return "scSearch";
    }

Modal Class

public class SearchInput {

    private String keyWord;
    public String getKeyWord() {
        return keyWord;
    }
    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }
    public SearchInput() {
    }
    public SearchInput(String keyWord) {
        this.keyWord = keyWord;
    }
}

Log

         :[Ljava.lang.Byte;@3929bes             :[Ljava.lang.Byte;@1995b82018-12-13 20:45:53.989 ERROR 6400 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "scCountry": Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "scCountry" - line 31, col 86)

org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "scCountry" - line 31, col 86) at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354) [thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468) [tomcat-embed-core-8.5.31.jar:8.5.31] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.31.jar:8.5.31] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.31.jar:8.5.31] at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121] Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'sInput' available as request attribute at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] ... 56 common frames omitted

2018-12-13 20:45:54.002

ERROR 6400 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "scCountry" - line 31, col 86)] with root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'sInput' available as request attribute at org.springframework.web.servlet.support.BindStatus.

(BindStatus.java:153) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903) ~[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]

...

I am getting the error : Neither BindingResult nor plain target object for bean name 'sInput' available as request attribute

I tried the solutions in below which is answered in this platform:

html form validation using thymeleaf not working spring boot

Thymeleaf registration page - Error during execution of processor 'org.thymeleaf.spring4.processor.attr.SpringInputGeneralFieldAttrProcessor'

Spring Thymeleaf TemplateProcessingException in SpringInputGeneralFieldTagProcessor

using search functionality in thymeleaf with request parameters

If I use the form like below,It is working.

<form class="form-inline"   action="/search" method="post">
                <input class="form-control mr-sm-2" type="text"  id="keyWord" name="keyWord" value=""  placeholder="Search" >
                <input type="submit" value="Submit" />
      <!--      <button class="btn btn-success" type="Submit" value="Submit">Search</button> -->
            </form>

But I wondering what i am missing for two days?

Could anyone help me ?

I want to know why th:object="${sInput} in the scCountry.html is not fetching the object in the model which i put it in model with the same name?

Michu93
  • 5,058
  • 7
  • 47
  • 80
Cocuthemyth
  • 264
  • 1
  • 2
  • 11

5 Answers5

4

Was facing a similar problem. This didn't work:

<input type="text" th:field="*{content}">

This did:

<input type="text" field="*{content}" name="content">

2
th:field="*{sInput.keyWord}"

should be

th:field="*{keyWord}"

When using *{...} expressions, the th:object is assumed.

Metroids
  • 18,999
  • 4
  • 41
  • 52
  • I tried it.But nothing has changed.Also according to the error description, th:object="${sInput}" is not available before than keyWord.I am editing the question as "*{keyWord}" – Cocuthemyth Dec 13 '18 at 13:12
  • So what happens when you print out ` – Metroids Dec 13 '18 at 15:34
  • I added the error log and Model Class in the above.I am getting the error : "There was an unexpected error (type=Internal Server Error, status=500). Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "scCountry" - line 31, col 86)". Also the page is not opening.The error is occuring. – Cocuthemyth Dec 13 '18 at 20:59
0

Do the below modifications in your controller and try

@GetMapping(value="/search")

public ModelAndView SearchForm(Model model, SearchInput sInput) {

    return new ModelAndView("scCountry");

}

Hope you will get solved your error.

borchvm
  • 3,533
  • 16
  • 44
  • 45
Priya
  • 1
  • 1
0

A was have the same proplem. That helped me. Old code -

@PostMapping()
public String create(@Valid AdditionalContact additionalContact, BindingResult bindingResult, BindingResult bindingResultForId, Model model) {
   if (bindingResult.hasErrors() || bindingResultForId.hasErrors())
        model.addAttribute("contacts", additionalContact.getAll());
    return "additionalContact/create";
    additionalContactService.save(additionalContact);
    return "redirect:contacts";
    }

new code

@PostMapping()
public String create(@Valid @ModelAttribute("contact") AdditionalContact additionalContact, BindingResult bindingResult,
                         BindingResult bindingResultForId) {

if (bindingResult.hasErrors() || bindingResultForId.hasErrors())
return "additionalContact/create";

additionalContactService.save(additionalContact);
return "redirect:contacts";
}
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

Simply take off the "th:" from the "th:field" and you are good to go!

Sam
  • 21
  • 2