Spring not accessing JSP inside /WEB-INF/jsp/ folder

2.9k views Asked by At

I am new to Spring MVC and developing an application. I developed a project with the help of Spring MVC Tutorial.Initially, it was working. But as soon as I upgraded my project with Hibernate 4 and Spring 4. It stopped working. Whenever, I am invoking any \WEB-INF\jsp, I am getting 404 Error.

Files

LoginController

 public class LoginController {
        @RequestMapping(value="/login", method=RequestMethod.GET)
        public ModelAndView showLoginPage(String path, ModelMap model){
            ModelAndView mv = new ModelAndView("login");
            model.addAttribute("welcome to Flight Services", "welcomeMessage");
            return mv;
        }
    }

Spring-servlet.xml

<beans ......-mvc-4.0.xsd">
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>
</beans>

applicationContext.xml

        <beans xmlns="http://www.springframework.org/schema/beans"  xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
        xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:util="http://www.springframework.org/schema/util"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
            http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
             http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <context:annotation-config/>
    <mvc:annotation-driven />

        <bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
            p:location="/WEB-INF/jdbc.properties">
        </bean>
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}">

        </bean>

        <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation">
             <value>classpath:hibernate.cfg.xml</value>
             </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">${jdbc.dialect}</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>     
        </bean>
            <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

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

web.xml

<display-name>AirportService</display-name>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

index.jsp

        <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
        <%@include file="/WEB-INF/jsp/include.jsp" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
        <c:redirect url="/home" /> 
    </body>
    </html>

Now everytime, I am launching my index page. I am getting this error

    org.apache.jasper.JasperException: /index.jsp (line: 3, column: 5) File "/WEB-INF/jsp/include.jsp" not found
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:133)
    org.apache.jasper.compiler.Parser.processIncludeDirective(Parser.java:322)
    org.apache.jasper.compiler.Parser.parseIncludeDirective(Parser.java:357)
    org.apache.jasper.compiler.Parser.parseDirective(Parser.java:467)
    org.apache.jasper.compiler.Parser.parseFileDirectives(Parser.java:1779)
    org.apache.jasper.compiler.Parser.parse(Parser.java:135)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
    org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:119)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:193)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
3

There are 3 answers

1
holtc On

You are trying to load some file "/WEB-INF/jsp/include.jsp", that does not exist. In your index.jsp file you have the line:

<%@include file="/WEB-INF/jsp/include.jsp" %>

According to the error logs, this file does not exist. Are you sure it is in the correct directory?

0
Ashish On

Anyways, I downgraded spring and Hibernate versions to 3.x and it is working. Seems like I am missing a trick here with version 4.

0
Bhaskara On

If i am correct, your index.jsp is outside your WEB-INF and the resource you are trying to access is inside WEB-INF. Remember that the resources outside your WEB-INF (like images, HTMLs and other static HTML stuff) cannot access the content present in WEB-INF. So you cannot do <%@include file="/WEB-INF/jsp/include.jsp" %>

You can do this:

<%@include file="include.jsp" %> where both include.jsp and index.jsp are in the same location within or outside WEB-INF.

(Optional) In your web.xml

replace <url-pattern>/</url-pattern> to <url-pattern>*.htm</url-pattern> .htm can be replaced with any extension that you are interested in