SpringBugでSpringMVCアプリケーションを実行しているときに問題に直面しています。アプリケーションはマップされたコントローラを呼び出してビューを正常に解決できますが、静的コンテンツを解決することはできません。私は春のブートが自動的に静的リソースについては、以下のフォルダを見て読んでいるSpringブートディスパッチャーサーブレットが静的コンテンツを検索/マップできません
: - static
など public
Web_INF/resources
、
私は、次の場所に私のファイルを維持しようとしたが、まだそれは助けにはなりませんでした。
コンソール上の私の出力である:
名とのDispatcherServletに内部コントローラ2016年8月23日17:22:12.002 [HTTP-NIO-9990-EXEC-1] [org.springframework.web.servlet.PageNotFoundn ] [WARN] org.springframework.web.servlet.PageNotFound:{} - マッピングが持つHTTPリクエストの を見つかりませんURI [/springBootApp/templates/hello.html] 'のDispatcherServlet'
ここで、springBootAppは私のコンテキストルートです
以下は私のクラスである: - SpringBootクラス: -
@SpringBootApplication
public class ServiceApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(ServiceApplication.class);
Set<ApplicationContextInitializer<?>> initLst = app.getInitializers();
initLst.add(new PropertyPasswordDecodingContextInitializer());
app.setInitializers(initLst);
ApplicationContext ctx = app.run(args);
System.out.println("Services Started");
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
application = application.initializers(new PropertyPasswordDecodingContextInitializer());
return application.sources(ServiceApplication.class);
}
@Override
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
//project specific config
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
MVCConfigClass:
@EnableWebMvc
@Configuration
@ComponentScan(basePackageClasses = ServiceApplication.class, includeFilters = @Filter(Controller.class), useDefaultFilters = false)
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Bean
public ViewResolver configureViewResolver() {
InternalResourceViewResolver viewResolve = new InternalResourceViewResolver();
viewResolve.setPrefix("/templates/");
viewResolve.setSuffix(".html");
return viewResolve;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/templates/**").addResourceLocations("/templates/");
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
RESTful Webサービスクラス -
@RestController
@RequestMapping(value = "/myapp")
public class TestWebService {
@RequestMapping(value = { "/hello" }, method = { RequestMethod.GET, RequestMethod.POST })
public ModelAndView list(HttpServletRequest request) throws Exception {
// HttpSession session = request.getSession();
System.out.println("Inside Controller");
ModelAndView modelView = new ModelAndView();
ModelMap modelMap = modelView.getModelMap();
modelView.setViewName("/hello");
return modelView;
}
}
私は、呼び出しに上記の出力を得ます: - http://localhost:9990/springBootApp/myapp/hello
'@ RestController'は、JSONやXMLなどで直接*の応答を返すことを意図しています。 HTMLテンプレートをレンダリングしたい場合は、プレーンな '@ Controller'を使用してください。 – chrylis
jsons – Saumyaraj
@Saumyarajを返すより多くのメソッドがあります。次にそれらを '@ResponseBody'で注釈を付けるか、別のコントローラを使用する必要があります。 – g00glen00b