有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我的程序没有运行,虽然它没有给我错误

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

计算器应用程序初始化器取代web。xml配置中的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("/");
        
        }
    
    }

CalculatorAppContext替换应用程序上下文。xml配置中的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" ;
     }
    }

嗨。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中添加了对spring webmvc的依赖。xml

    <?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


共 (0) 个答案