Reputation: 29
I have a spring MVC project: here are my files
web.xml
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
dispatcher-servlet
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:default-servlet-handler />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
MainController.java
@Controller
@RequestMapping(value="/test")
public class MainController {
@RequestMapping(value="/home")
public ModelAndView home()
{
String view="home";
ModelAndView modelAndView=new ModelAndView(view);
return modelAndView;
}
}
home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
</head></html>
My webaps hosts with URL
localhost:8080/projectName/test/home
but it doesn't load my static resource i.e. bootstrap.css. it should work because I have put my static resource at proper location webapp/resources/css/bootstrap.css
Q: why my static resource is being routes from controller path.. why its showing 404 not found.??
localhost:8080/projectName/test/resources/css/bootstrap.css
why controller path "test" is appending with URL when spring wants to look-up static resource.
Upvotes: 2
Views: 1832
Reputation: 3748
to avoid url problems use JSTL's <c:url
tag OR ${pageContext.request.contextPath}
for example in your case:
<link href='<c:url value="/resources/css/bootstrap.css"/>'
rel="stylesheet" type="text/css" />
OR
<link href='${pageContext.request.contextPath}/resources/css/bootstrap.css'
rel="stylesheet" type="text/css" />
Note: for first example import JSTL tag library in top of jsp before use. like:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Upvotes: 1
Reputation: 4649
<mvc:resources mapping="/resources/**" location="/resources/" />
Cuz the configuration of "/resources/" point to the "resources" directory of WebRoot
, so the problem becomes clear, just use
<link href="<%= request.getContextPath() %>/resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
to reference the static resources.
Besides, there is also another way to solve this problem, that is to configure the base tag like following,
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath %>">
<title></title>
<link href="resources/css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
....
both will work fine, hope you can help:)
Upvotes: 0
Reputation: 29
I made it run.. My home.jsp should have to include my static resources like this:
"../" prefix of resources makes controller route to honor static resources.
Upvotes: 0