Spring MVC应用程序不Map,9. history IntelliJ Idea,Linux Mint

deyfvvtc  于 2023-08-02  发布在  Spring
关注(0)|答案(2)|浏览(91)
  • 我刚刚开始学习如何制作Spring MVC应用程序。安装Tomcat 9。错误很可能是Map不起作用。我也用Thymeleaf。程序的执行基本上不会到达我的Java代码(我设置了断点,应用程序没有停止)。*

我试过安装不同版本的Tomcat
我还检查了端口并输入了正确的URL。我还想注意的是,当输入hello.html文件的完整路径(相对于webapp)时,在我的例子中 http://localhost:8080/views/hello.html,它也给出了一个404错误,但一切都将与jsp文件 http://localhost:8080/views/hello.jsp 一起工作(但这仍然不是正确的事情,因为Map不起作用)。
按照计划,当您在url中输入字符串 http://localhost:8080/helloW 时,hello.html应该会显示在浏览器中。
问题是,当我输入URL http://localhost:8080/helloW 并看到:x1c 0d1x的数据
我还尝试在配置中放置不同的应用程序上下文:


我的项目结构:



下面我将展示我的类的代码:
HelloController:

package edu.vega.spring;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @Controller
    public class HelloController {
        @GetMapping(value = {"/helloW", "/"})
        public String sayHello(){
            return "hello";
        }
    }

字符串
MySpringMVCDispatcherServletInitializer:

package edu.vega.spring.config;
    
    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
    
    public class MySpringMVCDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return null;
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
            return new Class[] {SpringConfig.class};
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[]{"/"};
        }
    }


SpringConfig:

package edu.vega.spring.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    import org.thymeleaf.spring5.SpringTemplateEngine;
    import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
    import org.thymeleaf.spring5.view.ThymeleafViewResolver;
    
    @Configuration
    @ComponentScan("edu.vega.spring")
    @EnableWebMvc
    public class SpringConfig implements WebMvcConfigurer {
        private final ApplicationContext applicationContext;
    
        @Autowired
        public SpringConfig(ApplicationContext applicationContext) {
            this.applicationContext = applicationContext;
        }
    
        @Bean
        public SpringResourceTemplateResolver templateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setApplicationContext(applicationContext);
            templateResolver.setPrefix("views/");
            templateResolver.setSuffix(".html");
            return templateResolver;
        }
    
        @Bean
        public SpringTemplateEngine templateEngine() {
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());
            templateEngine.setEnableSpringELCompiler(true);
            return templateEngine;
        }
    
        @Override
        public void configureViewResolvers(ViewResolverRegistry registry) {
            ThymeleafViewResolver resolver = new ThymeleafViewResolver();
            resolver.setTemplateEngine(templateEngine());
            registry.viewResolver(resolver);
        }
    }


pom.xml(dependencies):

<dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf-spring5</artifactId>
          <version>3.1.1.RELEASE</version>
        </dependency>
    
        <dependency>
          <groupId>jakarta.servlet</groupId>
          <artifactId>jakarta.servlet-api</artifactId>
          <version>6.0.0</version>
        </dependency>
    
      </dependencies>
      <build>
        <finalName>spring_web_app1</finalName>
      </build>
    </project>


我尝试了很多东西,但都不起作用,我已经在考虑把操作系统完全换成Ubuntu,也许这样会有帮助。
我拥有的所有日志(现在我在访问日志中):



输出量:

127.0.0.1 - - [07/Jul/2023:11:30:41 +0300] "GET / HTTP/1.1" 200 48
127.0.0.1 - - [07/Jul/2023:11:30:41 +0300] "GET / HTTP/1.1" 200 48
0:0:0:0:0:0:0:1 - - [07/Jul/2023:11:30:41 +0300] "GET / HTTP/1.1" 200 48
0:0:0:0:0:0:0:1 - - [07/Jul/2023:11:30:47 +0300] "GET /helloW HTTP/1.1" 404 682


LocalHost日志输出:

07-Jul-2023 11:30:40.491 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Marking servlet [dispatcher] as unavailable
07-Jul-2023 11:30:40.492 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [dispatcher] in web application [] threw load() exception
    java.lang.ClassCastException: class org.springframework.web.servlet.DispatcherServlet cannot be cast to class javax.servlet.Servlet (org.springframework.web.servlet.DispatcherServlet is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @168986bb; javax.servlet.Servlet is in unnamed module of loader java.net.URLClassLoader @3aeaafa6)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1020)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:960)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4673)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4980)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:683)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:658)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:662)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1782)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:460)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:408)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1310)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1412)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1623)


Tomcat 10+

