【问题标题】:internalResourceViewResolver doesn't work in springinternalResourceViewResolver 在春季不起作用
【发布时间】: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


    【解决方案1】:

    找到问题了,所有的代码其实都找到了,不知什么原因我需要在maven中添加这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2015-02-27
      • 2016-08-20
      相关资源
      最近更新 更多