Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I am working on a Spring-MVC application in which I have CSS, JS files and they are not loading although they are properly called. What am I doing wrong?

Resources folder :

Project --> webapp --> resources--> css, js directories

Config :

root-context.xml :

 <context:component-scan base-package="com.APP">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                destroy-method="close">
        <beans:property name="driverClassName" value="org.postgresql.Driver"/>
        <beans:property name="url"
                        value="jdbc:postgresql://IP:PORT/DBNAME"/>
        <beans:property name="username" value="DBUSER"/>
        <beans:property name="password" value="DBPASS"/>
        <beans:property name="removeAbandoned" value="true"/>
        <beans:property name="removeAbandonedTimeout" value="20"/>
        <beans:property name="defaultAutoCommit" value="false"/>
    </beans:bean>

    <!-- Hibernate 4 SessionFactory Bean definition -->
    <beans:bean id="hibernate4AnnotatedSessionFactory"
                class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource"/>
        <beans:property name="packagesToScan" value="com.APP.model"/>

        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">false</beans:prop>
                   <beans:prop key="hibernate.order_updates">true</beans:prop>-->
                <beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
                <beans:prop key="cache.use_second_level_cache">true</beans:prop>
                <beans:prop key="cache.use_query_cache">true</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

    <beans:bean id="LoginServiceImpl" class="com.APPName.service.LoginServiceImpl"/>

    <task:annotation-driven/>

    <tx:annotation-driven transaction-manager="transactionManager"/>

    <beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/>
    </beans:bean>

    <cache:annotation-driven />

    <beans:bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
        <beans:property name="caches">
            <beans:set>
                <beans:bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"
                            p:name="person"/>
            </beans:set>
        </beans:property>
    </beans:bean>



</beans:beans>

servlet-context.xml :

<security:global-method-security
            secured-annotations="enabled"
            jsr250-annotations="disabled"
            pre-post-annotations="enabled"/>

    <context:component-scan base-package="com.App" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <mvc:annotation-driven/>

    <mvc:default-servlet-handler/>

    <resources mapping="/resources/" location="/resources/"/>

    <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>

    <beans:bean class="com.App.service.DoNotTruncateMyUrls"/>

    <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <beans:property name="maxUploadSize" value="2147483648"/>
    </beans:bean>
    <!--  52428800 -->


</beans:beans>

security-applicationContext.xml :

  <security:http pattern="/resources/**" security="none"/>
    <security:http create-session="ifRequired" use-expressions="true" auto-config="false" disable-url-rewriting="true">
        <security:form-login login-page="/login" username-parameter="j_username" password-parameter="j_password"
                             login-processing-url="/j_spring_security_check" default-target-url="/dashboard"
                             always-use-default-target="true" authentication-failure-url="/denied"/>
        <security:remember-me key="_spring_security_remember_me" user-service-ref="userDetailsService"
                              token-validity-seconds="1209600" data-source-ref="dataSource"/>
        <security:logout delete-cookies="JSESSIONID" invalidate-session="true" logout-url="/j_spring_security_logout"/>
        <security:port-mappings>
            <security:port-mapping http="8080" https="8443"/>
        </security:port-mappings>
        <security:logout logout-url="/logout" logout-success-url="/" success-handler-ref="myLogoutHandler"/>

        <security:session-management session-fixation-protection="migrateSession">
            <security:concurrency-control session-registry-ref="sessionReg" max-sessions="5" expired-url="/login"/>
        </security:session-management>
    </security:http>

    <beans:bean id="sessionReg" class="org.springframework.security.core.session.SessionRegistryImpl"/>

    <beans:bean id="rememberMeAuthenticationProvider"
                class="org.springframework.security.web.authentication.rememberme.PersistentTokenBasedRememberMeServices">
        <beans:constructor-arg index="0" value="_spring_security_remember_me"/>
        <beans:constructor-arg index="1" ref="userDetailsService"/>
        <beans:constructor-arg index="2" ref="jdbcTokenRepository"/>
        <property name="alwaysRemember" value="true"/>
    </beans:bean>

    <beans:bean id="jdbcTokenRepository"
                class="org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl">
        <beans:property name="createTableOnStartup" value="false"/>
        <beans:property name="dataSource" ref="dataSource"/>
    </beans:bean>

    <!-- Remember me ends here -->
//Auth mechanisms
</beans>

Screenshot :

enter image description here

JSP :

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page session="true" %>
<html>

