1

I have the following files:

web.xml:

<web-app 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_3_0.xsd"
     version="3.0">



<display-name>To do List</display-name>

<servlet>
 <servlet-name>dispatcher</servlet-name>
 <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
 </servlet-class>
 <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/tk-servlet.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
</servlet>

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

LoginController:

package de.yellowsub.tk.mvc;

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

@Controller
public class LoginController {

    @RequestMapping(value = "/login")
    @ResponseBody
    public String sayHello() {
        return "Hello World!";
    }
}

Now if I put in localhost:8080/login in the URL I can see "Hello World" in the Browser but not if I write localhost:8080/spring-mvc/login

Any ideas?

Also here is the tk-servlet.xml if it's any use:

<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/bean/spring-beans.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="de.yellowsub" /> //I also tried "de.yellowsub.*"

    <mvc:annotation-driven />

</beans>
t-MURO
  • 642
  • 5
  • 10
  • didn't work ... – t-MURO Nov 21 '17 at 20:14
  • What are you using to run the application? Standalone Tomcat? Tomcat in you IDE? Other? To me it is very strange that the web.xml is not processed, are you sure, you are hitting this application when excluding the context-path? I have copied your code and it is working for me with spring-mvc context path. – helospark Nov 21 '17 at 20:15
  • It's my first time working with Spring/Maven/Tomcat so I don't understand everything yet. What do you mean with excluding the content path? I'm using Spring Tool Suite. So i press on start Spring app and it does eveything for me – t-MURO Nov 21 '17 at 20:36
  • @VanDeckel By context-path I meant the "spring-mvc" part, that should be required for every request. Do you click on "Run on server" in STS? Or do you by any chance made a Spring Boot app (in which case many of the configs you are doing is not needed/used)? – helospark Nov 21 '17 at 20:54
  • Yes it does work without the /spring-mvc/! Yes I made a Spring boot app! thanks a lot for helping by the way!! – t-MURO Nov 21 '17 at 21:02
  • @VanDeckel If you are using a Spring Boot app you do not need web.xml (and will not be used if you create one), also your tk-servlet.xml will not be used either (you can delete it). You can add "server.contextPath=/spring-mvc" to your application.properties (in src/main/resources) to set the context path. Also please find a different tutorial, because pure Spring based MVC application is totally different from Spring Boot web application. – helospark Nov 21 '17 at 21:07
  • Damn, you saved me a lot of time. We have a project at school/uni where we make a Spring Boot Application (not necessarily web app) and I got the task of making the UI (plus the communication between ui and app). So I thought doing it through a browser (with HTML/CSS) might be a good idea. Where would I put the HTML/CSS/JS code if all these xml files are not needed? In the tutorial he put it in /WEB-INF/views, making a connection in the web.xml and adding prefix and suffix in the config xml – t-MURO Nov 21 '17 at 21:17
  • I can't go into much detail in the comments, but you can add static files (css, js, html) to `src/main/resources/static` and it will be automatically served. To manually serve please search how to define a ViewResolver in Spring Boot. Also I have posted an answer about context-path, consider accepting it, if it solved your current problem. – helospark Nov 21 '17 at 21:27
  • Thanks for everything, going to watch other tutorials :) – t-MURO Nov 21 '17 at 21:57

2 Answers2

0

You NEED TO fix the root context for your web application (spring-mvc) to fix the URL

If you are using Maven, you can fix your war name by adding the below tag into the pom.xml:

<build>
    <finalName>spring-mvc</finalName>
</build>

Then you should be able to access your application controller always with below url :

http://localhost:8080/spring-mvc/login

if you dont use maven follow this link How to set the context path of a web application in Tomcat 7.0

AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • I put finalName into the build tag but it didn't work. The tutorial I used also doesn't have it :/ – t-MURO Nov 21 '17 at 20:08
0

Based on your comment you have generated a Spring Boot application. That is quite different from a Spring MVC application, you do not need web.xml or tk-servlet.xml to configure it. You can delete both.

You can add server.contextPath=/spring-mvc to your application.properties (create it in src/main/resources) to set the context path.

Also please find a different tutorial, because pure Spring based MVC application is totally different from Spring Boot web application.

helospark
  • 1,420
  • 9
  • 12