0

I am facing a problem with the loading of images, CSS and themes in Spring MVC.
When adding the resources, I'm getting an error such as No mapping found for HTTP request with URI

I am using Spring 4.1.4 RELEASE running on Java 1.8 and Tomcat 8.

Here is the project Structure.

Project Structure

Web.xml

<?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/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringHibernate</display-name>  
   <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <!--   <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/SpringHibernate-servlet.xml</param-value>
        </init-param> -->
        <load-on-startup>1</load-on-startup>
    </servlet>  

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml
                     /WEB-INF/spring-security.xml
        </param-value>
    <!--     <param-value>/WEB-INF/config/SpringHibernate-servlet.xml</param-value> -->
</context-param>


<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
         <url-pattern>*.html</url-pattern> 
        <url-pattern>/</url-pattern>
    </servlet-mapping>

   <welcome-file-list>
    <welcome-file>loginPage.jsp</welcome-file>    
  </welcome-file-list>  



  <filter>
      <filter-name>springSecurityFilterChain</filter-name>
      <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
      <filter-name>springSecurityFilterChain</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>



</web-app>

mvc-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath:resources/database.properties" />
    <context:component-scan base-package="com.wbview" />

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

    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver" id="viewResolver">  
    <property name="viewClass">  
    <value>  
        org.springframework.web.servlet.view.tiles3.TilesView  
    </value>  
     </property>  
</bean>  
<bean class="org.springframework.web.servlet.view.tiles3.TilesConfigurer" id="tilesConfigurer">  
    <property name="definitions">  
     <list>  
         <value>/WEB-INF/tiles.xml</value>  
     </list>  
      </property>  
</bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>
<mvc:resources mapping="/resources/**" location="/resources/" /> 
<!--    <mvc:resources mapping="/theme/**" location="/theme/" />
    <mvc:resources mapping="/script/**" location="/script/" /> 
 -->
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <!-- class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean"> -->
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.wbview.model.Employee</value>
                <value>com.wbview.model.UL</value>              
            </list>         
        </property>
        <property name="mappingResources">
            <list>
                <value>com/wbview/model/ul.hbm.xml</value>                          
            </list>         
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
                <prop key="maxwait">10000</prop>
                <prop key="maxidle">25</prop>
                <prop key="minidle">5</prop>            
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

</beans>

A small part of logincontroller

@Controller
public class LoginController {

    @Autowired
    private LoginService loginService;


    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView welcome1() {
        return new ModelAndView("mflogin");
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public ModelAndView welcome() {
        return new ModelAndView("mflogin");
    }

    @RequestMapping(value="/validateLogin", method = RequestMethod.POST)
    public ModelAndView validateLogin(@RequestParam("userId") String userId, HttpSession session) {     
        String sessionId = session.getId();
        session.setAttribute("userId", userId);
        session.setAttribute("sessionId", sessionId);
        System.out.println("User Id : 1 : "+userId +" : 1 : "+sessionId);
        boolean loginStatus= loginService.setUserLoginEntries(userId, sessionId, "L");
        if (loginStatus) 
            return new ModelAndView("welcome");
        else
            return new ModelAndView("mflogin"); 
    }


<%@ page import="org.apache.log4j.Logger,java.util.regex.*,java.net.*" errorPage="errorPage.jsp"%>

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<style> 
#loginBox {
    /*margin: 0.7in auto;*/    
    width: 60%;
    padding: 25px;
    text-align: center;
    background-color: #F3F2EE;
    border: 3px solid #fff; 
    vertical-align: middle;   
}
#loginBox p {
  padding:0;
  margin:2ex 0;
}
</style>
<script type="text/javascript" src="resources/script/login.js"></script> 
<script type="text/JavaScript">
  curvyCorners.addEvent(window, 'load', initCorners);  
  //window.onload=initCorners; 
  function initCorners() {
    var settings = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: { radius: 10 },
      br: { radius: 10 },
      antiAlias: true
    }


    curvyCorners(settings, "#loginBox");
    break_out_of_frame();    
    //document.loginform.userId.focus(); 
    setTimeout(function(){document.getElementById('userId').focus();},300);                 
  }

  var activebtn = "submitbtn";
    window.document.onkeydown = function(e) {
        if (!e){ var e = window.event; }      
        // Enter is pressed     
        if (e.keyCode == 13) 
            { 
                 if(activebtn=="submitbtn"){
                     submit_form();;
                 }               
            }        
    }

    function greenBtn(obj){
        if(obj.value.length>0){
            document.getElementById("loginBtn").innerHTML = '<a class="ovalbuttongreen" href="javascript:submit_form();" tabIndex="3" style="text-decoration:none;color:black;width:80px"><span>Login</span></a>';
        }
    }
    function clearForm(){
        document.loginform.userId.value='';
        document.loginform.pswd.value=''
    }
    function submit_form(){
        //document.loginform.userId.value='';
        //document.loginform.pswd.value=''
        alert(document.loginform.userId.value);
        document.loginform.action="validateLogin.html";
        document.loginform.submit();
    }
