欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > springMVC访问不同位置的静态资源

springMVC访问不同位置的静态资源

2025/5/1 20:31:54 来源:https://blog.csdn.net/BlackPudding_/article/details/141167582  浏览:    关键词:springMVC访问不同位置的静态资源

resources和webapp目录结构如下图:

1. 访问webapp目录下的静态资源

1. 配置类

        开启默认的servlet处理,处理webapp目录下的静态资源访问。需继承WebMvcConfigurer接口。

@Configuration
@EnableWebMvc // 开启Spring MVC的注解驱动
@ComponentScan(basePackages = "com.huan.web") // 扫描Controller包
public class WebConfig implements WebMvcConfigurer {/*** 访问 webapp下的静态资源* 配置默认的servlet处理器* @param configurer*/@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}
}

2. 控制类

        在控制类中处理访问静态资源时,需要使用forward转发。通过使用 forward,Spring MVC 会将请求转发给默认的 Servlet 处理静态资源,而不会通过视图解析器处理。

@Controller
public class WebController {@RequestMapping("/")public String index() {return "index";}@RequestMapping("/demo")public String test1() {return "forward:/demo.html";}
}

        以下图片是没有添加forward后运行访问的结果,这里将请求当作一个资源视图名称交给视图解析器进行处理,所以处理失败了。这里就需要搞懂/springmvc/xxx.html和/springmvc/xxx的区别

3. 前端页面(demo.html)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>Hello test1</h1><img src="static/images/OIP2.jpg">
</body>
</html>

4.测试访问

2. 访问WEB-INF目录下的静态资源

1. 配置类

        这里配置了视图解析器,处理访问WEB-INF目录下的静态资源。当服务器接受到无后缀的请求路径,会将该路径交给配置的视图解析器处理,拼接前后缀形成新的路径,然后进行访问。

@Configuration
@EnableWebMvc // 开启Spring MVC的注解驱动
@ComponentScan(basePackages = "com.huan.web") // 扫描Controller包
public class WebConfig implements WebMvcConfigurer {/*** 访问web-inf目录下的静态资源* 视图解析器 配置路径匹配规则* @return*/@Beanpublic InternalResourceViewResolver internalResourceViewResolver() {InternalResourceViewResolver resolver = new InternalResourceViewResolver();resolver.setPrefix("/WEB-INF/views/");resolver.setSuffix(".html");return resolver;}
}

2. 控制类

@Controller
public class HideController {@RequestMapping("/test")public String test() {return "test";}
}
//内部资源访问一定要经过springmvc

3. 前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>Hello world</h1>访问webapp下的static目录下的图片:<img src="static/images/OIP2.jpg">
</body>
</html>

        WEB-INF目录下的html要访问webapp目录下的资源时,相当于将WEB-INF/views目录下的文件复制到webapp目录下,项目结构如下图。

4. 测试访问

3. 访问其他任意位置的静态资源

1. 配置类

        以下配置访问路径是statics/下的所有资源,都由classpath:/statics/下的资源提供。

@Configuration
@EnableWebMvc // 开启Spring MVC的注解驱动
@ComponentScan(basePackages = "com.huan.web") // 扫描Controller包
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 配置访问路径是statics/下的所有资源,都由classpath:/statics/下的资源提供registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");}
}

2. 控制类

        由于在配置注册资源处理程序时,配置访问路径为statics/*,所以这里的路径映射也是/statics/*。

@Controller
public class ResourceController {@RequestMapping("/statics/testResources")public String testResources() {return "forward:/statics/testResources.html";}
}

3. 前端页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><h1>Hello world</h1>访问resources目录下的图片:<img src="image/OIP1.jpg"><br>访问webapp下的static目录下的图片:<img src="../static/images/OIP2.jpg">
</body>
</html>

        resources目录下的资源与WEB-INF目录下的类似,实际上会将resources下的statics目录放到webapp目录下,然后通过这样的目录结构进行访问。

4. 测试访问

over...

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词