在Spring 4中设置.html和.jsp ViewResolvers?

时间:2018-01-02 14:14:57

标签: java spring spring-4

如果我只需要.jsp作为我的网络应用程序中的视图,那么我写下这样的内容:

 @EnableWebMvc
 @ComponentScan("com.test")
 public class WebConfig extends WebMvcConfigurerAdapter {

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

是否也可以使用.html?例如:

@Controller
public class HelloController {
  @RequestMapping(value = "/hello", method = RequestMethod.GET)
  public String printHello(ModelMap model) {
    model.addAttribute("message", "Hello Spring MVC Framework!");
    return "hello"; // uses jsp extension
  }
}

@Controller
public class HomeController {
  @RequestMapping(value = "/home", method = RequestMethod.GET)
  public String home() {
    return "home"; // uses html extension
  }
}

0 个答案:

没有答案