Mapping Resources in dispatcher servlet (Resin Server) - java

This is my dispacter-servlet.xml file. When i deploy the project on resin pro 4.0.36 it loads my index page and the content BUT FAILS to load css files and images stored under the staticresources folder
<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-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.dogears" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<import resource="classpath:beans.xml"/>
<mvc:resources mapping="/resources/**" location="/staticresources/" />
<mvc:annotation-driven />
Please can anyone tell me how to mapp my static resources folder, so that whenever the request is of the patter /resources/* it redirects the request to the static resources folder. the staticresources folder is in MyspringProject/src/main/webapps directory.

I believe you need to map your staticresources directory as a root directory. In Eclipse that would be a "Source Folder" and in IntelliJ a "Resources Root".
Refer to the following help docs depending on your IDE:
Eclipse: http://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-wizard-source-folder.htm
IntelliJ: https://www.jetbrains.com/webstorm/help/configuring-folders-within-a-content-root.html

Suppose your project directory structure is like
- src
- main
- webapp
- resources
- css
- abc.css
- images
- xyz.jpg
& your .html or .jsp pages are located in the directory as below
- webapp
- index.jsp
- pages
- welcome.jsp
then you can add link to your resources in index.jsp page with the URL BaseURL/index.jsp
<link href="resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
& to welcome.jsp with URL BaseURL/pages/welcome.jsp as
<link href="../resources/css/abc.css" rel="stylesheet" type="text/css" />
<body>
<img src="../resources/images/xyz.jpg" alt="" style="width:100px;height:100px" />
</body>
Hope this helps you.

I finally found my solution. I had deployed my spring webapp in the webapps folder of the resin directory.
I realised that <mvc:resource /> mapping tag does not work when you have deployed your spring app on resin server instead of tomcat.
Hence i solved this by first creating a war file of my project and then extracted the war file on the desktop and later placed all the contents from the war file in the webapps/root (and not webapps folder) folder of resin directory. and then from my index page i used JSTL TAG to include the external stylesheet.
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<link rel="stylesheet" type="text/css" href="<c:url value="/staticresources/externalcss.css"/>">
</head>
<body>
<h2 class="text_style">Hello World!</h2>
</body>
</html>
IT WORKS !!!

Related

CSS file is not getting mapped/loaded in SPRING MVC program in eclipse

I'm very new to Spring mvc,for this question i've found many answers here but nothing is working for me and please help me to find out what mistake i'm doing here.
I have created a css file under WebContent>WEB-INF>resources>css>style.css and mapping is done under spring-servelet.xml and the code is
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<context:component-scan base-package="com.journaldev.spring" />
<!-- 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:bean>
<!-- Handles HTTP GET requests by efficiently serving up static resources
in the corresponding directory -->
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="resources/**" location="resources/" cache-period="31556926"/>
</beans:beans>
and i've called the css file from jsp and the jsp file is
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>welcome</title>
<!-- Custom Theme files -->
<link href="<spring:url value='resources/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<h1>Hello world!</h1>
<P>The time on the server is ${serverTime}.</p>
</body>
</html>
Now network is aborted error is displaying at console .As i told i'm newbie to this framework so please help me out other than giving a link to an answer because i've tried almost all.Thank you
Put your CSS folder inside webcontent and add below code servlet xml file
<mvc:resources mapping="/css/**" location="/css/" />
Now you will be able to take the CSS from JSP inside views folder using below code -
<link href="/css/yourCSS.css" rel="stylesheet" type="text/css" media="all" />
This should work otherwise let usknow!
So, you will need to do what vipin cp is suggesting. You need this:
<link href="<spring:url value='/css/style.css' />" rel="stylesheet" type="text/css" media="all" />
This is because the you are using the mapping mapping="/css/**" , so your value needs to begin with the same thing as your mapping "/css/...", without the beginning slash, it doesn't match your mapping so spring doesn't know where to map it too, you could also change your mapping to mapping="css/**". Which ever you choose, they just need to match each other.
Once they match Spring can then resolve your urls to what they need to be.
Also, just a suggestion for readability. Specify all your spring:url elements at the top with a var argument and then you can reference that with standard SpEl.
And example is:
<spring:url value='/css/style.css' var='styleCss'/>
So, that tells Spring that the styleCss variable references the resolved url from /css/style.css. You can then reference that variable like:
<link href="${styleCss}" rel="stylesheet" type="text/css" media="all" />
Obviously, up to you, but I have always found it more readable.
For what it is worth, this can also be done with your JS imports if you have any.

Java - Google App Engine - Encoding problems with UTF-8

I would like to add some characters with accents in my ".jsp" and ".html" but when I write those characters with accent in those files and load my pages:
"è" becomes "é"
"ç" becomes "ç"
"é" becomes "Ã "
Steps:
I have configured Eclipse for creating all files with UTF-8 encoding (I have checked: all .jsp and .html files in the /src or /target folder are now UTF-8).
None of my .jsp or .html files (inside /src or /target folder) explicitely contains those "é", "ç", "à ".
All my .jsp contains those lines at the beginning :
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta content="IE=edge" http-equiv="X-UA-Compatible" />
<meta content="width=device-width, initial-scale=1" name="viewport" />
<!-- ... -->
</head>
I have added those parameters in my appengine-web.xml:
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
<!-- Added below -->
<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />
</system-properties>
I have also tried to add this thing showed in the GAE documentation but my application throws Exception :
<env-variables>
<env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>
GAE documentation says (source) :
To avoid conflicts with your local environment, the development server does not set environment variables based on this file, and requires that the local environment have these variables already set to matching values. (This does not apply to system properties.)
So I have removed this part and added the following environment variable in my system: DEFAULT_ENCODING = UTF-8
Further more:
When I use firebug to see the network, it shows Content-Type text/html; charset=utf-8 for my loaded page.
Google's tool has already added the <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> in my pom.xml when I've generated my project.
Interesting fact:
When I load html code from my text files with AJAX, there is no problem, "é" or "ç" characters are ok. I use the BalusC's code ( and you can notice that he uses response.setCharacterEncoding("UTF-8"); so I don't know where the problem comes from).
On the development environment (my localhost), if I replace <%# page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %> by this <%# page pageEncoding="UTF-8" %> characters with accents are ok, but only on my localhost, then when I upload my project to Google App Engine servers it's still not ok, firebug doesn't show Content-Type text/html; charset=utf-8 anymore, and W3C Markup Validation Service tells me :
Internal encoding declaration utf-8 disagrees with the actual encoding of the document (windows-1252)
My configuration:
Maven 3.2
IDE: Eclipse Luna
OS: Windows 7
I ran into the same issue. My solution was to add at the top of my .jsp files that generate html the following tag:
<%#page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

Spring Boot - CSS not loading

I have a Spring Boot Application. I am trying to load to my HTML file a CSS file, but probably I am doing something wrong. In Spring MVC Project I've done something like this:
In HTML file I added the following line:
<link href="<c:url value="/resources/css/style.css" />" rel="stylesheet">
And in mvc-dispatcher-servlet.xml I added following code:
<mvc:resources mapping="/resources/**" location="/resources/"
cache-period="31556926"/>
<mvc:annotation-driven />
And everything worked perfect.
Now I'm using thymeleaf templating language and load my CSS file like that:
<link rel="stylesheet" type="text/css" th:href="#{/assets/css/style.css}" />
My project files hierarchy is following:
In current project I don't have WEB-INF directory so I don't have such a files like mvc-dispatcher-servlet.xml or web.xml so I don't know if I should or where I should paste that code with resources mapping. Im completely new to Spring so I apologise for my lack of knowledge.
Any tips and solutions will be much appreciated.

Icons not showing in Spring project with Bootstrap

I am utilising the Bootstrap front end framework, although I am having some trouble getting the icons to load. I have implemented Spring security, I am not sure whether this may be affecting them not to load. I am calling my .css and .js files like this:
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/style/bootstrap.css" />
<script src="${pageContext.request.contextPath}/static/js/jquery.js"></script>
This is the configuration that I have in my Dispatcher servlet config:
<mvc:resources location="/resources/" mapping="/static/**" />
This is really irritating, can someone please help. Thanks
In your spring security config, have you excluded your resources directory containing your css, js, images from the secured zone ?
<http use-expressions="true" security="none" pattern="/resources/**">
</http>
If the problem is only related to the twitter bootstrap icons, have you included the files glyphicons-halflings.png glyphicons-halflingswhite.png in a resources/img folder ?

static content rendering with spring mvc

I am new to spring and am trying it.
I have a jsp file which includes css and images as follows
<link href="css/style.css" rel="stylesheet" type="text/css" />
Now i have a url path as /localhost/myapp/test/tst which is mapped to a controller which then forwards it to view jsp
view.jsp is present it
/web-inf/jsp/view.jsp
But when hit the path /localhost/myapp/test/tst the css and images path gets resolved to
/localhost/myapp/test/tst/css/style.css
Actaully i want it to come as /localhost/myapp/css/style.css
css and html are present in root of webapp
How i have following in my spring config
<mvc:annotation-driven />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/css/**" location="/css/" />
Your CSS href is currently relative to your page, you need to make it relative to the server root by adding a leading slash (plus context, etc). Its also a good idea to add the contextPath so that if/when you change the webapp name or deploy it as a root webapp, the resources still work.
<link href="${pageContext.request.contextPath}/css/style.css" rel="stylesheet" type="text/css" />

Categories