无法正确映射静态资源

时间:2016-11-25 01:53:08

标签: java spring spring-mvc spring-boot

我的项目结构如下

| SRC

| - 主

| --- web应用

| ----静态

| ----- CSS

| ----- HTML

| ----- JS

我正在尝试通过我的控制器返回一个HTML资源,用于直接在root下的链接没有问题但是对于其他链接,我遇到了问题。

这是我的控制器

@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时,控制器返回给定的值,作为回报,我获得了正确的资源。

但是当我尝试使用/ support / 1 / xsdda时,我被映射到/support/1/static/html/index.html有人可以解释内部并从逻辑上指出我的错误。

谢谢!

1 个答案:

答案 0 :(得分:1)

在webapp中创建文件夹WEB-INF并将HTML文件夹放在其中

  • 将解析器的前缀设置为/ WEB-INF / HTML /
  • 将解析器的后缀设置为.html
  • 通过返回" index"
  • 来调用它

Servlet 2.4规范说明了WEB-INF(第70页):

  

名为的应用程序层次结构中存在一个特殊目录   import time from neopixel import * # LED strip configuration: LED_COUNT = 1 # Number of LED pixels. LED_PIN = 18 # GPIO pin connected to the pixels (must support PWM!). LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz) LED_DMA = 5 # DMA channel to use for generating signal (try 5) LED_BRIGHTNESS = 64 # Set to 0 for darkest and 255 for brightest LED_INVERT = False # True to invert the signal (when using NPN transistor level shift) # Define functions which animate LEDs in various ways. def colorWipe(strip, color, wait_ms=50): """Wipe color across display a pixel at a time.""" for i in range(strip.numPixels()): strip.setPixelColor(i, color) strip.show() time.sleep(wait_ms/1000.0) # Main program logic follows: if __name__ == '__main__': # Create NeoPixel object with appropriate configuration. strip = Adafruit_NeoPixel(LED_COUNT, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS) # Intialize the library (must be called once before other functions). strip.begin() print ('Press Ctrl-C to quit.') while True: # Color wipe animations. colorWipe(strip, Color(255, 0, 0)) # Red wipe colorWipe(strip, Color(0, 255, 0)) # Blue wipe colorWipe(strip, Color(0, 0, 255)) # Green wipe 。该目录包含与之相关的所有内容   应用程序不在应用程序的文档根目录中。的的   WEB-INF节点不是公共文档树的一部分   应用即可。不能提供WEB-INF目录中包含的文件   由容器直接发送给客户端。但是,内容了   使用WEB-INF对servlet代码可见WEB-INF目录   和getResource上的getResourceAsStream方法调用可能   使用ServletContext来电曝光。