internalResourceViewResolver在春季不起作用

时间:2018-08-02 12:17:28

标签: spring spring-mvc spring-boot jstl

我有以下简单的应用程序,无论将WEB-INF文件夹放在哪里,访问http://localhost:8080时总是会收到此错误:

  

2018-08-02 15:06:23.076警告716 --- [nio-8100-exec-1]   o.s.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;
    }
}

结构:

enter image description here

1 个答案:

答案 0 :(得分:0)

发现了问题,实际上所有的代码都找到了,出于某种原因,我需要将其添加到maven:

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>