【发布时间】:2018-08-02 12:17:28
【问题描述】:
我有以下简单的应用程序,无论我把WEB-INF文件夹放在哪里,访问http://localhost:8080时总是出现这个错误:
2018-08-02 15:06:23.076 警告 716 --- [nio-8100-exec-1] os.web.servlet.PageNotFound : 未找到 HTTP 映射 在 DispatcherServlet 中使用 URI [/WEB-INF/home.jsp] 请求名称 'dispatcherServlet'
所有代码如下:
package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableAutoConfiguration
@SpringBootApplication
@ComponentScan
@EnableWebMvc
@Configuration
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
。
package demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import javax.management.RuntimeErrorException;
@Controller
@ComponentScan
public class HomeController {
@RequestMapping(value="/", method=GET)
private String home() {
return "home";
}
}
.
package demo;
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.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@EnableWebMvc
@Configuration
@ComponentScan("demo")
public class WebConfig implements WebMvcConfigurer {
// All web configuration will go here
@Bean
public ViewResolver internalResourceViewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/");
bean.setSuffix(".jsp");
return bean;
}
}
结构:
【问题讨论】:
标签: spring spring-mvc spring-boot jstl