0

I have a doubt about the correct creation of a Spring MVC project.

When I run an imported project (for example the Spring MVC Showcase example downloadable form the STS dashboard), inside the browser URL bar this project have an URL like:

http://localhost:8080/project-name/ 

(for example the Web MVC Showcase example have project name: "spring-mvc-showcase" and have the following URL:

http://localhost:8080/spring-mvc-showcase/ )

Now, my doubt is related to the creation of a new Spring MVC project using Spring Template in STS\Eclipse.

I do the following operation inside STS:

File --> New --> Project and appears to me a wizard where I can chose the type of project.

So I chose Spring Template Project, now appears to me ano other wizard windows where I can chose the specific typology of Spring Project and I chose "Spring MVC Project"

Ok, now appears to me an other wizard windows in which I have to set up:

Project name and I insert: my-spring-project

Top level package and I have insert: org.mycompany.foo

Ok, now I run my project on server, this don't run and inside the URL bar of the browser I have not

http://localhost:8080/my-spring-project/ (as I would expect) 

but I have:

http://localhost:8080/foo/

In the stacktrace I have the following error message:

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/foo/] in DispatcherServlet with name 'appServlet'

Then if I do a project clean of my project, STS rebuild the project and if I try to run it again the project run well and the URL address still remain:

http://localhost:8080/foo/

I think that this is a strange behavior and I have not ideas what is the reason about it...

So:

1) Why the URL is not

http://localhost:8080/my-spring-project/ 

?

2) Why I have to clean and rebuild my project to run it?

3) I am doing some error in the creation of a standard Spring MVC project?

Cœur
  • 37,241
  • 25
  • 195
  • 267
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

1

Eclipse > Properties > Web Project Settings : Set as Context Root my-spring-project Then run the project as you do normal within eclipse. Its should start as http://localhost:8080/my-spring-project/ . Also do the mapping as showcase project does : <mvc:view-controller path="/" view-name="home"/>

George Papatheodorou
  • 1,539
  • 19
  • 23
0

This is my web.xml file:

<?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">

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 3
    You should extend your question if you want to add some more details, and not post it as an answer! – Ralph Dec 03 '12 at 12:44
0

I think you have forgotten to deploy the new project on your server.

  • 1) Open the "Servers" view (Ctrl+3, type "servers")
  • 2) right click your Server instance (for example "tomcat 7 at localhost")
  • 3) select "Add and Remove..."
  • 4) Select "my-spring-project" from the "available" and "add>" them
  • 5) Start the server and access "localhost:8080/my-spring-project/"
Ralph
  • 118,862
  • 56
  • 287
  • 383