spring怎么引用静态资源

不及物动词 其他 40

回复

共3条回复 我来回复
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在Spring框架中,可以通过配置和注解的方式来引用静态资源。下面给出两种常用的引用静态资源的方法:

    方法一:配置方式

    1. 在Spring的配置文件(如applicationContext.xml)中,添加以下配置:
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    

    这里的/resources/**是用来映射请求路径的,/resources/是静态资源存放的路径。

    1. 在HTML页面中,可以通过以下方式引用静态资源:
    <link rel="stylesheet" type="text/css" href="/contextPath/resources/css/style.css">
    <script src="/contextPath/resources/js/script.js"></script>
    

    这里的/contextPath是应用的上下文路径,即应用的根目录路径。

    方法二:注解方式

    1. 在Spring的配置文件中,启用静态资源处理:
    <mvc:annotation-driven/>
    
    1. 在Controller类中,添加@Controller注解,并在需要引用静态资源的方法上添加@RequestMapping注解,示例如下:
    @Controller
    @RequestMapping("/")
    public class MyController {
        @RequestMapping("/index")
        public String index() {
            return "index";
        }
    }
    
    1. 在HTML页面中,可以通过以下方式引用静态资源:
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/style.css">
    <script src="${pageContext.request.contextPath}/resources/js/script.js"></script>
    

    这里的${pageContext.request.contextPath}是获取应用的上下文路径。

    通过以上两种方法,都可以成功引用Spring中的静态资源。

    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    在Spring项目中引用静态资源(如CSS、JavaScript、图片等)非常简单,可以通过以下几种方式来实现:

    1. 使用WebMvcConfigurer配置类:WebMvcConfigurer是Spring框架提供的一个接口,可以通过实现该接口来自定义Spring MVC的配置。在实现类中,我们可以通过重写addResourceHandlers方法,来指定静态资源的路径和访问方式。具体代码如下:
    @Configuration
    @EnableWebMvc
    public class MvcConfig implements WebMvcConfigurer {
        
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/static/**")
                    .addResourceLocations("classpath:/static/");
        }
    }
    

    其中,addResourceHandler指定了静态资源的URL路径,addResourceLocations指定了静态资源的存放位置。上述代码中,将静态资源存放在静态资源/static/目录下,通过URL路径/static/进行访问。

    1. 添加静态资源的映射:在Spring Boot项目中,可以直接通过在application.properties文件(或者application.yml文件)中添加静态资源的映射。具体代码如下:
    spring.resources.static-locations=classpath:/static/
    

    上述代码将静态资源存放在静态资源/static/目录下,只需将静态资源文件直接放在该目录下即可。

    1. 使用Thymeleaf模板引擎:如果项目中使用了Thymeleaf模板引擎,可以直接通过Thymeleaf的标签来引用静态资源。具体代码如下:
    <link rel="stylesheet" th:href="@{/static/css/style.css}" />
    <script th:src="@{/static/js/script.js}"></script>
    <img th:src="@{/static/img/logo.png}" />
    

    上述代码中,使用了Thymeleaf的th:hrefth:src标签来指定静态资源的路径。需要注意的是,这里的路径是相对于Web应用的根路径的。

    1. 使用Servlet的方式:如果以上方法都无法满足需求,还可以在Spring项目中使用Servlet的方式来处理静态资源。可以通过在web.xml文件中配置org.springframework.web.servlet.DispatcherServlet的映射路径来实现。具体代码如下:
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    springmvc.xml配置文件中添加以下内容:

    <mvc:default-servlet-handler/>
    

    通过以上配置,可以将所有请求转发给Spring MVC的DispatcherServlet处理,同时,静态资源文件也会被正确地访问。

    总结:
    引用静态资源在Spring项目中非常简单,可以通过WebMvcConfigurer配置类、静态资源的映射、Thymeleaf模板引擎或者Servlet的方式来实现。根据具体需求,选择适合的方式即可。

    1年前 0条评论
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Spring框架中,引用静态资源主要有两种方式:通过WebMvcConfigurer配置类和通过注解@ControllerAdvice + @RequestMapping方式。

    1. 配置类方式:通过实现WebMvcConfigurer接口,在配置类中添加资源处理器。

    首先,创建一个配置类,并添加@Configuration注解:

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
        
    }
    

    然后,重写addResourceHandlers()方法,配置静态资源处理器:

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
            .addResourceLocations("classpath:/static/");
    }
    

    以上代码将会将/static/路径下的静态资源映射为classpath:/static/目录下的文件。

    最后,将配置类注册到Spring容器中。

    1. 注解方式:通过使用@ControllerAdvice注解和@RequestMapping注解,可以在控制器中直接引用静态资源。

    首先,创建一个控制器类,并添加@ControllerAdvice注解:

    @ControllerAdvice
    public class StaticResourceController {
        
    }
    

    然后,在控制器类中使用@RequestMapping注解,指定访问静态资源的URL路径:

    @RestController
    @RequestMapping("/static")
    public class StaticResourceController {
        
        @GetMapping("/css/{fileName}")
        public ResponseEntity<Resource> getCssFile(@PathVariable String fileName) throws IOException {
            Resource resource = new ClassPathResource("static/css/" + fileName);
            return ResponseEntity.ok()
                .contentType(MediaType.TEXT_HTML)
                .body(resource);
        }
        
        @GetMapping("/js/{fileName}")
        public ResponseEntity<Resource> getJsFile(@PathVariable String fileName) throws IOException {
            Resource resource = new ClassPathResource("static/js/" + fileName);
            return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_JS)
                .body(resource);
        }
        
        // 其他静态资源的处理方法...
        
    }
    

    上述代码中,通过@GetMapping注解指定了访问CSS和JS文件的方法,并使用@PathVariable注解来获取文件名。然后使用Resource接口来加载静态资源文件,并返回ResponseEntity对象,设置相应的Content-Type。

    最后,需要将控制器类注册到Spring容器中。

    通过以上两种方式,我们可以在Spring框架中引用静态资源。通过配置类方式适用于配置多个静态资源处理器的情况,而注解方式适用于在控制器中直接引用静态资源文件的情况。无论哪种方式,都能实现在Spring框架中引用静态资源。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部