如何通过单击视图名称链接到控制器中的视图文件

时间:2019-08-30 13:57:26

标签: spring-boot spring-mvc intellij-idea

一个springboot web项目,按需工作。在控制器代码中,我要单击“ manage / operate / createCluster”,然后该想法将打开/web-inf/jsp/manage/operate/createCluster.jsp文件。

如何实现或可以引用哪些信息?

@RequestMapping(value = "createCluster", method = RequestMethod.GET)
public ModelAndView doClusterList(HttpServletRequest request,
                                  HttpServletResponse response, Model model) {
    model.addAttribute("activeMenuId","createCluster");
    return new ModelAndView("manage/operate/createCluster");
}

1 个答案:

答案 0 :(得分:0)

如果您只希望添加一个视图而无需填充任何数据,则可以在您的WebMvc配置类中添加一个视图控制器,例如:

@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig implement WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
  registry.addViewController("/manage/operate/createCluster").setViewName("manage/operate/createCluster");
}

}

如果要在视图中发送数据作为示例,则必须仅将视图名称发送为String:

@RequestMapping(value = "createCluster", method = RequestMethod.GET)
public String doClusterList(HttpServletRequest request,
                              HttpServletResponse response, Model model) {
model.addAttribute("activeMenuId","createCluster");
return "manage/operate/createCluster";

}

对于两种情况,都应添加视图解析器的配置:

@Bean
public InternalResourceViewResolver viewResolver() {
   InternalResourceViewResolver resolver = new InternalResourceViewResolver();
   resolver.setPrefix("/WEB-INF/jsp/");
   resolver.setSuffix(".jsp");
   return resolver;
 }