</script>
<form:form id="loginform" name="loginform" method="post" action="validateLogin" >

<div class="rcorn_box2" style="background-image:url('resources/img/globe_background.jpg');"><br><br><br>
    <table border="0" cellspacing="0" cellpadding="0" width="100%" align="center">          
            <tr><td align="center" valign="middle">
                <div id="loginBox">
                    <table cellspacing="0" cellpadding="0" width="100%" align="center" border="0">
                        <tr><td bgcolor="#E8EAE2">
                            <table cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                    <td bgcolor="#E8EAE2" align="left" width="15%"><img src="resources/img/CitiGroupWebView_LogoV2.gif" alt="" border="0"></td>
                                    <td bgcolor="#E8EAE2" width="85%">
                                        <I><B><FONT face='Garamond (W1), Century, Times New Roman ,serif' color=1D5898 size=6>Login</FONT></B></I>
                                    </td>
                                </tr> 
                            </table>                            
                        </td></tr>                      
                        <tr><td>                            
                            <input id="sortorder" type="hidden" name="sortorder" value="msg"> 
                            <input id="sortOrderType" type="hidden" name="sortOrderType" value="asc"> 
                            <input id="cmd" type="hidden" name="cmd" value="validateLogin">
                            <TABLE bgColor=white border=0 cellPadding=2 cellSpacing=0 WIDTH='615'>
                            <TR>
                                <TD width=15%>&nbsp;</TD>
                                <TD width=15%>&nbsp;</TD>
                                <TD width=10%>&nbsp;</TD>
                                <TD width=10%>&nbsp;</TD>
                                <TD width=30%>&nbsp;</TD>
                            </TR>                           
                            <TR>
                                <TD class="CB" colspan="5">

                                </TD>
                            </TR>                           
                            <TR>
                                <TD colspan=5 align="center">&nbsp;</TD>
                            </TR>

                            <TR>
                                <TD>&nbsp;</TD>
                                <TD>&nbsp;</TD>
                                <TD align="left" nowrap>User ID &nbsp;</TD>
                                <TD align="left"><Input type="text" name='userId' id='userId' maxlength='8' style="width:100px" value='<%-- <% if (request.getParameter("userId")!= null) { out.println(filterInputString(request.getParameter("userId"))); }%> --%>'></TD>
                                <TD>&nbsp;</TD>                             
                            </TR>
                            <TR>
                                <TD>&nbsp;</TD>
                                <TD>&nbsp;</TD>
                                <TD align="left" nowrap>Password &nbsp;</TD>
                                <TD align="left"><input type="password" name='pswd' id='pswd' maxlength='12' style="width:100px" onKeyPress="greenBtn(this);" value='<%-- <% if (request.getParameter("pswd")!= null && sPasswordExpired.equals("Y")) { out.println(request.getParameter("pswd")); }%> --%>'></TD>
                                <TD>&nbsp;</TD>
                            </TR>                           
                            <TR>
                                <TD>&nbsp;</TD>
                                <TD align="left" nowrap>

                                </TD>
                                <TD align="left" nowrap>

                                </TD>
                                <TD align="left">

                                </TD>
                                <TD>&nbsp;</TD>
                            </TR>
                            <TR>
                                <TD>&nbsp;</TD>
                                <TD>&nbsp;</TD>
                                <TD align="left" nowrap>
                                    <div id=divPwdBlock3  name=divPwdBlock3 <%-- <% if (sPasswordExpired.equals("N")) { %> --%> style="visibility: hidden"<%-- <% } %> --%>>Retype Password &nbsp;</div>
                                </TD>
                                <TD align="left">
                                    <div id=divPwdBlock4 name=divPwdBlock4 <%-- <% if (sPasswordExpired.equals("N")) { %>  --%>style="visibility: hidden"<%-- <% } %> --%>>
                                        <Input type=password name='retypepswd' id='retypepswd' class='TS' style="width:100px" maxlength=12 size=14 value=''>
                                    </div>
                                </TD>
                                <TD>&nbsp;</TD>
                            </TR>                           
                            <TR>
                                <TD>&nbsp;</TD>
                                <TD>&nbsp;</TD>
                                <TD>&nbsp;</TD>
                                <TD align="left">
                                    <div class="buttonwrapper" id="loginBtn">
                                        <a class="ovalbutton" href="javascript:submit_form();" style="cursor:hand;text-decoration:none;color:black;width:80px"><span>Login</span></a>
                                    </div>
                                </TD>
                                <TD align="left">
                                    <div class="buttonwrapper" id="clearBtn">
                                        <a class="ovalbutton" href="javascript:clearForm();" style="cursor:hand;text-decoration:none;color:black;width:80px"><span>Clear</span></a>
                                    </div>
                                </TD>
                            </TR>
                            <%-- <%}else{%> --%>
                                <tr><td colspan="5" height="100px">&nbsp;</td></tr>
                            <%-- <%}%> --%>
                            <TR>
                                <%-- <% if (request.getAttribute("errMsg") == null) { %> --%>
                                    <TD colspan=5><br></TD>
                                <%-- <% } else { %> --%>
                                    <TD colspan=5 class="LB"><%=request.getAttribute("errMsg")%><br></TD>
                                <%-- <% } %> --%>
                            </TR>                           
                            <TR>
                                <TD colspan=5 align="center">&nbsp;If you are experiencing any difficulties, please reference our <a target="_blank" href="ReportServlet?cmd=sharePoint">Sharepoint Site</a>
                                    <br><br>Also visit our <a target="_blank" href="ReportServlet?cmd=sharePoint">Sharepoint Site</a> for access instructions, training, and request forms
                                </TD>
                            </TR>
                            <TR>
                                <TD colspan=5 align="center"></TD>
                            </TR>
                            <TR>
                                <TD colspan=5 align="center">
                                    <a target="_blank" href="https://globalconsumer.collaborationtools.consumer.citigroup.net/sites/webview/Web%20View%20Training1/AboutWebView.pdf">About Web View</a>&nbsp;&nbsp;&nbsp;
                                    <a target="_blank" href="https://globalconsumer.collaborationtools.consumer.citigroup.net/sites/webview/Password%20Reset/How%20to%20%20reset%20your%20Web%20View%20password.pdf">Password Reset</a>
                                </TD>
                            </TR>
                            <TR>
                            <TD colspan=5 align="center"><font color="#B5B6CF"> 
                            <p align="justify" style="padding-left:10px;padding-right:10px">Please be aware that the files you are requesting contains sensitive information and will be written to your temporary 
                            internet file cache on your workstation and is accessible offline from that location. Please clean up your temporary internet cache for security reasons.
                            </p></font></TD>
                            </TR>                                                       
                        </table>
                            <font color="#5D5D5D"> 
                            <p align="justify" style="padding-left:10px;padding-right:10px">You are authorized to use this System for approved business purposes only. Use for any other purpose is prohibited. All transactional records, reports, email, 
                            software and other data generated by or residing upon this System, to the extent permitted by local law, are the property of Citigroup Inc. or one of its subsidiaries
                             or their affiliates (individually or collectively ' Citigroup ') and may be used by Citigroup for any purpose authorized and permissible in your country of work. Activities on this System are monitored to the extent permitted by local law.
                            </p></font>                     
                        </td></tr>
                    </table>
                </div>
                </td>           
            </tr>           
    </table><br><br><br>
