I know that there are other topics about adding resources(CSS, Javascript) in Spring MVC, but in my case that doesn't work. I have this error when adding: HTTP Status 404 The requested resource is not available.
mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/"
My application works properly until this point.
In my index.jsp I've got:
link type="text/css" rel="stylesheet" href="c:url value="/resources/css/test.css"
script type="text/javascript" src="c:url value="/resources/js/carousel.js"
I had a similar problem and I think I solved it by adding a default mapping in web.xml
<servlet-mapping>
<servlet-name>dispatherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Problem solved.
In DispatcherServlet I added :
<mvc:default-servlet-handler />
<mvc:annotation-driven />
At this point 'HTTP Status 404 The requested resource is not available.' has disappeared, but my resources were not available at all. So i found another way to solve the problem, configuring the base tag like
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath %>">
<title>index</title>
<link href="resources/css/test.css" rel="stylesheet" type="text/css" />
</head>
...
And now it works properly.
Related
I am new to Jira and I have created one application and her I have used atlassian-scanner and also I have included angular project everything is working but when any rest request is getting called from main.js it is not working, not sure what is happening is it not considering assets folder or what please help.
Here is my below code for xml web-resource:
<?xml version="1.0" encoding="UTF-8"?>
<bundles>
<web-resource key="client-resources" name="Client Resources">
<resource name="assets/" type="download" location="client/assets/" />
<resource name="main.js" type="download" location="client/main.js" />
<context>com.ngdemo.client-resources</context>
</web-resource>
</bundles>
and my index.html code as below:
<!DOCTYPE html>
<html lang="en">
<head>
<base href="/jira/plugins/servlet/demo/" />
<meta name="decorator" content="atl.general" />
<meta charset="UTF-8">
<title>Angular Demo</title>
$webResourceManager.requireResourcesForContext("com.ngdemo.client-resources")
</head>
<body>
<app-root></app-root>
</body>
</html>
the url which is wrong it is as below:
http://localhost:2990/jira/plugins/servlet/demo/assets/i18n/en.json
but instead I think it should be as below like main.js:
http://localhost:2990/jira/s/d41d8cd98f00b204e9800998ecf8427e-CDN/-ufur03/713000/b6b48b2829824b869586ac216d119363/1.0.0-SNAPSHOT/_/download/resources/com.demo.ngdemo:client-resources/main.js
http://localhost:2990/jira/s/d41d8cd98f00b204e9800998ecf8427e-CDN/-ufur03/713000/b6b48b2829824b869586ac216d119363/1.0.0-SNAPSHOT/_/download/resources/com.demo.ngdemo:client-resources/assets/i18n/en.json
I'm new to spring framework and I was able to run my index.html from my localhost but all resources won't show. Below is my folder structure
- src/main
- webapp
- resources
- css (All css files)
- fonts (All font files)
- images (All image files)
- js (All js files)
- static
- index.html (my index.html)
- WEB-INF
My index.html page:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta charset="utf-8" />
<title>My First Application</title>
<meta name="description" content="overview & stats" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />
<!-- bootstrap & fontawesome -->
<link rel="stylesheet" href="/resources/css/bootstrap.min.css" />
<link rel="stylesheet" href="/resources/font-awesome/4.5.0/css/font-awesome.min.css" />
.........
In the "href" I tried changing the directory path multiple times, the page is loading but just a bunch of texts (No css, image, etc)
Do I also need to update my servlet-context.xml or web.xml?
EDIT:
I checked servlet-context.xml and the mapping already exists for resoucres:
<mvc:resources mapping="/static/**" location="/static/" />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
I even added:
<resources mapping="/resources/css" location="/resources/css" />
<resources mapping="/resources/fonts" location="/resources/fonts" />
<resources mapping="/resources/images" location="/resources/images" />
but still won't work.
EDIT 2:
When I go to chrome - inspect - console. This is the log:
springmvc:13 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/font-awesome/4.5.0/css/font-awesome.min.css
springmvc:21 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace.min.css
springmvc:18 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/fonts.googleapis.com.css
springmvc:26 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace-skins.min.css
springmvc:27 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace-rtl.min.css
springmvc:36 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/ace-extra.min.js
springmvc:2149 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery-2.1.4.min.js
springmvc:2159 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/bootstrap.min.js
springmvc:2166 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery-ui.custom.min.js
springmvc:2167 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.ui.touch-punch.min.js
springmvc:2168 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.easypiechart.min.js
springmvc:2169 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.sparkline.index.min.js
springmvc:2170 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.flot.min.js
springmvc:2171 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.flot.pie.min.js
springmvc:2172 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.flot.resize.min.js
springmvc:12 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/bootstrap.min.css
springmvc:18 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/fonts.googleapis.com.css
springmvc:13 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/font-awesome/4.5.0/css/font-awesome.min.css
springmvc:21 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace.min.css
springmvc:26 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace-skins.min.css
springmvc:27 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/css/ace-rtl.min.css
springmvc:36 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/ace-extra.min.js
springmvc:2149 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery-2.1.4.min.js
springmvc:2159 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/bootstrap.min.js
springmvc:2166 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery-ui.custom.min.js
springmvc:2167 GET http://localhost:8080/$%7BpageContext.request.contextPath%7D/resources/js/jquery.ui.touch-punch.min.js
springmvc:2180 Uncaught ReferenceError: jQuery is not defined
at springmvc:2180
Basically, all the lines in my html that has the:
<link rel="stylesheet" href=
Throws an error.
I tried to change the "href" to:
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/ace-skins.min.css" />
But still the same, css, images, etc won't load.
You need to register Resource Handler in spring to serve static content you can do it by XML way as
or
<mvc:resources mapping="/resources/**" location="/resources/" />
Java config
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
}
you can also add multiple resource location as
.addResourceLocations("/resources/","classpath:/other-resources/");
use commat to delimit the string and you can provide as many resource location as you want!
Enjoy coding!
Try using relative links like below. Also use Thymeleaf's th:href
<link rel="s`tylesheet" type="text/css" media="all"
href="../../../resources/css/styles.css"`
th:href="#{/resources/css/styles.css}" />
See your deployed directory structure and ensure required files are present in said folders (resources)
Remember to change the relative path ../../ to correct level.
If files not being copied to /resources folder then trying putting them in /main/src/webapp/resources
I am struggling with spring mvc file structure.
I am able to display an image ressource in a jsp but not to link a css to it, even if they are at the same location !
Here is my file structure :
/src
/main
/resources
/webapps
/resources
main.css
logo.png
/views
somePage.jsp
/WEB-INF
web.xml
dispatcher-servlet.xml
....
Here is what I have in my dispatcher :
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
And in my somePage.jsp :
<img src="<c:url value="/resources/logo.png" />" alt="blabla" />
<link href="<c:url value="/resources/main.css" />" rel="stylesheet" />
...
The image load correctly but not the css...
Note : I have the following message in eclipse : : "invalid location of tag (link). " about the css link...
If you have any idea, thank you by advance !
Finally it seems to be a cache issue.
I just delete my chrome cache and my css appeared.
When I compiled the first time, my css was empty and I filled it later.
It seems that even after editing it and re-running the project the css state ,wasn't refreshed.
chrome probably kept my css state as empty and didn't detect the update .. weird.
You add some code. like this.
Your servelet-context.xml
<mvc:default-servlet-handler />
Your jsp
<c:set var="contextPath" value="<%=request.getContextPath()%>"></c:set>
<link href="${contextPath}/resources/main.css" rel="stylesheet" />
I have a Spring project where I included the following webjars into pom.xml:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7-1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.1.1</version>
</dependency>
Then I included in my HTML view the following link and scripts:
<link rel="stylesheet" href="#{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />
<script src="#{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script src="#{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
But it didn't work, no mapping found:
[org.springframework.web.servlet.PageNotFound] (default task-15) No mapping found for HTTP request with URI [/TestPublicWeb-0.0.1-SNAPSHOT/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css] in DispatcherServlet with name 'testapp'
...so I tried to include the following mapping into servlet.xml:
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>
But with this, mapping for my /TestApplication is not found:
[org.springframework.web.servlet.PageNotFound] (default task-13) No mapping found for HTTP request with URI [/TestApplication/] in DispatcherServlet with name 'testapp'
How should webjars be included in a Spring project in a correct way?
The problem is that you are mixing the standard HTML href tag with Thymeleaf's syntax #{}. Change it as follows:
<link rel="stylesheet" th:href="#{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}" />
<script th:src="#{/webjars/jquery/3.1.1/jquery.min.js}"></script>
<script th:src="#{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
And if you are using Spring Security you need also specify the authorization to your webjars in the configure(HttpSecurity http) method like:
http.authorizeRequests().antMatchers("/webjars/**").permitAll();
A couple things I could think of as to why the above is not working for you. Make sure you are using spring mvc namespace inside of your servlet.xml file. Looks something like:
<bean xmlns:mvc="http://www.springframework.org/schema/mvc"....
Also not sure why you are trying to access assets using #{..} when you have specified the classpath. Try removing the #{..} like so:
<link rel="stylesheet" href="/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css" />
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js"></script>
I have been testing some request mapping in Spring MVC, and I came across a strange situation in my application. I decided to create a simple cenario so that you can understand my problem. I will first show you the details of my project (the source), and then I'll get to my question.
I have the following directory structure in my project:
+webapp
+WEB-INF
+recursos
+estilos
test.css
+spring
fronte-beans.xml
+views
+testes
page1.jsp
page2.jsp
web.xml
My Tomcat deployment descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.4">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>fronte</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring/fronte-beans.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fronte</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
My application context for DispatcherServlet:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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 ">
<mvc:annotation-driven />
<mvc:resources mapping="/recursos/**" location="/WEB-INF/recursos/" />
<context:component-scan base-package="com.regra7.minhaapp.contro" />
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My controller class for page1.jsp:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
#RequestMapping(value="/path")
public class TestController
{
#RequestMapping(value="/to/something")
public String getPage()
{
return "testes/page2";
}
}
My page1.jsp:
<!DOCTYPE html>
<%# page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<html lang="pt-BR">
<!-- ############################################### -->
<!-- HEADER -->
<!-- ############################################### -->
<head>
<title>Test Page</title>
<meta name="author" content="RDS" />
<meta name="description" content="? ? ?" />
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="recursos/estilos/test.css" media="all" />
</head>
<!-- ############################################### -->
<!-- BODY -->
<!-- ############################################### -->
<body>
<h1>PAGE 1</h1>
<p>This is a test, p1.</p>
<p>This is a test, p2.</p>
<p>This is a test, p3.</p>
CLICK TO PAGE 2
</body>
</html>
I can access page1.jsp and page2.jsp smoothly, but the CSS file of page2.jsp ends up not being found. The following text is printed on my console:
dez 29, 2014 8:16:22 PM org.springframework.web.servlet.PageNotFound noHandlerFound
WARNING: No mapping found for HTTP request with URI [/testeSpringMvc/path/to/recursos/estilos/test.css] in DispatcherServlet with name 'fronte'
For what reason "/path/to" is being included in the resulting path? If I try to add different combinations of mapping, both class or method-level, the same thing happens. However, if I map the link to a query string (URL) as follows, the file is found without problems...
page1.jsp:
CLICK TO PAGE 2
Controller:
#Controller
#RequestMapping(value="/")
public class TestController
...
#RequestMapping(params="cd=page2")
public String getPage()
What's happening? How can I set a path I want so that my pages use and find the necessary resources? I'm trying to separate pages in different paths so that I can apply the security features from Tomcat (security constraints).
NOTE: I've tried using contextPath to help me in setting the CSS file path, but nothing worked. In fact, the situation worsened because Page1.jsp also turned out not having stylization:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}recursos/estilos/test.css" media="all" />
As always, thank you for your attention and time.
Same was happening to me, and I found a solution. I hope this can help someone else:
As you see in the error message, paths you use for resources /testeSpringMvc/path/to/recursos/estilos/test.css are trying to be resolved by Spring's DispatchServlet.
This happens because in your web.xml file you have the following:
<servlet-mapping>
<servlet-name>fronte</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Your fronte servlet will try to resolve anything that starts with /.
Just add a simple extension to you <url-pattern>. Something like <url-pattern>/*.html</url-pattern> should do the job.
I just found the answer to my problems. I will leave here the solution, but unfortunately I did not understand how it solves the seen scenario.
I have found that this can be solved with JSTL. I read the JSTL documentation, and all I found was this description:
Creates a URL with optional query parameters.
If reference to the CSS file is changed by the following sentence, the problem will be solved:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<link rel="stylesheet" type="text/css" href="<c:url value="/recursos/estilos/test.css" />" media="all" />
If any moderator see this and know the explanation of how this is resolved, I kindly ask you to expose it here. Edit my answer, or comment it out, please. I'm sure other people can have the same doubt as me in the future.
Thank you all for the attention and time.
Take a look at this blog entry. The relevant passage is:
If I want to apply main.css to url: www.mysite.com/index.html, I need
to use following construction in HTML code:
< link href="resources/css/main.css" rel="stylesheet" type="text/css"/ >
If I want to apply main.css to url: www.mysite.com/some/location.html,
I need to use following construction in HTML code:
< link href="../resources/css/main.css" rel="stylesheet" type="text/css"/ >
As a possible workaround, perhaps this might work:
<mvc:resources mapping="/**/recursos/**" location="/WEB-INF/recursos/" />
You can access resource as given below.
<link rel="stylesheet" type="text/css" href="/recursos/estilos/test.css" media="all" />
you are missing / at the beginning