新手在学Springboot整合jsp的时候出现的一个问题
问题描述:
无法在名称为“ dispatcherServlet”的servlet中解析名称为“ Index”的视图
问题分析:
在控制层:
@RequestMapping("/TestIndex")
public ModelAndView Test() {
System.out.println("hello");
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "Hello,Springboot");
mv.setViewName("Index");
return mv;
}
按住ctrl加鼠标 的时候可以跳转过去,说明本身没啥问题,但就是一直报错,
突然想到了dispathServlet是mvc框架的东西,会不会是那边的问题。
在之前这项目弄过Springboot整合Springmvc的时候,出现一个工具类
package com.example.springboot.demo01.Config;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@EnableWebMvc
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/userHello").setViewName("Hello");
}
}
注解@EnableWebMvc
将此注释添加到@Configuration类中将导入Spring MVC配置
解决方案
将注解@EnableWebMvc去掉即可
运行结果:
一个小小的问题弄了半天,我也是佛了,大家不要学我把大部分整合案例放在一个工程里,以免出现不必要的麻烦,可以新建模块,进行学习整合。