【问题标题】:No mapping found for HTTP request with URI [/WEB-INF/views/welcome.jsp] in DispatcherServlet with name 'dispatcherServlet' [duplicate]在名称为“dispatcherServlet”的 DispatcherServlet 中找不到具有 URI [/WEB-INF/views/welcome.jsp] 的 HTTP 请求的映射 [重复]
【发布时间】:2016-12-22 00:25:09
【问题描述】:

我配置了应用程序并将“DispatcherServlet”编码为 viewResolver,如下所示:

@Configuration
@EnableWebMvc
@ComponentScan ({"controllers"})
@EnableAutoConfiguration
@SpringBootApplication
public class Application {

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
public static void main(String[] args){
        SpringApplication.run(Application.class, args);
}
}

处理请求的控制器类如下所示:

@Controller
public class HelloControllerImpl {

@RequestMapping(value= "/welcome", method= RequestMethod.GET)
public String getWelcomePage(ModelMap model) {
    model.addAttribute("message", "Spring 3 MVC - Hello World");
    model.addAttribute("name", "vzateychuk");
    return "welcome";
}
}   

视图文件:\WEB-INF\views\welcome.jsp

<html>
<body>
    <h1>Hello, : ${name}</h1>
    <h2>Message : ${message}</h2>
</body>
</html>

应用结构: Welcome application structure

我认为配置文件中缺少某些内容,但我看不到。你能不能客串一下有什么问题,什么意思:“没有为带有 URI [/WEB-INF/views/welcome.jsp] 的 HTTP 请求找到映射”? 我应该提供像 dispatcher-servlet.xml 之类的 xml 配置文件吗? 提前谢谢你。

更新:我猜我的 DispatcherServlet 找不到合适的视图。我试图完全删除 /WEB-INF 目录,但没有任何变化。这段代码可能有问题:

    public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    **viewResolver.setPrefix("/WEB-INF/views/");**

.... 谁能猜出什么是错的? (可能是注释@EnableAutoConfiguration 不允许定义viewResolver 的前缀?

【问题讨论】:

  • 您是否向/WEB-INF/views/welcome.tmpl 端点发送了请求?您应该将请求发送到/welcome 端点
  • 阿里,我正在尝试请求端点:localhost:8080/welcome
  • 检查你在@ComponentScan中扫描了正确的包
  • 扫描包是控制器,所以没有实际线索
  • 你的项目结构是什么?你能提供你的welcome.jsp 文件的位置吗?

标签: java spring spring-mvc requestdispatcher


【解决方案1】:

我做了与你类似的简单项目。你可以查看我的github

你要做的是:

  1. hello.html 重命名为 hello.jsp
  2. 检查您的 pom.xml 中是否包含所有依赖项。我没有看到它,所以我不确定它是否是错误的。确保你有这两个依赖项:
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <scope>provided</scope>
    </dependency>
    
    对于这一点,你会找到解释here
  3. 确实,您在使用 IDEA 社区版启动它时可能会遇到问题。我也遇到过这个问题。你可以做的是首先使用命令行和maven检查它。执行以下命令:
    mvn spring-boot:run
    您还可以配置您的 IDEA 以运行该命令。转到运行->编辑配置,单击左侧的绿色加号并选择 Maven。然后在“命令行”字段中写入“spring-boot:run”,按确定。 并运行此配置。
  4. (可选)您的应用程序类上还有一些冗余注释。您可以删除:
    • @Configuration,因为@SpringBootApplication 已经有了它
    • @EnableWebMvc,因为 Spring Boot 在 classpath 上看到 spring-webmvc 时会自动添加它
    • @EnableAutoConfiguration,因为@SpringBootApplication 已经有了它

请注意,由于您的包结构,您需要 @ComponentScan ({"controllers"}) - 您的 Application 类与您的控制器位于不同的包中。

【讨论】:

  • 谢谢,终于成功了。问题与 IDEA 社区版本有关。谢谢你们。
  • 工作。谢谢。
【解决方案2】:

配置解析器配置application.properties

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp

spring boot 会自动配置你的内部视图解析器。

你需要在pom中添加jstl jars

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

让你的视图解析器工作添加

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}

spring-boot 有一个 sample 给你

【讨论】:

  • 谢谢你,kuhajeyan,终于成功了!但有一件事出乎我的意料:对于 VIews,只有 *.jsp 可能(不是 *.html 或其他)。
猜你喜欢
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 2013-11-19
  • 1970-01-01
  • 2016-07-02
  • 2013-09-12
  • 2016-04-01
相关资源
最近更新 更多