Not able to load my resource files using spring mvc web app - spring-mvc

I am using spring MVC to run my dynamic web application. I am able to write one and run the app successfully. But problem is with loading the resource files like image,JS and CSS. I am getting file Failed to load resource. key point is i am placing the JSP files Outside WEB-INF and Using web logic server.
My web.xml file
<display-name>Support</display-name>
<welcome-file-list>
<welcome-file>resources/jsp/home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-servlet.xml,/WEB-INF/spring-jpa.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
my dispathcer servlet
<mvc:annotation-driven/>
<mvc:resources mapping="/resources/**" location="/resources/"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/resources/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
I have added this in my JSp file
<link media="screen" rel="stylesheet" href="<spring:url value='resources/css/Map.css' />" type="text/css" />
<spring:url value="resources/css/Mass.css" var="resourceUrl2"/>
<LINK media="screen" rel="stylesheet" TYPE="text/css" href="${resourceUrl2}" />
Getting the Failed to load resource: the server responded with a status of 404.
any Help???
Update:
What I found with my weblogic server is not able to get the context value for this app. so got the conext path in the jsp file and appended before resources. it worked for me
<link media="screen" rel="stylesheet" href="<%=request.getContextPath()%>/resources/css/MaRK.css" type="text/css" />
still don't know why spring resources did not work.

I ran into the same issue and this is how i resolved the issue:
In spring configuration file
Replace
<mvc:resources mapping="/resources/**" location="/resources/"/>
With
<mvc:default-servlet-handler/>
And provide the below path in the JSP to load static contents
<link rel="stylesheet" href="resources/css/Mass.css" type="text/css"/>
Hope this will help !

Try this
<mvc:resources mapping="/resources/**" location="resources/"/>
Works for me.

I had the following in my JSP, and ran into the same problem:
<link href="/css/styles.css" rel="stylesheet" type="text/css">
After I removed leading / from /css/styles.css it loaded.
<link href="css/styles.css" rel="stylesheet" type="text/css">
I can't explain why. The answers written above did not help me. In fact, I don't even have a resources folder at all, all of my static content is placed directly in the /webapp folder—and, specifically, in /css, /jsp, and /WEB-INF subdirectories.

Related

Include CSS in JSP page of Dynamic Web Application

My JSP pages in my dynamic web application (in Eclipse) are not being styled by my CSS code. I have included a stylesheet in index.jsp as follows:
index.jsp
<html>
<head>
<title>To Do List - Home</title>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/stylesheet.css">
</head>
<body>
Tasks
</body>
</html>
And my project structure is as follows:
I thought that href="${pageContext.request.contextPath}/css/stylesheet.css would look for stylesheet.css in ToDoList/WebContent/css/. If I try navigating directly to the stylesheet in the browser via http://localhost:8080/ToDoList/css/stylesheet.css it returns a 404 error.
I am aware this question has been asked before but from looking at the other questions I still can't figure out what is wrong with my project structure.
Update:
So I added <mvc:resources mapping="/css/**" location="/css/" /> to my servlet config, but now when I navigate to any page other than index.jsp I get a 404 error.
todolist-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scan for JavaConfig, annotated with #Configuration -->
<context:component-scan base-package="com.petehallw.todolist.main" />
<context:annotation-config/>
<mvc:resources mapping="/css/**" location="/css/" />
<!-- Configure Spring view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
The error I get is:
WARNING: No mapping found for HTTP request with URI [/ToDoList/tasks] in DispatcherServlet with name 'todolist'
This is upon clicking a link to "tasks" in index.jsp which was previously returning the tasks.jsp page.
For every URL in the app, you can make use of c:url tag in JSTL lib.
Tasks.
So, if you have controller method mapping #requestmapping("/tasks"), it will be invoked.
Or try as Tasks like you use at css files.
Important: You should add <mvc:annotation-driven /> in xml configuration for the support of annotation-driven MVC controllers like #RequestMapping, #Controller.

My css files don't get loaded

I'm going crazy why my css file isn´t getting loaded. I'm using spring mvc 4 + apache tomcat 8
I already use the same code for other projects and it works normally.
Here's my project structure
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>AO</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>hibernate.xml</param-value>
</context-param>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
my servlet context :
<context:component-scan base-package="controller"/>
<mvc:annotation-driven />
<mvc:resources mapping="/RESOURCES/**" location="/RESOURCES/" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
when he try to load :
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/RESOURCES/css/welcome.css" >
or
<link rel="stylesheet" type="text/css" href="/RESOURCES/css/welcome.css">
or
<link rel="stylesheet" type="text/css" href="/AO/RESOURCES/css/welcome.css">
i got this error:
GET http://localhost:8080/AO/RESOURCES/css/welcome.css 404 (Introuvable)
Try adding <mvc:default-servlet-handler /> to your mvc configuration file.
change the mapping for RESOURCES to LOWERCASE resources
Except the name of your Application,which can contain Uppercase elements, the path for resources must contain only lowercase
<mvc:resources mapping="/resources/**" location="/RESOURCES/" />
Then ref :
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/resources/css/welcome.css" >
Use c:url tag of JSP Standard Tag Library to make the URL relative.
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel=stylesheet href="<c:url value='/RESOURCES/css/welcome.css'/>"
type="text/css" />
I Delete the Apache tomcat server and I add it again and finally my CSS has been loaded :)
Can somebody explain me why ?