<body>
        <nav class="col-lg-5 col-md-5 col-sm-5">
            <ul class="pull-right">
                <li class="purple"><a href="#"><i class="icons icon-user-3"></i> Login</a>
                    <ul id="login-dropdown" class="box-dropdown">
                        <li>
                            <form id="form" action="<c:url value='/j_spring_security_check'/>"  method="POST">
                                <div class="box-wrapper">
                                    <h4>LOGIN</h4>

                                    <div class="iconic-input">
                                        <input type="text" placeholder="Username" name="j_username" id="j_username">
                                        <i class="icons icon-user-3"></i>
                                    </div>
                                    <div class="iconic-input">
                                        <input type="password" placeholder="Password" name="j_password" id="j_password">
                                        <i class="icons icon-lock"></i>
                                    </div>
                                    <input type="checkbox"  id="_spring_security_remember_me" name="_spring_security_remember_me"> <label for="_spring_security_remember_me">Remember me</label>
                                    <br>
                                    <br>
                                    <div class="pull-left">
                                        <input name="submit" type="submit" class="orange" value="Login">
                                    </div>
                                    <div class="pull-right">
                                        <a href="#">Forgot your password?</a>
                                        <br>
                                        <a href="#">Forgot your username?</a>
                                        <br>
                                    </div>
                                    <br class="clearfix">
                                </div>

                                <div class="footer">
                                    <h4 class="pull-left">NEW CUSTOMER?</h4>
                                    <a class="button pull-right" href="create_an_account.html">Create an account</a>
                                </div>
                            </form>
                        </li>
                    </ul>
                </li>
                <li><a href="#"><i class="icons icon-lock"></i> Create an Account</a></li>
            </ul>
        </nav>

        <c:url var="add" value="/displayproducts" ></c:url>
        <form:form action="${add}" commandName="product">
            <table>
                <tr>
                    <td>
                        <form:label path="productname">
                            <spring:message text="productname"/>
                        </form:label>
                    </td>
                    <td>
                        <form:input path="productname"  />
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="submit" type="submit" value="Submit" />
                    </td>
                </tr>
            </table>
        </form:form>




<c:url var="addAction" value="/user/add" ></c:url>
<form:form action="${addAction}" commandName="user">
<div class="row">

<div class="col-lg-12 col-md-12 col-sm-12 register-account">

<div class="carousel-heading no-margin">
    <h4>Register</h4>
</div>

<div class="page-content">
<div class="row">

    <div class="col-lg-12 col-md-12 col-sm-12">
        <p><strong>Tausch information</strong></p>
    </div>

    <div class="col-lg-4 col-md-4 col-sm-4">
        <p>E-Mail*</p>
    </div>

    <div class="col-lg-8 col-md-8 col-sm-8">
        <form:input type="text" path="email"/>
    </div>

</div>

<div class="row">

    <div class="col-lg-4 col-md-4 col-sm-4">
        <p>Username*</p>
    </div>
    <div class="col-lg-8 col-md-8 col-sm-8">
        <form:input type="text" path="username"/>
    </div>

</div>

<div class="row">

    <div class="col-lg-4 col-md-4 col-sm-4">
        <p>Password</p>
    </div>
    <div class="col-lg-8 col-md-8 col-sm-8">
        <form:input type="password" path="password"/>
    </div>

</div>


<div class="row">
    <div class="col-lg-12 col-md-12 col-sm-12">
        <input class="big" type="submit" value="<spring:message text="Register"/>">
        <input class="big" type="reset" value="Cancel">
    </div>

</div>
</div>
</div>
</form:form>




</body>
</html>
share|improve this question
    
Are you getting 404 or 401 for your requests? – aksappy Jul 13 at 9:38
    
@aksappy : 200, the page is loaded, but html, css and all is not, adding screenshot in 2 mins. – We are Borg Jul 13 at 9:39
    
I didnt found any css in your code.. – Ryan Jul 13 at 9:53
<link href="${pageContext.request.contextPath}/resources/css/bootstrap-datetimepicker.css" rel="stylesheet" />

Add ${pageContext.request.contextPath}/ to your link.

share|improve this answer
    
CSS code directly added in JSP is also not loading. – We are Borg Jul 13 at 9:42
    
could you add your jsp. – Ryan Jul 13 at 9:44
    
Added. Check it out. – We are Borg Jul 13 at 9:50
    
I am sure you are trying to build a login screen, So if you want responsive screen go to w3layouts.com . w3layouts.com/free-web-elements – Ryan Jul 13 at 9:56

Your code : <resources mapping="/resources/" location="/resources/"/>

Try : <resources mapping="/resources/**" location="/resources/"/>

OR : <mvc:resources mapping="/resources/**" location="/resources/"

share|improve this answer

Make sure all required CSS files are under resources folder as in security-applicationContext.xml file you have the following line:

<security:http pattern="/resources/**" security="none"/>

If you are correctly done that, for further debugging put the <debug> tag in your security-applicationContext.xml file and if you are using any logging framework (like log4j) enable the logging for "org.springframework.security" package at DEBUG level. The above configuration will enable the debug level logs of spring security.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.