我有一个使用pushState的Backbone.js客户端路由的单页web应用程序。为了让它工作,我必须告诉我的服务器(Java、Spring3、Tomcat)哪些URL应该在服务器上解析(实际的JSP视图、API请求),哪些应该简单地发送到索引页由客户端处理。目前,我使用InternalResourceViewResolver简单地提供与URL请求名称匹配的JSP视图。由于客户端URL在服务器上没有视图,因此服务器返回404。
向Spring (或Tomcat)指定几个特定的URL(我的客户端路由)都应该解析到index.jsp,而其他任何东西都应该解析到InternalResourceViewResolver,最好的方法是什么?
发布于 2012-11-12 16:18:56
我发现Spring MVC3添加了一个我需要的标签,即mvc:view-controller
标签。这就帮我完成了:
<mvc:view-controller path="/" view-name="index" />
<mvc:view-controller path="/admin" view-name="index" />
<mvc:view-controller path="/volume" view-name="index" />
http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html
发布于 2016-12-06 21:38:56
理论上,要通过history.pushState处理导航,您需要为未处理的资源返回index.html。如果你看一下现代web框架的官方文档,它通常是基于404状态实现的。
在spring中,你应该按顺序处理资源:
适用于其他的REST controllers
要做到这一点,你至少有4种可能的解决方案。
使用EmbeddedServletContainerCustomizer和自定义404处理程序
@Controller
static class SpaController {
@RequestMapping("resourceNotFound")
public String handle() {
return "forward:/index.html";
}
}
@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {
return container -> container.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/resourceNotFound"));
}
使用自定义默认请求映射处理程序
@Autowired
private RequestMappingHandlerMapping requestMappingHandlerMapping;
static class SpaWithHistoryPushStateHandler {
}
static class SpaWithHistoryPushStateHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(final Object handler) {
return handler instanceof SpaWithHistoryPushStateHandler;
}
@Override
public ModelAndView handle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
response.getOutputStream().println("default index.html");
return null;
}
@Override
public long getLastModified(final HttpServletRequest request, final Object handler) {
return -1;
}
}
@Bean
public SpaWithHistoryPushStateHandlerAdapter spaWithHistoryPushStateHandlerAdapter() {
return new SpaWithHistoryPushStateHandlerAdapter();
}
@PostConstruct
public void setupDefaultHandler() {
requestMappingHandlerMapping.setDefaultHandler(new SpaWithHistoryPushStateHandler());
}
使用自定义ResourceResolver
@Autowired
private ResourceProperties resourceProperties;
@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations(resourceProperties.getStaticLocations())
.setCachePeriod(resourceProperties.getCachePeriod())
.resourceChain(resourceProperties.getChain().isCache())
.addResolver(new PathResourceResolver() {
@Override
public Resource resolveResource(final HttpServletRequest request, final String requestPath, final List<? extends Resource> locations, final ResourceResolverChain chain) {
final Resource resource = super.resolveResource(request, requestPath, locations, chain);
if (resource != null) {
return resource;
} else {
return super.resolveResource(request, "/index.html", locations, chain);
}
}
});
}
使用自定义ErrorViewResolver
@Bean
public ErrorViewResolver customErrorViewResolver() {
final ModelAndView redirectToIndexHtml = new ModelAndView("forward:/index.html", Collections.emptyMap(), HttpStatus.OK);
return (request, status, model) -> status == HttpStatus.NOT_FOUND ? redirectToIndexHtml : null;
}
摘要
第四种选择看起来最简单,但一如既往地取决于你需要什么。您可能还希望仅当请求需要文本/html时才返回index.html ( BasicErrorController已经基于"produces“标头执行了此操作)。
我希望其中一个选项对您的情况有所帮助。
发布于 2012-11-09 00:17:37
我会给我的urls一个明确的方案,并将前端与后端分开。
一些建议:
/server
开头的请求路由到后端,将所有其他请求路由到frontend.https://stackoverflow.com/questions/13295300
复制相似问题