Che*_*rry 2 java spring spring-mvc
我创建了没有web.xml的Spring应用程序.我想从WEB-INF提供静态index.html.以下是来源:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {return new Class<?>[]{AppConfiguration.class};}
@Override
protected Class<?>[] getServletConfigClasses() {return new Class[]{WebConfiguration.class};}
@Override
protected String[] getServletMappings() {return new String[]{"/ololo/*"};}
}
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfiguration {}
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/index.html").addResourceLocations("classpath:/WEB-INF/view/index.html");
}
}
Run Code Online (Sandbox Code Playgroud)
一切似乎都是正确的 - index.html被放入webapp\WEB-INF\view\index.html,但我仍然进入日志(contextRoot是我部署的战争的上下文根):
[2015-09-11T11:06:21.247+0500] [glassfish 4.1] [WARNING] [] [org.springframework.web.servlet.PageNotFound] [tid: _ThreadID=29 _ThreadName=http-listener-1(3)] [timeMillis: 1441951581247] [levelValue: 900] [[
No mapping found for HTTP request with URI [/contextRoot/ololo/index.html] in DispatcherServlet with name 'dispatcher']]
Run Code Online (Sandbox Code Playgroud)
配置有什么问题?
更新
当出现问题时,Spring中的路径是一个大问题,因此欢迎在调度请求或我可以看到配置实例化的上下文中有关spring调试的任何建议.
试试下面的代码,我有一个有效的例子
@Configuration
@EnableWebMvc
@ComponentScan("com.Your.Package")//in my case com.igu
public class WebConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}
Run Code Online (Sandbox Code Playgroud)
将您的静态html文件(index.html)放在webapp/static /中
哪个会将所有以/ static /开头的请求传递给webapp/static /目录.