[英]Basic SpringMVC + Tomcat issue
我对 SpringMVC 很陌生,现在我正在尝试使用本教程构建一个简单的应用程序: http : //websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-annotation-javaconfig-full-example/我已经检查过基本 SpringMvC 控制器不起作用,这似乎不是我的问题,即使我插入应用程序名称,我的应用程序也不可用。 我使用基于注释的配置和 Tomcat 9。
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";
}
}
博客配置.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;
}
}
博客Initializer.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 抱怨甚至不存在的值。
首先检查服务器是否正在运行尝试点击apache的主页。
然后检查 webapps 文件夹内的 tomcat 目录,如果您的应用程序在服务器上成功部署,应该有一个包含您的应用程序名称的文件夹。
所以,我最终放弃了在 eclipse 中做这件事的尝试,在 Netbeans 中创建了一个 WebApp 项目,并且设法在那里启动了一个基于注释的项目,问题明显减少了。 感谢大家的贡献。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.