我正在开发基于Spring Boot的Web应用程序。我正在使用百里香叶将内容注入到我的网页中。
这是我的控制器类:
@GetMapping("/search")
public String searchPage(@RequestParam("q") String queryString) throws IOException, SolrServerException {
ModelMap model = new ModelMap();
SearchPageResult searchPageResult = tweelyzeConfig.getSearchResults(queryString);
System.out.println("Here are they==============================================");
System.out.println(searchPageResult.getCity());
System.out.println(searchPageResult.getHashtags());
System.out.println(searchPageResult.getLang());
System.out.println(searchPageResult.getText());
System.out.println(searchPageResult.getTopic());
System.out.println(searchPageResult.getTweet_urls());
model.addAttribute("searchresult",searchPageResult);
return "searchresult";
}
在这里,我接受来自UI的查询以搜索我的solr。我正在使用solrj对solr进行其余调用,以获取与此查询相关的结果。我的TweelyzeConfig类:
private SolrQuery setSolrQuery(String query) {
solrQuery.setQuery(query);
return solrQuery;
}
public SearchPageResult getSearchResults(String query) throws IOException, SolrServerException {
QueryResponse queryResponse = solrClient.query(setSolrQuery(query));
List<SearchPageResult> sdList = queryResponse.getBeans(SearchPageResult.class);
return sdList.get(0);
}
我的SearchResultPage类是:
public class SearchPageResult {
private List<String> city;
private List<String> topic;
private List<String> lang;
private List<String> text;
private List<String> tweet_urls;
private List<String> hashtags;
public SearchPageResult(){}
@Field("city")
public void setCity(List<String> city) {
this.city = city;
}
@Field("topic")
public void setTopic(List<String> topic) {
this.topic = topic;
}
@Field("lang")
public void setLang(List<String> lang) {
this.lang = lang;
}
@Field("text")
public void setText(List<String> text) {
this.text = text;
}
@Field("tweet_urls")
public void setTweet_urls(List<String> tweet_urls) {
this.tweet_urls = tweet_urls;
}
@Field("hashtags")
public void setHashtags(List<String> hashtags) {
this.hashtags = hashtags;
}
public List<String> getCity() {
return city;
}
public List<String> getTopic() {
return topic;
}
public List<String> getLang() {
return lang;
}
public List<String> getText() {
return text;
}
public List<String> getTweet_urls() {
return tweet_urls;
}
public List<String> getHashtags() {
return hashtags;
}
}
这就是我试图将内容嵌入html的方式:
<p th:text="${searchresult.text[0]}">hhhhhhhhh</p>
但我收到此错误:
评估SpringEL表达式的异常:“ searchresult.text [0]”(模板:“ searchresult”-第90行,第23行)
我无法弄清楚我在做什么错。
任何帮助表示赞赏。
更新:这是堆栈跟踪,以便更好地参考。
2018-12-01 11:57:08.390 ERROR 38681 --- [nio-8080-exec-5] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-5] Exception processing template "searchresult": Exception evaluating SpringEL expression: "searchPageResult.text[0]" (template: "searchresult" - line 90, col 23)
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "searchPageResult.text[0]" (template: "searchresult" - line 90, col 23)
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:290) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1314) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:661) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) [thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
<snip>
... 105 common frames omitted
2018-12-01 11:57:08.394 ERROR 38681 --- [nio-8080-exec-5] 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: Exception evaluating SpringEL expression: "searchPageResult.text[0]" (template: "searchresult" - line 90, col 23)] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'text' cannot be found on null
at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:213) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:59) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:89) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:111) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:328) ~[spring-expression-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:263) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.VariableExpression.executeVariableExpression(VariableExpression.java:166) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.SimpleExpression.executeSimple(SimpleExpression.java:66) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:109) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.expression.Expression.execute(Expression.java:138) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.standard.processor.AbstractStandardExpressionAttributeTagProcessor.doProcess(AbstractStandardExpressionAttributeTagProcessor.java:144) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
<snip>
最佳答案
您的实体中没有任何称为text的属性。如您在控制台中所见,这导致模板解析器失败。
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'text' cannot be found on null
您需要使用正确的字段名称。例如,使用
text
代替city
。下面的代码应该工作。<th:block th:each="city : ${searchPageResult.city}">
<h1><span th:text="${city}" /></h1>
</th:block>
对于实体的其余属性,只需对其对应的名称进行相同的处理。
关于java - 异常处理模板“searchresult”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53568927/