</div>
</form:form>

The Main Problem is when iam not including the resources in mvc-dispatcher-servlet.xml i.e. My page is getting loaded with no images or themes or css.

the console logs are as below

08/18/2015 22:15:19.192|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/script/curvycorners.src.js] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.228|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/theme/buttons.css] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.249|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/img/banner_1.jpg] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.312|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/theme/tab_support.css] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.328|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/resources/img/fitiGroupWebView_LogoV2.gif] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.343|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/img/f_cglogo11.gif] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:19.359|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/resources/script/login.js] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:21.241|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/img/spacer.gif] in DispatcherServlet with name 'mvc-dispatcher'
08/18/2015 22:15:21.867|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/resources/img/globe_background.jpg] in DispatcherServlet with name 'mvc-dispatcher'

when iam defining the resources path in the mvc-dispatcher-servlet.xml iam getting error as below

HTTP Status 404 - 


type Status report

message 

description The requested resource is not available.

--------------------------------------------------------------------------------

Apache Tomcat/8.0.20

in Console as 08/18/2015 22:32:23.817|;|PageNotFound|;|No mapping found for HTTP request with URI [/SpringMVCDemo/] in DispatcherServlet with name 'mvc-dispatcher'

I have tried with changing url-pattern

<url-pattern>/</url-pattern>

