The problem is that when i run my app with Tomcat it shows only index.jsp in browser. But controllers don't work.
Here's my web.xml
file:
<?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-course-mvc</display-name>
<absolute-ordering />
<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/applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml
file:
<?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">
<context:component-scan base-package="com.myapp.spring.mvc" />
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
My controller:
package com.myapp.spring.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
@RequestMapping("/")
public String showFirstView() {
return "first-view";
}
}
I have view
directory in webapp/WEB-INF/
where my first-view.jsp
lies. So it looks webapp/WEB-INF/view/first-view.jsp
And when i run project on http://localhost:8080/spring_course_mvc/
I get 404 Error.
Interesting that when i added webapp/index.jsp
file I see its content on http://localhost:8080/spring_course_mvc/
.
Then i added webapp/first-view.jsp
file and on adress http://localhost:8080/spring_course_mvc/first-view.jsp
it's working.
Without webapp/index.jsp
adress http://localhost:8080/spring_course_mvc/
gives me 404 Error.
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.myapp.spring.mvc</groupId>
<artifactId>spring_course_mvc</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring_course_mvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.1.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>6.1.7</version>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>6.1.7</version>
</dependency>
</dependencies>
<build>
<finalName>spring_course_mvc</finalName>
</build>
</project>
First, you need to make sure that if you are using Spring 5, you need to use Tomcat 9.
And if you are using Spring 6, you need to use Tomcat 10.
The next thing that you need to make sure is that your web.xml and your applicationContext.xml are located in webapp/WEB-INF/web.xml
and webapp/WEB-INF/applicationContext.xml
.
Another thing that you need to keep in mind if your application does not run a browser automatically you need to make sure that you are using a correct context root value. In eclipse you can check that by right clicking on your project > Properties > Web Project Settings.