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" />
Related
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.
I just add this in the head of my _Layout.cshtml.
<head>
<link rel="shortcut icon" type="image/ico" href="~/Images/favicon.ico">
</head>`
the icon is showing but only in homepage, what is the reason for that?
In general, it is a good practice to put all favicon-related files in the root directory of the web site. Some browsers will always check for favicon.ico in the root folder.
<head>
<link rel="shortcut icon" type="image/ico" href="~/favicon.ico">
</head>
Also, take a look at these posts
Is putting your favicon.ico file in a non-root path a bad idea?
Serving favicon.ico in ASP.NET MVC
Rather than moving the favicon.ico file into the root, you can easily add a rewrite rule using the IIS IRL rewrite module, which will make it act like it is in the root directory even though it is where you would rather keep it.
Simply install the rewrite module, and drop this into your root web.config file.
<configuration>
<system.webServer>
<rewrite>
<rule name="Rewrite favicon.ico location from root to physical">
<match url="^favicon\.ico$"/>
<action type="Rewrite" url="images/favicon.ico"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Of course, you can also specify the actual location for the file in your _Layout.cshtml file for those browsers that will respect the shortcut icon tag.
<head>
<link rel="shortcut icon" type="image/ico" href="#Url.Content("~/Images/favicon.ico")">
</head>
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 ?
I have a Java web application with struts2 framework. in my index.jsp file, I link the CSS file, but it doesn't show in my page and Error 404: not found display in Firebug. I tested one application without Struts and everything is fine. but when i add struts2 library and config struts.xml and web.xml file, CSS doesn't display :(
here is my index.jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="styles/main.css" rel="stylesheet" type="text/css"/>
</head>
Of course, I add <%# taglib prefix="s" uri="/struts-tags" %> and tried <s:url> in href, but don't see any effect.
here is web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<display-name>HotelSystem</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
and here is part of my struts.xml:
<package name="default" namespace="/" extends="struts-default">
<action name="index.jsp" class="Actions.Test">
<result name="success">/index.jsp</result>
</action>
</package>
Note: Action.Test is a test action and always return SUCCESS.
You should map the .css type in the web.xml file as following!
<filter-mapping>
<filter-name>expires-filter</filter-name>
<url-pattern>*.css</url-pattern>
</filter-mapping>
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.