to 

<url-pattern>/*</url-pattern>

but no success .

Can anyone please help me iam struck

cbmeeks
  • 11,248
  • 22
  • 85
  • 136
Learner
  • 945
  • 11
  • 23
  • 38
  • 1
    You application is too complex. First strip it down without database reference so that others could try to compile and test it. Anyway, you are using the same xml file for Spring root context and mvc context. **Never do that**. You will get different instances of bean in each context, and will never be sure of which will be used. – Serge Ballesta Aug 19 '15 at 06:11
  • I dint get the Point of using the same xml file for Spring root context and mvc context. can you please give me the example so that i can differentiate in the two different xml's. – Learner Aug 19 '15 at 10:24
  • You explicitely initialize you root context with `mvc-dispatcher-servlet.xml`. And your servlet is called `mvc-dispatcher` and has no explicit `ContextConfigLocation` parameter, so it looks for a `mvc-dispatcher-servlet.xml` file to initialize the child context. So all beans declared in that file will end in both the root and the child context. – Serge Ballesta Aug 19 '15 at 10:39

3 Answers3

0

You should map resources in your spring config mvc-dispatcher-servlet.xml. There are two ways to do it:

  1. First it declare

    <mvc:default-servlet-handler/>

  2. Second is use

    <mvc:resources mapping="/script/**" location="/script/"/> <mvc:resources mapping="/resources/script/**" location="/resources/script/"/>

and etc. for every resource folder(/resource/img/, /img/, /theme/, /resources/img/, /resources/script/)

  • I have made the changes as suggested or .... no progress still iam getting HTTP Status 404 - /SpringMVCDemo/ there are no console logs this time – Learner Aug 19 '15 at 01:33
0

why not try adding your root context to your image name example and dont forget to map your resources like gennady said

style="background-image:url('SpringMvcDemo/resources/img/globe_background.jpg');"

hope it helps

Robert Ellis
  • 714
  • 6
  • 19
0

Just remove the url pattern "*.html " from the servlet mapping. I hope this resolves the issue.

<servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>
J.P
  • 495
  • 1
  • 6
  • 15
  • Sorry I have tried with removing the Pattern but still no success.... i don't understand why after including the resources it will throw the Http 404 Error.. but when i comment out the resources the page will load with no images :( – Learner Aug 19 '15 at 12:54