當前位置:首頁 » 密碼管理 » spring靜態文件訪問

spring靜態文件訪問

發布時間: 2023-01-22 04:03:04

Ⅰ spring mvc 訪問靜態資源問題

怎麼感覺你的靜態資源配置有問題 ,問什麼要分開配置呢 ,直接配置一次就好了呀 <mvc:resources location="/WEB-INF/resource/" mapping="/resource/**"/>,jsp引用的時候直接用resource/img/xxx.jpg

Ⅱ springMVC訪問靜態文件如json文件,只能以get方法訪問嗎為什麼我前台ajax訪問用的是post都被拒絕了

訪問當然用get啦,提交用post

要想POST 首先定義一下 @RequestMapping(value="/XXX", method = RequestMethod.POST)

因為默認就是get形式

Ⅲ SpringMvc訪問靜態資源的幾種方法

第一種:在web.xml中配置名為default的servlet-mapping
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>

</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
第二種:在SpringMVC配置文件中添加<mvc:resource>標簽
<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/img/**" location="/img/"/>
第三種:在SpringMVC配置文件中添加<mvc:default-servlet-handler/>標簽
<mvc:default-servlet-handler/>
第四種:修改SpringMVC的攔截路徑,我這里使用的是/,也就是說所有的鏈接的攔截,
可以修改成*.do或者*.action或者其他,這樣靜態資源就不會攔截了
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Ⅳ Spring Boot自定義靜態資源映射

0、基礎html頁面

注意將不同demo的序號按文件修改即可, demo01.html 內容如下:

1、Spring Boot項目靜態資源默認訪問路徑

2、在IDEA中對應的路徑

訪問方法: http://localhost:8080/demo01.html ,如下:

3、優先順序順序

Ⅰ classpath:/META-INF/resources
→ Ⅱ classpath:/resources
→ Ⅲ classpath:/static
→ Ⅳ classpath:/public

1、配置文件配置

Ⅰ 此處是覆蓋原有配置的,所以默認路徑不能漏掉,假設刪除原有路徑映射,如下:

可以看到此時正常訪問的為,demo01和demo05,如下:

注意: 如此配置原有配置僅剩 classpath:/META-INF/resources 還生效。

Ⅱ 靜態文件請求匹配方式

修改後訪問路徑 http://localhost:8080/test/demo05.html ,訪問如下:

2、WebMvcConfigurationSupport配置

注意: 此種配置下,原有的靜態資源路徑被覆蓋、失效。

以上即為Spring Boot自定義靜態資源映射的全部內容,感謝閱讀。

Ⅳ springboot靜態資源訪問問題

1.springboot訪問靜態資源的幾種方式
(1)在src/main/resources/目錄下創建
static文件夾
(2)在src/main/resources/目錄下創建
resources文件夾
(3)在src/main/resources/目錄下創建
public文件夾
(4)在src/main/resources/目錄下創建
META-INF/resources文件夾

---------------------

本文來自 吳錦濤1 的CSDN 博客 ,全文地址請點擊:https://blog.csdn.net/qq_34797335/article/details/80194137?utm_source=

Ⅵ Spring Boot之如何配置靜態資源的地址與訪

靜態資源,例如HTML文件、JS文件,設計到的Spring Boot配置有兩項,一是「spring.mvc.static-path-pattern」,一是「spring.resources.static-locations」,很多人都難以分辨它們之間的差異,所以經常出現的結果就是404錯誤,無法找到靜態資源。
1. 「spring.mvc.static-path-pattern」
spring.mvc.static-path-pattern代表的含義是我們應該以什麼樣的路徑來訪問靜態資源,換句話說,只有靜態資源滿足什麼樣的匹配條件,Spring Boot才會處理靜態資源請求,以官方配置為例:
# 這表示只有靜態資源的訪問路徑為/resources/**時,才會處理請求
spring.mvc.static-path-pattern=/resources/**,
假定採用默認的配置埠,那麼只有請求地址類似於「http://localhost:8080/resources/jquery.js」時,Spring Boot才會處理此請求,處理方式是將根據模式匹配後的文件名查找本地文件,那麼應該在什麼地方查找本地文件呢?這就是「spring.resources.static-locations」的作用了。
2. 「spring.resources.static-locations」
「spring.resources.static-locations」用於告訴Spring Boot應該在何處查找靜態資源文件,這是一個列表性的配置,查找文件時會依賴於配置的先後順序依次進行,默認的官方配置如下:
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
繼續以上面的請求地址為例,「http://localhost:8080/resources/jquery.js」就會在上述的四個路徑中依次查找是否存在「jquery.js」文件,如果找到了,則返回此文件,否則返回404錯誤。
3. 靜態資源的Bean配置
從上面可以看出,「spring.mvc.static-path-pattern」與「spring.resources.static-locations」組合起來演繹了nginx的映射配置,如果熟悉Spring MVC,那麼理解起來更加簡單,它們的作用可以用Bean配置表示,如下:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/public-resources/")
.setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
}
}
或者等同與以下的XML。
<mvc:resources mapping="/resources/**" location="/public-resources/">
<mvc:cache-control max-age="3600" cache-public="true"/>
</mvc:resources>
結論
「spring.mvc.static-path-pattern」用於闡述HTTP請求地址,而「spring.resources.static-locations」則用於描述靜態資源的存放位置。

Ⅶ 如何實現Spring的靜態文件訪問控制

public class UserResourcesInterceptor extends HandlerInterceptorAdapter {

private List<String> urls;

@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {

String url = request.getRequestURI();

if (user.isLogin())
return true;
else {
if(urls.contains(url))
throw new AccessDeniedException();
}
return true;
}

}

Ⅷ 關於springMVC中靜態文件路徑問題

<!-- 對靜態資源文件的訪問,方案一 將無法mapping到Controller的path交給default servlet handler處理
<mvc:default-servlet-handler/> -->

<!-- 對靜態資源文件的訪問 方案二 -->
<mvc:resources mapping="/images/**" location="/WEB-INF/views/front/images/" />
<mvc:resources mapping="/css/**" location="/WEB-INF/views/front/css/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/upload/**" location="/upload/" />
<mvc:resources mapping="/frame/**" location="/frame/" />
<mvc:resources mapping="/slidebox/**" location="/WEB-INF/views/front/slidebox/" />

Ⅸ 如何讓springmvc對靜態資源文件的訪問使用文件系統的相對路徑

<!-- 自動掃描的包名 -->
<context:component-scan base-package="com.app,com.core,JUnit4" ></context:component-scan>

<!-- 默認的註解映射的支持 -->
<mvc:annotation-driven />

<!-- 視圖解釋類 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/><!--可為空,方便實現自已的依據擴展名來選擇視圖解釋類的邏輯 -->
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>

<!-- 攔截器 -->
<mvc:interceptors>
<bean class="com.core.mvc.MyInteceptor" />
</mvc:interceptors>

<!-- 對靜態資源文件的訪問 方案一 (二選一) -->
<mvc:default-servlet-handler/>

<!-- 對靜態資源文件的訪問 方案二 (二選一)-->
<mvc:resources mapping="/images/**" location="/images/" cache-period="31556926"/>
<mvc:resources mapping="/js/**" location="/js/" cache-period="31556926"/>

<mvc:resources mapping="/css/**" location="/css/" cache-period="31556926"/>

Ⅹ springmvc的靜態資源為什麼被攔截

"mvc:annotation-driven" 的前綴 "mvc"未綁定
辦法:這是我在spring-servlet.xml文件里使用<mvc>開頭的標簽時,忘記引入了命名空間。在xml的beans裡面加入如下代碼即可
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd

靜態文件訪問,主要是讓DispatcherServlet不攔截以下靜態資源
<mvc:annotation-driven />
<mvc:resources location="/image/" mapping="/image/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>

<!-- 啟動掃描所有的controller -->
<context:component-scan base-package="com.peidw.web"/>
<mvc:annotation-driven/>

<!-- 對特定路徑攔截-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/MyHome"/>
<mvc:mapping path="/um/*"/>
<bean class="com.peidw.web.interceptor.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>

java代碼里實現攔截
package com.peidw.web.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.*;
/**
* Created by peidw on 2015/3/6.
*/

