0

I've been trying now for about a week to get a simple Spring hello world style project running without any luck.. so I'm hoping SO can help.

I'm trying to access this URL (localhost:8080/test/welcome) to display "Hello World!".

My aim is to use Spring annotations instead of configuring the beans via xml.

Currently when I access the above URL I get a HTTP Status 404 (the requested resource is not available) error.

These are the files I have:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0"
    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_3_0.xsd">
    <display-name>test</display-name>
    <!-- Spring MVC -->
    <servlet>
        <servlet-name>test-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>test-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/test-dispatcher-servlet.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

test-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="com.test.test.controllers"/>
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
</beans>

WelcomeController.java

package com.test.test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/welcome")
public class WelcomeController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(Model model) {

        return "welcome";
    }
}

welcome.jsp

<html>
<body>
<h2>Hello World!</h2>
</body>
</html>

pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>test</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>test</name>
    <build>
        <finalName>test</finalName>
    </build>
    <properties>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <org.springframework.version>3.2.3.RELEASE</org.springframework.version>
        <org.springframework.security.version>3.1.4.RELEASE</org.springframework.security.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
        <!-- Spring Dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework.version}</version>
        </dependency>
        <!-- Spring Security Dependencies -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-acl</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
            <version>${org.springframework.security.version}</version>
        </dependency>
    </dependencies>
</project>

My project structure: project-folder-structure

I just can't figure out why it isn't working and I'm not sure if I can even strip it down any further than I already have...

I have so many questions about Spring but I guess they will have to wait for another question. This experience so far has been more challenging than anything else I've done... and it's just to set it up! I can't wait to get to the actual programming!

Thanks for any help.

Mick
  • 665
  • 2
  • 6
  • 18
  • Change the url pattern to /* and try – Renjith Jul 11 '13 at 12:00
  • Does your web application container (Tomcat? Glassfish?) report the WAR as deployed? – Raedwald Jul 11 '13 at 12:02
  • @Renjith Thanks for unfortunately that didn't change anything. – Mick Jul 11 '13 at 12:17
  • @Raedwald Thanks, I think you might be on the right track.. I'm look at the eclipse console after I restart the tomcat server but I don't see anything that specifically says "deployed" or something similar. – Mick Jul 11 '13 at 12:25

1 Answers1

2

Is there a reason why you put your controller in the src/test/java folder? That folder is, as far as I know, skipped during packaging, so your WAR-file doesn't contain a controller anymore when you package it.

Create a folder called src/main/java. I notice you're using the Maven plugin so normally it will automatically be picked up as a source folder. If not, right click your project and go to Maven and then Update configuration (or something similar).

Now move the package com.test.test.controller to the src/main/java folder and build + deploy again.

g00glen00b
  • 41,995
  • 13
  • 95
  • 133
  • Thanks Dimitri, good catch.. but unfortunately that didn't solve the issue. The issue is something else it seems. I'm looking at Raedwald's comment at the moment, but I'm not sure how to check whether the app has deployed in eclipse using a tomcat server. I've looked in the console when starting the server and I don't see anything related to deploying the war file.. so maybe that is the right direction to look at. – Mick Jul 11 '13 at 12:16
  • Did you try using the command line: `mvn clean install tomcat:run` option already? I don't trust IDE and deployments anymore. Be sure to shut off your other Tomcat before deploying with the Maven command line since it will open its own Tomcat instance (at port 8080). – g00glen00b Jul 11 '13 at 12:18
  • I'm thinking about 10 hours this week has been dedicated to problems related to eclipse.. (I had another one earlier with eclipse defaulting to version 2.3 servlets). By the way THANKS. I ran a maven clean and re-ran the server on eclipse and lo and behold.. it now works! Now back to solving the original problem I was having using maven security annotations :) – Mick Jul 11 '13 at 12:29
  • No problem, I was quite sure the problem had to do with the `src/test/java` directory, and because it didn't work I thought it was a deployment issue. The `src/test/java` is only used for classes you need during the test phase (for example unit tests). You should never put actual code that must be deployed inside the `src/test` folders. – g00glen00b Jul 11 '13 at 13:04