springmvc 配置了跳转到jsp页面 怎么实现跳转到html页面

2025-01-24 09:42:50
推荐回答(1个)
回答1:

先看controller:
@RequestMapping(value = "/{id}/view", method = RequestMethod.GET, produces = "text/html")
public String getPostByIdHtml( @PathVariable String id) throws IOException {
return "/resources/Post.html";
}

再看配置:
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan({ "com.my.web.api"})
@ImportResource("classpath:db-context.xml")
public class ApiServletConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
internalResourceViewResolver.setPrefix("/resources/");
internalResourceViewResolver.setSuffix("*.html");
return internalResourceViewResolver;
}
}