@Component
public class HelloInterceptor implements HandlerInterceptor{
private static Logger logger=Logger.getLogger(HelloInterceptor.class);

@Override
public boolean preHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o) throws Exception {
logger.info("執行Action前先判斷許可權");
return true;
}

@Override
public void postHandle(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, ModelAndView
modelAndView) throws Exception {
logger.info("生成視圖前,可以改model和視圖");
}

@Override
public void afterCompletion(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, Exception e) throws
Exception {
logger.info("生成視圖前,可以改model和視圖");
}
}

package com.peidw.web;

import com.peidw.web.interceptors.HelloInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
* Created by peidw on 2015/3/6.
*/

@Configuration
@EnableWebMvc
public class WebApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HelloInterceptor()).addPathPatterns("/hello");;

}
}

<!-- 全局攔截 -->
<mvc:interceptors>
<bean class="com.peidw.web.interceptor.MyInterceptor"></bean>
</mvc:interceptors>

一些常用註解
@Autowired和@Qualifier 自動注入[根據類型注入]
@Autowired 可以對成員變數、方法以及構造函數進行注釋,
@Qualifier 的標注對象是成員變數、方法入參、構造函數入參。
ps:兩者結合使用相當於@Resourcede效果。
@Resource 自動注入[根據名稱注入],可寫參數name=""
@Controller 表示控制器
@Service 表示業務處理層[一般在serviceImpl]
@Repository 表示持久層[一般在Impl]
@Component 當你的類不清楚是哪一層的時候使用該註解
@ResponseBody 非同步返回數據類型為json
@RequestMapping 路徑,請求類型等設置
@InitBinder 數據綁定
@RequestBody、@ModeleAttributes、@SessionAttributes等

熱點內容
電腦網路ip設置緩存 發布:2025-08-27 16:41:33 瀏覽:124
排隊叫號源碼 發布:2025-08-27 16:28:50 瀏覽:853
冪數運演算法則 發布:2025-08-27 16:25:37 瀏覽:695
為什麼魔法覺醒伺服器載入不出來 發布:2025-08-27 16:24:53 瀏覽:675
奕歌哪個配置最好看 發布:2025-08-27 16:12:39 瀏覽:603
美能達ftp掃描沒有文件 發布:2025-08-27 15:54:42 瀏覽:159
昂科威plus應該買哪個配置 發布:2025-08-27 15:52:52 瀏覽:947
fdisklinux 發布:2025-08-27 15:52:48 瀏覽:948
ipad連接id伺服器出錯要怎麼辦 發布:2025-08-27 15:48:15 瀏覽:727
電腦室伺服器嗎 發布:2025-08-27 15:41:30 瀏覽:302