有 Java 编程相关的问题?

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

java Basic SpringMVC+Tomcat问题

enter image description here我对SpringMVC非常陌生,现在我正尝试使用本教程构建一个简单的应用程序: http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/ 我查过了 Basic SpringMvC controller not working这似乎不是我的问题,即使我插入应用程序名称,我的应用程序也不可用。我使用基于注释的配置和Tomcat9

here is my project structure. The error is eclipse complaining about the issue in pom.xml, while the file itself works fine and the code compiles 我有三门课: 主控制器。爪哇

package mvc_webapp.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class MainController {

@RequestMapping(method = RequestMethod.GET)
public String sayHello() {
    return "index";
}

@RequestMapping(value = "/index", method = RequestMethod.GET)
public String indexPage() {
    return "index";
}
}

博客配置。爪哇

package mvc_webapp.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "mvc_webapp")
public class BlogConfiguration {
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("WEB-INF/views/");
        viewResolver.setSuffix(".html");
        return viewResolver;
    }
   }

博客初始化器。爪哇

package mvc_webapp.configuration;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class BlogInitializer implements WebApplicationInitializer {

public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(BlogConfiguration.class);
    ctx.setServletContext(servletContext);

    ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    servlet.setLoadOnStartup(1);
    servlet.addMapping("/");
}

}

波姆。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>net.atos</groupId>
  <artifactId>mvc_webapp</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mvc_webapp 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-webmvc</artifactId>
    <version>5.0.8.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>4.0.1</version>
    <scope>provided</scope>
</dependency>       
  </dependencies>
  <build>
    <pluginManagement>
        <plugins>
           <plugin>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-plugin-api</artifactId>
                <version>3.5.4</version>
                 <configuration>
                    <warSourceDirectory>src/main/webapp</warSourceDirectory>
                    <warName>mvc_webapp</warName>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
             </configuration>
        </plugin>
    </plugins>
</pluginManagement>
<finalName>mvc_webapp</finalName>

我或多或少了解这些组件是什么,但我无法检测出到底是什么错误-类的内容与教程略有不同。 更新: 我添加了带有项目问题的屏幕截图,尽管它们似乎与我无关,因为编译器和构建工具都没有发布任何错误。更不用说那个pom了。xml抱怨甚至不存在的值


共 (2) 个答案

  1. # 1 楼答案

    因此,我最终放弃了在eclipse中实现这一点的尝试,在Netbeans中创建了一个WebApp项目,在那里启动一个基于注释的项目的问题大大减少。谢谢大家的贡献

  2. # 2 楼答案

    首先,检查服务器是否正在运行,尝试访问apache的主页

    然后检查webapps文件夹中的tomcat目录,如果您的应用程序在服务器上成功部署,则应该有一个包含应用程序名称的文件夹