Java Spring Boot dispatcher servlet无法找到/映射静态内容
在使用SpringBoot运行SpringMVC应用程序时,我遇到了一个问题。应用程序能够调用映射的控制器并成功解析视图,但无法解析静态内容 我读到spring boot会自动看到静态资源的以下文件夹:-Java Spring Boot dispatcher servlet无法找到/映射静态内容,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,在使用SpringBoot运行SpringMVC应用程序时,我遇到了一个问题。应用程序能够调用映射的控制器并成功解析视图,但无法解析静态内容 我读到spring boot会自动看到静态资源的以下文件夹:- 静态, public Web\u INF/resources等 我试着把我的文件放在下面的地方,但还是没用 我在控制台上的输出是: 内部控制器2016-08-23 17:22:12.002[http-nio-9990-exec-1] [org.springframework.web.servl
静态
,
public
Web\u INF/resources
等
我试着把我的文件放在下面的地方,但还是没用
我在控制台上的输出是:
内部控制器2016-08-23 17:22:12.002[http-nio-9990-exec-1]
[org.springframework.web.servlet.PageNotFoundn][WARN]
org.springframework.web.servlet.PageNotFound:{}-未找到映射
对于在中具有URI[/springBootApp/templates/hello.html]的HTTP请求
名为“DispatcherServlet”的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();
}
}
RESTfulWeb服务类-
@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;
}
}
我在调用时得到上述输出:-扩展
webmvcconfigureadapter
解决了问题扩展webmvcconfigureadapter
解决了问题Spring boot自动在src/main/resources文件夹中查找资源。
但是如果您为资源处理实现WebMvcConfigurationSupport方法,那么它会删除资源处理方法的默认实现。Spring boot会自动在src/main/resources文件夹中查找资源。
但是,如果您为资源处理实现WebMvcConfigurationSupport方法,那么它将删除资源处理方法的默认实现。A
@RestController
用于直接返回响应,例如JSON或XML。如果要呈现HTML模板,请使用普通的@Controller
。将有更多方法返回jsons@Saumyaraj然后,你应该用@ResponseBody
注释这些控件,或者使用单独的控制器。这与重点无关。它们应该在单独的控制器中,或者在方法级别而不是类级别应用了@ResponseBody
的同一控制器中。modelView.setViewName(“/hello”);应该是modelView.setViewName(“hello”);@RestController
用于直接返回响应,如JSON或XML。如果要呈现HTML模板,请使用普通的@Controller
。将有更多方法返回jsons@Saumyaraj然后,你应该用@ResponseBody
注释这些控件,或者使用单独的控制器。这与重点无关。它们应该在单独的控制器中,或者在方法级别而不是类级别应用了@ResponseBody
的同一控制器中。modelView.setViewName(“/hello”);应该是modelView.setViewName(“hello”);是的,我跳过了addResource处理程序,但它仍然无法获取静态资源。是的,我跳过了addResource处理程序,但它仍然无法获取静态资源
@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;
}
}