gpt4 book ai didi

java - 异常处理模板 "searchresult"

转载 作者:行者123 更新时间:2023-12-02 10:31:58 25 4
gpt4 key购买 nike

我正在开发一个基于Spring Boot的Web应用程序。我正在使用 thymeleaf 将内容注入(inject)我的网页。这是我的 Controller 类:

@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>

但我收到此错误:

Exception evaluating SpringEL expression: "searchresult.text[0]" (template: "searchresult" - line 90, col 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

您需要使用正确的字段名称。例如,使用 city 而不是 text。下面的代码应该可以工作。

<th:block th:each="city : ${searchPageResult.city}">
<h1><span th:text="${city}" /></h1>
</th:block>

对于实体的其余属性,只需对其相应的名称执行相同的操作即可。

关于java - 异常处理模板 "searchresult",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com