0

i deployed a small java maven springmvc web application on tomcat10.1.8/10.1.9. Even though I've added all the dependencies I need related to jakarta changement, I'm having trouble invoking the controller.

1)Controller

package test;

import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController
{
@GetMapping("/hello")
public String homeInit(Locale locale, Model model) {
return "hello";
}
}

2)Context

package test;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class MyApplicationInitializer implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) {
    AnnotationConfigWebApplicationContext context
        = new AnnotationConfigWebApplicationContext();
    context.setConfigLocation("test.WebMvcConfig");

    servletContext.addListener(new ContextLoaderListener(context));

    ServletRegistration.Dynamic dispatcher = servletContext
        .addServlet("hello", new DispatcherServlet(context));
   

    dispatcher.setLoadOnStartup(2);
    dispatcher.addMapping("/hello");
  }
}

3)set jsp resolver

package test;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = {"test"})
public class WebMvcConfig implements WebMvcConfigurer {

  @Bean
  public InternalResourceViewResolver resolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setViewClass(JstlView.class);
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
   
    return resolver;
  }
}

4)hello.jsp

<html>
  <head><title>Spring Web MVC Example</title></head>
  <body>
    <h1>Test</h1>
    <h2>Spring Web MVC Example</h2>
  </body>
</html>

5)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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>spring-webmvc-test</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>spring-webmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>17</java.version>
<failOnMissingWebXml>false</failOnMissingWebXml>
<spring.version>6.0.6</spring.version>
</properties>
<dependencies>
<!-- Spring MVC Dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>jakarta.servlet.jsp</groupId>
<artifactId>jakarta.servlet.jsp-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>


<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>

<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>

<dependency>
<groupId>jakarta.el</groupId>
<artifactId>jakarta.el-api</artifactId>
<version>5.0.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>11.0.15</version>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven3-plugin</artifactId>
<configuration>
<container>
<containerId>tomcat10x</containerId>
<artifactInstaller>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat</artifactId>
<version>10.0.27</version>
</artifactInstaller>
</container>
<configuration>
<properties>
<!-- <cargo.servlet.port>8080</cargo.servlet.port> -->
<cargo.logging>low</cargo.logging>
</properties>
</configuration>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</build>
</project>

Thanks and best regards

6)under jetty

mvn clean install jetty:run

URL: http://localhost:8080/hello

7)under tomcat10.1.8/10.1.9

either with the cargo plugin as follows or deploy the war file on tomcat mvn clean package cargo:run

URL: http://localhost:8080/hello

It works under jetty but under Tomcat I always get the error ""GET /hello HTTP/1.1" 404" and "No mapping for GET /hello". the controller is recognized but the mapping is not I can't see any more errors I work with jdk17 and springframework6, see pom.xml

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

1 Answers1

0

I have found the solution. The problem was that the URL is wrong. I also had to use the name of the war application as follows:http: localhost:8080/<name_of_the_warfile>/hello for the jetty it works without the name of the web application i developed this application to test if new versions of jdk 17, springframework 6, jakarta-servlet 3, jsp 3 work on tomcat 10.x.

I came to the conclusion from another similar application that something in the mapping has changed