我的程序没有运行以为它不会给我错误

问题描述

我是 Spring mvc 的初学者,我在 eclipse 中使用 maven 创建了一个 webapp 项目,它是用 java 纯代码配置的。项目中没有错误,但是当我运行它时,它不起作用,它给我 HTTP 404 错误 [/spring-calculator/test] 不可用。 spring-calculator 是我的名字项目 测试是控制器 url 映射

Class CalculatorApplicationInitializer 替换 xml 配置中的 web.xml

        public class CalculatorApplicationInitializer implements WebApplicationInitializer {
    
         
             public void onStartup(ServletContext servletContext) throws servletexception {
            
            AnnotationConfigWebApplicationContext  WebApplicationContext = new   
            AnnotationConfigWebApplicationContext();
            WebApplicationContext.register(CalculatorAppContext.class);
            
            // create servlet dispacher object
            dispatcherServlet dispatcherServlet = new
            dispatcherServlet(WebApplicationContext);  
            
            //registred dispatcher servlet with servlet context
            ServletRegistration.Dynamic myCustomdispatcherServlet =           
            servletContext.addServlet("MydispatcherServlet",dispatcherServlet);
            
            myCustomdispatcherServlet.setLoadOnStartup(1);
            myCustomdispatcherServlet.addMapping("/");
        
        }
    
    }

Class CalculatorAppContext 替换 xml 配置中的 applicationContext.xml

    @EnableWebMvc
    @Configuration
    
    @ComponentScan(basePackages = "com.home.lc.controllers")
    public class CalculatorAppContext {
    
        // setup my view resolver
        @Bean
        public InternalResourceViewResolver viewResolver() {
            
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
    }

That's the controller returning vue

    @Controller
    public class TestController {
    
    
    
    @RequestMapping("/test")
     public String SayHello() {
    
         return "hi" ;
     }
    }

Vue hi.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <Meta charset="UTF-8">
    <title>:)</title>
    </head>
    <body>
    <p> Hello </p>
    
    </body>
    </html>
 

我刚刚在 pom.xml

添加了 spring-webmvc 依赖
    <?xml version="1.0" encoding="UTF-8"?>
    
    <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>com.home</groupId>
      <artifactId>spring-calculator</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>spring-calculator Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.3.5</version>
        </dependency>
      </dependencies>
    
      <build>
        <finalName>spring-calculator</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may  
          be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default- 
             bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>3.2.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>

There is a stucture of my project

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)