No mapping found for HTTP request with URI [/favicon.ico]

I can't recall what I have changed on my code but whenever I click on any links on my web it gives me this :
WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'mvc-dispatcher'
A portion of my web.xml
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter>
<filter-name>HttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
some configuration of my mvc-dispatcher-servlet.xml
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
<beans:property name="order" value="1" />
</beans:bean>
<!-- testing for pdf export -->
<beans:bean class="org.springframework.web.servlet.view.XmlViewResolver">
<beans:property name="location" value="/WEB-INF/spring-pdf-views.xml" />
<beans:property name="order" value="0" />
</beans:bean>
Other than that everything works fine, means any page is loaded correctly without any error. May I know what might cause this ? And who is using that .ico image ?
Most web browsers attempt to fetch a site's favicon at the root of the context with a request for the /favicon.ico resource automatically. In your case is not handled by any configured Spring mapping.
If you have a favicon at /favicon.ico or in another location you could configure a mapping in Spring to resolve the request to a valid resource:
<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />
The favicon.ico : icon in the browser tab
Since Spring Security are using
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Did you exclude the favicon.ico from filtering by Spring Security?
<http pattern="/favicon.ico" security="none" />
Try to locate favicon.ico in your html page as below:
<html>
<head>
<link href="[YOUR_FAVICON_PATH]/favicon.ico" rel="icon" type="image/x-icon" />
</head>
</html>

Spring-JS unable to locate web-resources

I am using Spring JS 2.3.0 with Spring 3.0.5 and believe I have configured the applicationContext.xml properly for resources (css and js files). Here are the snippets:
applicationContext.xml
<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
<mvc:default-servlet-handler />
The jsp page
<LINK rel="stylesheet" type="text/css" href="<c:url value="/resources/js/xwt/themes/reboot2/reboot2.css"/>">
<LINK rel="stylesheet" type="text/css" href="<c:url value="/resources/js/xwt/themes/reboot2/reboot2-xwt.css"/>">
<SCRIPT type="text/javascript" src="<c:url value="/resources/js/dojo/dojo.js"/>"></SCRIPT>
<script type="text/javascript" src="<c:url value="/resources/Spring.js" />"> </script>
<script type="text/javascript" src="<c:url value="/resources/Spring-Dojo.js" />"> </script>
web.xml
<servlet-mapping>
<servlet-name>Spring Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
When the jsp page is requested, the Spring.js and Spring-Dojo.js are not accessed while other resources under the app root are accessed. Btw, spring-js is under WEB-INF/lib folder. Firebug shows 404 for spring resources:
404 Not Found - http://localhost:8080/springmvc/resources/Spring.js
404 Not Found - http://localhost:8080/springmvc/resources/Spring-Dojo.js
Could some please point me in the right direction as to what I'm doing wrong?
you have to play with the web.xml in this case.
http://static.springsource.org/spring-webflow/docs/2.0.x/reference/html/ch11s02.html
If you have been using spring security, then you have to define the intercept url, like this:
<security:intercept-url pattern="/resources/**" filters="none" />

Spring and the /* url pattern

We have a url-pattern of "/*" and requests get to our controller, but we always get a 404.
Here is our web.xml
<servlet>
<servlet-name>bro</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:mo/config/mo-spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bro</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
mo-spring.xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix" value=".jsp"/>
</bean>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/views/" location="/views/" />
A bit of the controller:
#RequestMapping(value="/signon", method=RequestMethod.GET)
public String signon(HttpServletRequest request) {
...
return "/WEB-INF/index";
}
If i use /xxx/* as the url-pattern in my web.xml everything works as expected, but we have a dojo app that we really don't want to modify that wants to talk to /* and not /xxx/*
You have bro as the servlet name, but reference brio as the servlet name for your url mapping in server.xml
If that's not the problem and you are talking about wanting your app to respond to http://yourserver/* instead of http://yourserver/yourcontext/* then you need to deploy your webapp as the root webapp for the server. Here's a question relating to that kind of configuration in tomcat Tomcat 6: How to change the ROOT application
edit: copied from my comment - If you are mapping DispatcherServlet to root in your webapp you will need the default-servlet configuration mentioned in http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-default-servlet-handler

Resources