12-Jul-2023 13:32:02.151 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.ApplicationContext.log Servlet.init() for servlet [dispatcher] threw exception
    java.lang.NoSuchMethodError: 'void org.springframework.web.context.support.ServletContextResourceLoader.<init>(jakarta.servlet.ServletContext)'
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:155)
        at jakarta.servlet.GenericServlet.init(GenericServlet.java:143)
        at jakarta.servlet.http.HttpServlet.init(HttpServlet.java:121)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:944)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:901)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:805)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4633)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4926)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:683)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:658)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:713)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1821)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:428)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:376)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1310)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1412)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1623)
12-Jul-2023 13:32:02.151 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.core.StandardContext.loadOnStartup Servlet [dispatcher] in web application [] threw load() exception
    java.lang.NoSuchMethodError: 'void org.springframework.web.context.support.ServletContextResourceLoader.<init>(jakarta.servlet.ServletContext)'
        at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:155)
        at jakarta.servlet.GenericServlet.init(GenericServlet.java:143)
        at jakarta.servlet.http.HttpServlet.init(HttpServlet.java:121)
        at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:944)
        at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:901)
        at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:805)
        at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4633)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4926)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:683)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:658)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:713)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1821)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:428)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:376)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:294)
        at java.management/com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:803)
        at java.management/com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802)
        at java.management/com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1472)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1310)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1412)
        at java.management.rmi/javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at java.rmi/sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:360)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:200)
        at java.rmi/sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:714)
        at java.rmi/sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:598)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:844)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:721)
        at java.base/java.security.AccessController.doPrivileged(AccessController.java:400)
        at java.rmi/sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:720)
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
        at java.base/java.lang.Thread.run(Thread.java:1623)


spring 5和thymeleaf 5例外

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.thymeleaf.spring5.view.ThymeleafViewResolver#0' defined in ServletContext resource [/WEB-INF/applicationContextMVC.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'debug' of bean class [org.thymeleaf.spring5.view.ThymeleafViewResolver]: Bean property 'debug' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?


我的pom.xml与openJDK 20:

<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 https://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.vega.spring</groupId>
  <artifactId>spring_web_app1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring_web_app1 Maven Webapp</name>
  <url>https://maven.apache.org</url>
  <properties>
    <maven.compiler.source>20</maven.compiler.source>
    <maven.compiler.target>20</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring6</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>

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

  </dependencies>
  <build>
    <finalName>spring_web_app1</finalName>
  </build>
</project>


上面的pom.xml错误日志(抱歉截图,但正文中有30 k符号限制):x1c4d 1x的

nwlls2ji

nwlls2ji1#

java.lang.ClassCastException:类org.springframework.web.servlet.DispatcherServlet不能强制转换为类javax.servlet.Servlet(org.springframework.web.servlet.DispatcherServlet
Spring 6.0需要新的jakarta.servlet命名空间而不是javax.servlet。所以你需要Tomcat 10,看起来。
https://spring.io/blog/2022/11/16/spring-framework-6-0-goes-ga
https://tomcat.apache.org/migration-10.html

g6ll5ycj

g6ll5ycj2#

所以,在1周内我解决了我的问题,我希望如果有人采取同样的问题,我的答案帮助你。
pom.xml中的所有依赖项都是正确的(jakarta-api 6 with spring 6 and thymeleaf 6),我需要使用TomCat 10+ for Spring 6+(10.1.11 -目前我的版本)。
我试着在终端中使用mvn clean install命令,我看到了这个问题:

Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war 
(default-war) on project spring_web_app1: Execution default-war of goal 
org.apache.maven.plugins:maven-war-plugin:2.2:war failed: Unable to load 
the mojo 'war' in the plugin 'org.apache.maven.plugins:maven-war- 
plugin:2.2' due to an API incompatibility:

字符串
org.codehaus.plexus.component.repository.exception.ComponentLookupException:无法访问属性的默认字段
问题与Maven War插件版本兼容性有关。
之后,我添加了新版本的插件:

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>


也许也许默认版本是2.2
在此之后,如果你运行使用mvn clean install,你将采取与web.xml错误,你需要添加该文件,并使用基本配置(你可以在谷歌中找到它)配置web.xml后,你可以使用mvn clean install
我的完整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 https://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.vega.spring</groupId>
  <artifactId>spring_web_app1</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>spring_web_app1 Maven Webapp</name>
  <url>https://maven.apache.org</url>
  <properties>
    <maven.compiler.source>20</maven.compiler.source>
    <maven.compiler.target>20</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.1</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>6.0.11</version>
    </dependency>

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring6</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf</artifactId>
      <version>3.1.1.RELEASE</version>
    </dependency>

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

  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
      </plugin>
    </plugins>
  </build>
</project>


如果你发现我的回答有错误,请纠正我。

相关问题