Chr*_*oph 21 spring spring-mvc spring-boot
我有一个Spring Boot(v1.3.6)单页面应用程序(angular2),我想将所有请求转发给index.html
.
对http:// localhost:8080/index.html的请求正在运行(200,我得到index.html),但http:// localhost:8080/home不是(404).
Runner.class
@SpringBootApplication
@ComponentScan({"packagea.packageb"})
@EnableAutoConfiguration
public class Runner {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext run = SpringApplication.run(Runner.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
WebAppConfig.class
@Configuration
@EnableScheduling
@EnableAsync
public class WebAppConfig extends WebMvcConfigurationSupport {
private static final int CACHE_PERIOD_ONE_YEAR = 31536000;
private static final int CACHE_PERIOD_NO_CACHE = 0;
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.setOrder(-1);
registry.addResourceHandler("/styles.css").addResourceLocations("/styles.css").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
registry.addResourceHandler("/app/third-party/**").addResourceLocations("/node_modules/").setCachePeriod(CACHE_PERIOD_ONE_YEAR);
registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(CACHE_PERIOD_NO_CACHE);
registry.addResourceHandler("/systemjs.config.js").addResourceLocations("/systemjs.config.js").setCachePeriod(CACHE_PERIOD_NO_CACHE);
registry.addResourceHandler("/**").addResourceLocations("/index.html").setCachePeriod(CACHE_PERIOD_NO_CACHE);
}
}
Run Code Online (Sandbox Code Playgroud)
styles.css
,, /app/third-party/xyz/xyz.js
...正在工作(200,我得到正确的文件).只是/**
为了index.html
不工作.
Jea*_*ois 37
您还可以添加转发控制器,如:
@Controller
public class ForwardingController {
@RequestMapping("/{path:[^\\.]+}/**")
public String forward() {
return "forward:/";
}
}
Run Code Online (Sandbox Code Playgroud)
第一部分{path:[^\\.]+}
匹配除了之外的任何字符中的一个或多个.
.这样可以确保file.ext
此RequestMapping不会处理对a的请求.如果你需要支持子路径也可以转发,放在/**
外面{...}
.
这对我不起作用:
return "forward:/";
Run Code Online (Sandbox Code Playgroud)
感谢Spring MVC @RestController 和重定向,我找到了一个很好用的解决方案:
@RequestMapping(value = "/{[path:[^\\.]*}")
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/");
}
Run Code Online (Sandbox Code Playgroud)
如果不查看日志,我不完全确定为什么它没有正确映射,但是如果您想将 URL 映射到视图(HTML),那么您可能最好使用viewController
spring 提供的机制http://docs.spring。 io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-view-controller。例如
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("home");
}
}
Run Code Online (Sandbox Code Playgroud)
(取自上面链接的 spring 文档 - 这是您应该将 url 映射到视图的方式,而不是重新利用静态资源的映射。)
我不确定资源映射是否有任何类型的后缀过滤 - 例如我不知道 spring 如何决定将请求映射到ResourceHttpRequestHandler
- 你是否尝试过(只是为了确认或拒绝)是否像http:// localhost:8080/home.html放大器到什么?
由于 Spring-Boot 的默认主页行为,您上面定义的 html 映射也可能被忽略,而 index.html 正在工作: https: //github.com/spring-projects/spring-boot/blob /master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ResourceProperties.java#L108
归档时间: |
|
查看次数: |
21984 次 |
最近记录: |