我在 spring-boot 專案中有以下組態檔:
@Configuration
public class AppConfig {
@Bean
public ViewResolver mustacheViewResolver() {
MustacheViewResolver viewResolver = new MustacheViewResolver();
viewResolver.setPrefix("/templates/");
viewResolver.setSuffix(".mustache");
viewResolver.setOrder(1);
return viewResolver;
}
}
當我運行我的應用程式時,我收到以下錯誤:
說明:
無法注冊在類路徑資源 [org/springframework/boot/autoconfigure/mustache/MustacheServletWebConfiguration.class] 中定義的 bean 'mustacheViewResolver'。已在類路徑資源 [com/example/demo/AppConfig.class] 中定義了具有該名稱的 bean,并且禁用了覆寫。
行動:
考慮重命名 bean 之一或通過設定 spring.main.allow-bean-definition-overriding=true 來啟用覆寫
我不確定我是否正確配置了視圖決議器
洗掉配置類后報錯:
o.s.w.s.v.ContentNegotiatingViewResolver : Selected '*/*' given [*/*]
o.s.w.servlet.view.InternalResourceView : View name 'tweets.mustache', model {tweets=null}
o.s.w.servlet.view.InternalResourceView : Forwarding to [tweets.mustache]
o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/[email protected]", parameters={masked}
o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 404
o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/[email protected]", parameters={masked}
s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json', given [*/*] and supported [application/json, application/* json, application/json, application/* json]
o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=, status=404, error=Not Found, path=/tweet2}]
o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
@GetMapping("/tweet2")
public ModelAndView getTweetsByEmail(@RequestParam String email) {
HQLExample.insertRecords();
ModelAndView modelAndView = new ModelAndView("tweets.mustache");
List<Tweet> tweets = tweetMap.get(email);
modelAndView.getModel().put("tweets",tweets);
return modelAndView;
}
uj5u.com熱心網友回復:
假設您已添加spring-boot-starter-mustache為依賴項(以輕松包含所有需要的依賴項)。當 Spring Boot 在類路徑上檢測到 Mustache 時,它??將自動配置MustacheViewResolver將從類路徑上加載 Mustache 模板的配置/templates。檔案應以.mustache.
考慮到這一點,只需洗掉您的AppConfig類,因為它會干擾自動配置。
在您的控制器中,視圖的名稱是您擁有的名稱,但沒有.mustache將由ViewResolver.
所以簡而言之,你應該洗掉一些東西,它會起作用。在這種情況下,事半功倍。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/440098.html
上一篇:JPA物體在服務層中為空
