我无法理解为什么我的控制器没有重定向到我的html。有人可以帮我吗?
WebConfig.java
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.udemy.controller" })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
public InternalResourceViewResolver resolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix("");
return resolver;
}
}
HelloWorldController.java
@Controller
@RequestMapping("/example")
public class HelloWorldController {
public static final String EXAMPLE_VIEW = "example.html";
@GetMapping("/")
public String fileUploadForm(Model model) {
return "fileDownloadView";
}
@GetMapping("/helloworld")
public String helloWorld(){
return "helloworld";
}
// @RequestMapping(value="/exampleString", method=RequestMethod.GET)
@GetMapping("/exampleString")
public String exampleString(Model model){
model.addAttribute("name","John");
return EXAMPLE_VIEW;
}
// @RequestMapping(value="/exampleMAV", method=RequestMethod.GET)
@GetMapping("/exampleMAV")
public ModelAndView exampleMAV() {
ModelAndView mav= new ModelAndView(EXAMPLE_VIEW);
mav.addObject("name", "Mike");
return mav;
}
AppInitializer
public class MyWebAppInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] {};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
我的项目结构做得很好。所以我的html和jsps,都在根WEB-INF / views中。此外,anotation @ComponentScan正在检测控制器。所以,它不是root的问题。任何人都可以告诉我,为什么我不会重定向到.html,请...
错误说:
ADVERTENCIA:在DispatcherServlet中找不到带有URI [/spring-mvc-download-example/WEB-INF/views/example.html]的HTTP请求的映射,其名称为“dispatcher”
答案 0 :(得分:0)
在您的控制器类中,
上方@RequestMapping("/example")
插入:
@Controller
将会:
@Controller
@RequestMapping("/example")
答案 1 :(得分:0)
您必须使用@Controller
或@RestController
注释类HelloWorldController,然后才会被@Componentscan
注释选中。