0

Having issues with the DispatcherServlet, I do believe the problem is in one of the xmls but being new at Spring I'm not being able to identify it.

HotelController.java (src\main\java\hotelbooking\controller)

@Transactional
@Controller
public class HotelController {

    @Autowired
    HotelService hotels;

    @RequestMapping(value="/hotels", method=RequestMethod.GET)
    public String index(Model model) {
        System.out.println("test");
        model.addAttribute("hotels", hotels.findAllHotels());
        return "allhotels";
    }
}

web.xml (src\main\webapp\WEB-INF)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

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

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

    <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/spring/appServlet/mvc-dispatcher-servlet.xml</param-value>
           </init-param>
           <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
         <servlet-name>mvc-dispatcher</servlet-name>
         <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml ()

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>hotelbookings</name>
<groupId>CIAI_T1</groupId>
<artifactId>hotelbookings</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.M5</version>
</parent>

<properties>
    <hibernate.version>4.3.6.Final</hibernate.version>
    <mysql.connector.version>5.1.31</mysql.connector.version>
    <joda-time.version>2.3</joda-time.version>
    <testng.version>6.9.4</testng.version>
    <mockito.version>1.10.19</mockito.version>
    <h2.version>1.4.187</h2.version>
    <dbunit.version>2.2</dbunit.version>
    <java.version>1.8</java.version>
</properties>

<dependencies>
<!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
            <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency> 
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>

    </dependency>

    <!-- jsr303 validation -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
    </dependency>

    <!-- MySQL -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- Joda-Time -->       
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>

    <!-- To map JodaTime with database type -->      
    <dependency>
        <groupId>org.jadira.usertype</groupId>
        <artifactId>usertype.core</artifactId>
        <version>3.0.0.CR1</version>
    </dependency>

    <!-- Servlet+JSP+JSTL -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>          
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>


    <!-- Testing dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>${testng.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
         <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dbunit</groupId>
        <artifactId>dbunit</artifactId>
        <version>${dbunit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.4.RELEASE</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin> <!-- enable java 1.7 -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>          
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>org.jboss.repository.releases</id>
        <name>JBoss Maven Release Repository</name>
        <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
    </repository>
    <repository>
        <id>spring-milestone</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
    <pluginRepository>
        <id>spring-milestone</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

mvc-dispatcher-servlet.xml (src\main\webapp\WEB-INF)

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jee
        http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<context:component-scan base-package="hotelbooking.controller"/>
<mvc:annotation-driven />

<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/views/" />
    <property name="suffix" value=".jsp"/>
</bean>

</beans>

I have the views in: "\src\main\webapp\WEB-INF\views" and there I have a file called "allhotels.jsp", I'm trying to open the page with "http://localhost:8080/hotels" (though I'm not sure that's correct).

On front of the file names above is their relative path.

I'm using Spring boot with Maven. If you need me to post anything else please let me know.

LoganMzz
  • 1,597
  • 3
  • 18
  • 31
Aterin
  • 557
  • 1
  • 6
  • 13
  • If https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp/src/main/webapp/WEB-INF/jsp is right, maybe put it in `\jsp` instead of `\views`? – zapl Oct 11 '15 at 21:59
  • @zapl Sorry I forgot to include the mvc-dispatcher-servlet.xml. I tried changing the folder name and the xmp to jsp but it didn't work. – Aterin Oct 11 '15 at 22:13

2 Answers2

1

I could see you have used two different names for DispatcherServlet while configuring it in web.xml

<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/spring/appServlet/mvc-dispatcher-servlet.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
</servlet>

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

Do the below changes in your 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:p="http://www.springframework.org/schema/p"
   xmlns:jee="http://www.springframework.org/schema/jee" xmlns:context="http://www.springframework.org/schema/context" 
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd      http://www.springframework.org/schema/jee   http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
   http://www.springframework.org/schema/mvc      http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> 

<context:component-scan base-package="hotelbooking.controller"/>
<mvc:annotation-driven />

</beans>

I feel it's more about web.xml and spring configuration file issue, try using the above modified one and try once.

The Url pattern should be like below

http://hostname:port/<webapplication-name>/<controller-path> http://localhost:8080/hotelbookings/hotels (In your case try this assuming 'hotelbookings' as web application name.)

Uppicharla
  • 494
  • 8
  • 18
  • Thanks for your answer but still the same. Just to make sure, the web app name is the artifactid/name on the pom.xml? – Aterin Oct 12 '15 at 07:28
  • Ah right, it is indeed hotelbookings then. – Aterin Oct 12 '15 at 07:42
  • Indeed I am, specifically the error is: `No mapping found for HTTP request with URI [/hotelbookings/hotels] in DispatcherServlet with name 'dispatcherServlet'.` – Aterin Oct 12 '15 at 07:48
  • Still nothing. (I had to add `xmlns:context="http://www.springframework.org/schema/context"` to the beans) – Aterin Oct 12 '15 at 08:06
  • Still nothing. Does the `pom.xml` need a dependency for the DispatcherServlet? and is the controller well defined? – Aterin Oct 12 '15 at 08:26
  • Sorry I really am new at Spring, when you say "using clean", are you talking about the Maven goals? If so I'm already using `spring-boot:run clean`, as for the temp directory, could you please let me know where that is since I don't actually have a server folder in my project. – Aterin Oct 12 '15 at 08:41
1

you might be missing this part in your mvc-dispatcher-servlet.xml file

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

i think i see the problem. You've used value instead of path. Try using this:

@RequestMapping(path="/hotels", method=RequestMethod.GET)
niewierny
  • 11
  • 2
  • Thanks for your answer but I added the line and it didn't help, I think if that were the problem the app wouldn't start due to dependency issues. I did decide to leave `` for future proofing. – Aterin Oct 12 '15 at 07:36
  • Just noticed your edit. The examples I found all used `value`, regardless, I tried with `path` but it still doesn't work. – Aterin Oct 12 '15 at 09:22
  • fields 'path' and 'value' are the same behind the scenes – Suhail Jain Nov 17 '20 at 14:11