【问题标题】:Basic SpringMVC + Tomcat issue基本的 SpringMVC + Tomcat 问题
【发布时间】:2019-01-24 11:50:57
【问题描述】:

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

我有三个班级: MainController.java

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";
}
}

BlogConfiguration.java

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;
    }
   }

BlogInitializer.java

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("/");
}

}

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/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 抱怨甚至不存在的值。

【问题讨论】:

  • 在 Eclipse 中打开问题视图并分享与该项目相关的任何问题。
  • 首先修复工作区中的所有错误。
  • 我添加了截图,但消息没有多大意义,代码似乎正在编译。
  • « 制作 JAR,而不是 WAR。 »,乔什·朗

标签: java spring-mvc tomcat9


【解决方案1】:

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

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

【讨论】:

  • 服务器肯定正在运行,应用程序在 Tomcat 管理器中可见,但即使在这种情况下我也无法访问它。我的代码很可能有问题,我只是找不到确切的位置。
  • 尝试调试应用程序,在可疑的地方应用断点,看看,很可能你会发现问题。
  • 看看pom.xmlweb.xml中的错误,你需要先解决这个问题。
  • 好吧,教程声称:这里不需要 web.xml,因为配置是基于注释的,并且 设置为 FALSE,即使 eclipse 另有说明
  • 之前没有错误图片,所以我建议检查服务器。
【解决方案2】:

因此,我最终放弃了在 Eclipse 中进行此操作的尝试,在 Netbeans 中创建了一个 WebApp 项目,并且在那里启动了一个基于注释的项目,并且问题明显减少了。感谢大家的贡献。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2023-03-17
    • 2018-01-08
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多