Spring mvc: resource tag and 404 error

I am trying to use the <mvc:resources>

this is my project structure

enter image description here

i want to access javascript in js folder and css in styles folder
however, whenever I add this to my dispatcher-servlet.xml

 <mvc:resources mapping="/resources/**" location="/"/> 

I got a 404 error when starting the project (the project will be redirected to login.htm, which is the login.jsp page)

this is my dispatcher-servlet.xml code

 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <context:component-scan base-package="com.swcommodities.wsmill.controller"/> <mvc:resources mapping="/resources/**" location="/"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/" p:suffix=".jsp" /> <tx:annotation-driven/> . . . </beans> 

this is web.xml

 <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> 

I don’t know why I delete the line <mvc:resources... , the project works correctly, but when I add this to the project, I immediately get the 404 error. I think this conflicts with the current project.

+6
source share
3 answers

The answer is here:

Using <mvc: resources ... /> in spring 3 all other types stop working

Quote from the author: " <mvc:resources> requires <mvc:annotation-driven> (or explicitly declared handler mappings, etc.).

It helps me.

+14
source

You need to set the full path in the location attribute, for example:

 <mvc:resources mapping="/resources/js/**" location="/js/" /> 

One separate note, I would recommend that you combine all the resources into a single folder, for example, β€œassets”, because in your current setup, an attacker can gain access to something under WEB-INF /, which is clearly not what you want .

Let me know if this works.

Ayman

+1
source

since resource files are in fold js and styles, you need to change the mapping to:

 <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/styles/**" location="/styles/" /> 

and upload them to jsp:

 <script type="text/javascript" src="/js/file.js"></script> <link type="text/css" href="/styles/file.css" /> 
-1
source

Source: https://habr.com/ru/post/952255/


All Articles