我在搜索字段中搜索了一些有关我的问题的信息,但没有找到解决方案。在通过 maven 在 intellij idea 中创建的 spring 项目(不是引导)中,我设置了所有东西(见下文),如 tomcat 配置、简单 helloWorld 的 java 配置等......但是:我只得到了爆炸 war 的结果,而不是欢迎目录:我在 localhost:8080 看到它(不是在 localhost:8080/welcome)。 如果我将 artifact 设置为 war,我会得到通常的 tomcat 主页,而与我的项目无关。
下面的代码片段:
Controller :
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HomeController {
@RequestMapping("/")
public String welcome(Model model){
model.addAttribute("hello", "Hello");
model.addAttribute("anyone", "anyone");
return "welcome";
}
}
Web应用上下文配置:
<pre><code>package com.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; @Configuration @EnableWebMvc @ComponentScan("com") public class WebAppContextConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public InternalResourceViewResolver getInternalViewResolver(){ InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setViewClass(JstlView.class); resolver.setPrefix("/pages/"); resolver.setSuffix(".jsp"); return resolver; } } </code></pre>
DispatcherServletInitializer:
package com.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebAppContextConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
pom.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>io.github</groupId>
<artifactId>webstore</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
最佳答案
我建议您不要为 Spring 配置文件而烦恼,而是在已经为您配置好的地方使用 Spring Boot。
例如,这个“hello world”样本应该做你想做的:https://github.com/pgrimard/spring-boot-hello-world
另外,考虑 http://start.spring.io/用于生成项目的框架。
关于java - Spring + Maven + Intellij +HelloWorld,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44483428/