我的项目结构如下无法映射静态资源正确
| SRC
| - 主
| --- web应用
| ----静态
| ----- CSS
| --- HTML HTML
| ----- js
我想通过我的控制器返回一个HTML资源,直接在根目录下的链接没有问题,但对于其他链接,我面临问题。
这里是我的控制器
@Controller
public class HtmlServer {
@RequestMapping({"/", "/index", "/index.html", "/index.jsp", "/home","/home.html","/home.jsp"})
public ModelAndView index() {
return new ModelAndView("html/index.html");
}
@RequestMapping(method = RequestMethod.GET, value = "/support/{id}/{accessToken}")
public ModelAndView start(@PathVariable Long id, @PathVariable String accessToken) {
return new ModelAndView("html/index.html");
}
}
这里是我的WebMvcConfigurerAdapter扩展类
@Component
@ConfigurationProperties
@Configuration
@EnableWebMvc
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver internalResourceViewResolver(){
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("static/");
return internalResourceViewResolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("static/js/")
.setCachePeriod(0);
registry.addResourceHandler("/css/**").addResourceLocations("static/css/")
.setCachePeriod(0);
registry.addResourceHandler("/support/**").addResourceLocations("/static/")
.setCachePeriod(0);
}
}
当我打开/或/index.html控制器返回给定值,作为回报,我正在服务的正确的资源。
但是,当我尝试使用/支持/ 1/xsdda我映射到/support/1/static/html/index.html有人可以解释内部并逻辑上指出我的错误。
谢谢!解析器的
我暂时通过使用重定向解决了这个问题:/static/html/index.html但这不是我正在寻找的,我希望HTML页面呈现给定的URL。我确定必须有一些可配置的类正在执行此操作。只是无法弄清楚哪一个。 – user1615664