Deploy and run Spring MVC App on local Tomcat Server
Hi everyone,
I'm currently learning the Spring Framework with a Udemy tutorial. The videos are all made with Eclipse, but I use IDEA..
The current lecture is a simple Spring MVC app, without a pom.xml.
I installed Tomcat locally on my Macbook and it runs smooth. But I don't know if my config in IDEA correct is.



Following the config 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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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">
<!-- Step 3: Add support for component scanning -->
<context:component-scan base-package="com.luv2code.springdemo.mvc" />
<!-- Step 4: Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Step 5: Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Following the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>spring-mvc-demo</display-name>
<!-- Spring MVC Configs -->
<!-- Step 1: Configure Spring MVC Dispatcher Servlet -->
<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/spring-mvc-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Step 2: Set up URL mapping for Spring MVC Dispatcher Servlet -->
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
And here is my Controller:
package com.luv2code.springdemo.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String showPage() {
return "main-menu";
}
}
I tried different things, also with building a war file first. But everytime I run int, I come to a 404 ...
If any information is missing, please let me know!
Thanks for your help!
Please sign in to leave a comment.
You need an artifact for deployment, see https://www.mkyong.com/intellij/intellij-idea-run-debug-web-application-on-tomcat/ .
See also https://stackoverflow.com/a/54715508/104891 regarding the deployment context configuration.
Thanks for your answer Serge!
Unfortunately I get now these bunch of errors:
But I don't understand which fragments are meant..
Please share the complete project to reproduce it: https://intellij-support.jetbrains.com/hc/articles/206869619.
https://stackoverflow.com/a/52325208/104891 may help. It could be that you are deploying Spring libraries twice.
I uploaded the file via HTTPS upload service. File name is
Thanks. You need to change a couple of things.
1. Delete index.jsp from the web resource root, otherwise it will override your servlet mapping.
2. Add the library you have into the artifact so that it's deployed with your web app:
See https://www.jetbrains.com/help/idea/artifacts.html for details.
I still get the same error.
Another question with this is, that I cannot see the war file which should be built.
Shouldn't it be in the out folder?
It's in exploded form, there should be no .war file, just a directory.
The project deploys and runs with no issues for me:
Check this zip: https://www.dropbox.com/s/omu68cft2yzb2f0/spring-mvc-demo.zip?dl=1.
Serge, thanks a lot! Now it works!!
I don't know why at the moment, maybe I played too much with the server config.. but it runs now :) Thank you soooo much!
I'm following this same course and my issue is that my server is up and running but i'm get Error 404 on the web page.
Here is the link to my project on google drive: https://drive.google.com/file/d/1AKx3xIM3YSngL13twu3Nn932suyqnjTR/view?usp=sharing
I hope you can help me!
Check these links:
https://stackoverflow.com/a/42688676/104891
https://stackoverflow.com/a/56050202/104891 .
The app should be available at http://localhost:8080/spring_mvc_demo_war_exploded
Hi Serge ,
I've the same issue and followed everything , still when i go to ( http://localhost:8080/h )
i get 404 error.
I've Uploaded the project via https, the File name is
spring-mvc-demo.zip
Thanks in advance for your time
Your project works fine with http://localhost:8080/h URL:
Note that you have http://localhost:8080/ in the Run Configuration which will return 404, make sure to change it to http://localhost:8080/h :
Ok , I have changed the Run Configuration to http://localhost:8080/h :
It's still giving me 404 error
Sorry, no more ideas. The project you uploaded works perfectly fine.
You may want to investigate Tomcat logs, IDE logs and build logs to check if there are any related errors.
https://stackoverflow.com/a/45666862/104891
https://intellij-support.jetbrains.com/hc/articles/207241085 .
Ok thank you
I'm following the same tutorial in Udemy So far I got the MVC welcome page loaded onto Tomcat Following Serge Baranov's indications. So thank you Serge
I'm following the exact same Udemy tutorial and had a similar issue.
On URL http://localhost:8080/spring-mvc-demo/ I saw only the text
$END$
on my page.
Based on the hint I found here that IntelliJ generates web/index.jsp containing this:
If you look up the template for new JSP files in the Other section of the File Templates, you will see that it is indeed coming from there.
You should delete this file and all should be working as normal. Maybe we got to hint to Chad Darby that a lot of the Java Devs are using IntelliJ and this is such an easy thing to warn for.
Known issue, please check these links:
https://stackoverflow.com/questions/54712942/deploy-servlet-with-intellij-idea-to-local-tomcat-server/54715508#54715508
https://youtrack.jetbrains.com/issue/IDEA-146643 .
Serge, thanks a lot! :) I read your answers and now my first web-app works!
Any one still looking for a solution , here is what worked for me .
Thanks Serge Baranov - I'm also following this tutorial and your help got me up and running. I'm glad to be able to use IntelliJ for this :-)
Finding this 10 months from the last post and just wanted to say:
PSA:
Anyone who finds themselves here should really just download the linked project above from dropbox and study the structure there.
Spent a long time trying to set this up and it's just not worth it. Once you have the working project you can duplicate it and if you really want to know where things reside and how to set up the module/artifact configuration in Intellij then you can go there.
I did have one question on this if Serge Baranov is around to answer:
If I were trying to create this project with this structure from the "New Project" window, what settings would I choose?
> If I were trying to create this project with this structure from the "New Project" window, what settings would I choose?
See https://stackoverflow.com/a/63606566/104891.
In the current IDE version you should use Spring Initializr instead.