0
votes

Hi I am getting Http status 404 when I am trying to access an end point which should return me a view mapped with the end point.My application is starting fine as my health-check end point is working fine. Please find my code snippets below: I have already tried all possible suggestions on similar issues that I found on Stackoverflow but none of them seems to work.

My end point looks like this - http://localhost:8080/web/views/home, this should return the home.jsp but is returning http status 404.

Health check end point - http://localhost:8080/web/admin/health-check

web.xml

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

<display-name>Spring Demo</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>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      /WEB-INF/spring-servlet.xml
    </param-value>
  </context-param>

spring-servlet.xml

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


<context:component-scan base-package="cms.web, cms.service, cms.data"/>


<mvc:annotation-driven/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

HomeController.java

package cms.web.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/views")
public class HomeController {

@RequestMapping(value="/home", method = RequestMethod.GET)
public String getHomePage(){
    return "home";
 }
}

Health check controller -

package cms.web.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RestController
public class HealthCheckController {

/**
 * Returns a "200 OK" response if the service is running.
 *
 * @return String "200 OK"
 */
@RequestMapping( "admin/health-check" )
public String healthCheck()
  {
    return "OK";
  }
}

I have home.jsp inside WEB-INF/views/home.jsp.

1
Your WAR files dir structure please? The reason I'm asking is in your spring config, you mention ` <property name="prefix" value="/WEB-INF/views/"/>` and the HealthCheckController has the mapping as "admin/health-check". But your HomeController has it as "/views" (notice the leading '/' ? Could that be it? - Praba
I even tried by removing the leading / from my controller request mapping. It currently looks like this, @RequestMapping("views") I was able to debug the flow till my getHomePage() it runs fine till return "home" but fails to render my home.jsp. - sjaiswal
you're still getting a 404 or 500? And please paste the directory structure so we can see how your files are organized. - Praba
I am still getting 404. I am still getting 404. I am not able to paste my war directory structure since its not allowing me to attache any image in the comment. I can describe how it looks, inside root folder named web, i have WEB-INF, index.jsp and css folders, inside WEB-INF, i have web.xml spring-servlet.xml views lib classes and inside views I have home.jsp - sjaiswal
The issue got resolved :) changed the <url-pattern>/*</url-pattern> of the DispatcherServlet inside web.xml to <url-pattern>/</url-pattern>. The * was causing the issue. - sjaiswal

1 Answers

0
votes

The solution is to change the <url-pattern>/*</url-pattern> of the DispatcherServlet inside web.xml to <url-pattern>/</url-pattern>. After making this change you might start facing issues with your static css and js files not getting loaded. The solution for that would be to put them inside folder with any name for example. "static" inside WEB-INF and then specify <mvc:resources mapping="/static/**" location="/static/" /> in the spring-servlet.xml.

To include the static resources in your jsp use this for reference <link rel="stylesheet" href="<c:url value="/static/css/bootstrap.css" />" />"