0

I have configured a very simple Spring MVC project where instead of index.jsp file welcome page I have configured the welcome page in the controller. But I don't know why I've tried everything on running the project the error is resource is not available.

Can anyone please help.

Here are the files 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>Pet Clinic</display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>

    <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>

    </web-app>

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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:component-scan base-package="com.petclinic.controller"/>


    <!-- View Resolver Configured -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/view/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

Controller

package com.petclinic.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ClinicController {

    @RequestMapping("/")
    public String welcomeHandler(HttpServletRequest request)
    {
        System.out.println("Inside Controller");
        System.out.println(request.getServletPath());
        return "welcome";
    }

}

The code seems pretty right. Would really appreciate the help. Thanks On running the app the flow is not going to the controller even as I see nothing in the console

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
underdog
  • 4,447
  • 9
  • 44
  • 89
  • The welcome page has to be a actual page on the file system, it cannot be a reference to the servlet. So you will have to use an index.jsp which then redirects to the servlet. – M. Deinum Nov 24 '13 at 14:14
  • But in the Spring PetClinic sample code the welcome page is the reference & it runs fine. Besides its true it will work fine with redirect too – underdog Nov 24 '13 at 14:25
  • Well not exactly. It has no welcome files configured and due to the fact the `DispatcherServlet` is configured as the default servlet (it is mapped to /) everything (if nothing else can handle it) will pass through it. Due to the configuration of the `DispatcherServlet` (/ is a mvc:view-controller which maps to the welcome view.) It works. But it might (and probably will ) fail in older servlet containers (we had some problems with it). But indeed it doesn't always has to be a file. – M. Deinum Nov 24 '13 at 15:23
  • Is there any message in the servlet container (tomcat?) log file (exception for example)? – Ralph Nov 24 '13 at 20:45
  • Which url do you invoke? – Ralph Nov 24 '13 at 20:47
  • No message or exception, The flow in not going to the controller. I invoked http://localhost:8080/appname – underdog Nov 25 '13 at 06:11
  • what about http://localhost:8080/appname/ ? – Michał Rybak Nov 25 '13 at 10:45
  • @MichałRybak: It's a reply to Ralph :) When I am running the localhost url it is not going to the controller. The error is resource is not available. – underdog Nov 25 '13 at 12:16
  • sorry, I wanted to write "does localhost:8080/appname/ work?" (note the trailing slash - it was cut out by SO formatter in my previous comment) – Michał Rybak Nov 25 '13 at 14:08
  • Yes I remember I didn't worked too, anyways I put an index.jsp with a c:redirect. It worked. Thanks guys for the reply. I really appreciate it. – underdog Nov 25 '13 at 14:25

0 Answers0