javacssspringjspspring-security

How to display images and link CSS files to JSP pages


I am working on a Spring, Hibernate, JSP application. I am trying to display an image on my JSP page. The image is not displayed in the browser. I am also not able to link my CSS into JSP page.

My JSP page is:

<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body id="page1">
<div class="headerlogo" align="center"><a href=""><img src="images/Logo1.png" alt="" /></a></div>
<div align="center">
    <h1><spring:message code="header.title"/></h1>
    Language :
    <a href="?lang=en_US">English</a>|
    <a href="?lang=sp_SP">Spanish</a>
    
    <h3>
        <a href="home"><spring:message code="header.gotohome" /> </a>
    </h3>
</div>
</body>
</html>

I have set

<http pattern="/images/**" security="none" />
<http pattern="/css/**" security="none" />

for my images and CSS folders

My Spring Security xml is:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" 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.1.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
   

    <http pattern="/images/**" security="none" />
    <http pattern="/css/**" security="none" />

    <http auto-config="true" use-expressions="true">
        
        <intercept-url pattern="/admin*" access="hasRole('SYS_ADMIN')" />
                
        <form-login login-page="/login" default-target-url="/role-check"
            authentication-failure-url="/login?error=true" />
        <logout logout-success-url="/login" />
        
         </http> 
         
         <authentication-manager> 
        <authentication-provider> 
        
        <jdbc-user-service data-source-ref="fmsDataSource"  
        users-by-username-query="select USERNAME,PASSWORD, 'true' as enabled from users where USERNAME=?"
        authorities-by-username-query="select u.USERNAME, ur.AUTHORITY from users u, user_roles ur where u.USER_ID = ur.USER_ID and u.USERNAME =? " />
    </authentication-provider>
    </authentication-manager>
</beans:beans>

Solution

  • I have resolved the issue by adding following mapping in web.xml file

    <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.css</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.js</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.gif</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.jpg</url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>default</servlet-name>
            <url-pattern>*.png</url-pattern>
        </servlet-mapping>
    

    and also by adding <%=request.getContextPath()%> in image src url. Like <img src="<%=request.getContextPath()%>/images/logo.png" />