我正在尝试使用百里香和Spring Boot构建简单的搜索选项卡。 这是我的代码和html文件。
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";
}
模式类
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;
}
}
日志
:[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:执行处理器'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'时出错(模板:“ scCountry”-第31行,第86列) 在org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.util.ProcessorConfigurationUtils $ ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)上[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)上[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354)[thymeleafspring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)[tomcat-embed-core-8.5.31.jar:8.5.31] 在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)[tomcat-embed-core-8.5.31.jar:8.5.31] 在org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:790)[tomcat-embed-core-8.5.31.jar:8.5.31] 在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1468)[tomcat-embed-core-8.5.31.jar:8.5.31] 在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)上[tomcat-embed-core-8.5.31.jar:8.5.31] 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)[na:1.8.0_121] 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)[na:1.8.0_121] 在org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)[tomcat-embed-core-8.5.31.jar:8.5.31] 在java.lang.Thread.run(Thread.java:745)[na:1.8.0_121] 由以下原因引起:java.lang.IllegalStateException:Bean名称“ sInput”的BindingResult或纯目标对象都不能用作请求属性 在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:153)〜[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)〜[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305)〜[thymeleafspring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] ...省略了56个常见框架
2018-12-13 20:45:54.002
(BindStatus.java:153)〜[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)〜[spring-webmvc-5.0.7.RELEASE.jar:5.0.7.RELEASE] 在org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:305)〜[thymeleafspring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:252)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:226)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)〜[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.util.ProcessorConfigurationUtils $ ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.StandaloneElementTag.beHandled(StandaloneElementTag.java:228)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE] 在org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661)〜[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
...
我收到错误消息:Bean名称'sInput'的BindingResult或普通目标对象都不能用作请求属性
我尝试了下面的解决方案,该解决方案在此平台中得到了解决
html form validation using thymeleaf not working spring boot
Spring Thymeleaf TemplateProcessingException in SpringInputGeneralFieldTagProcessor
using search functionality in thymeleaf with request parameters
如果我使用下面的表格,它正在工作。
<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>
但是我想知道我两天想念什么吗?
有人可以帮助我吗?
我想知道为什么scCountry.html中的“ th:object =” $ {sInput}“不能在模型中获取相同名称的对象吗?
答案 0 :(得分:2)
遇到了类似的问题。 这不起作用:
<input type="text" th:field="*{content}">
这样做了:
<input type="text" field="*{content}" name="content">
答案 1 :(得分:0)
th:field="*{sInput.keyWord}"
应该是
th:field="*{keyWord}"
使用*{...}
表达式时,将假设th:object
。
答案 2 :(得分:0)
在控制器中进行以下修改,然后尝试
@GetMapping(value="/search")
public ModelAndView SearchForm(Model model, SearchInput sInput) {
return new ModelAndView("scCountry");
}
希望您会解决您的错误。