Spring mvc 使用AngularJS配置Spring MVC
我希望能够使用SpringMVC作为REST服务器,并在客户端使用AngularJS 我有几个REST的URL:Spring mvc 使用AngularJS配置Spring MVC,spring-mvc,angularjs,tomcat7,Spring Mvc,Angularjs,Tomcat7,我希望能够使用SpringMVC作为REST服务器,并在客户端使用AngularJS 我有几个REST的URL: /休息/产品 /rest/products/{id} 我有几个UI的URL: /商店/产品 /商店/产品/{id} 因为是AngularJS在客户端完成了这个任务,所以我只希望能够将所有默认的ui URL(而不是其他URL)重定向到AngularJS使用的index.html文件 因此,在Spring MVC配置中,我希望能够做到以下几点: @EnableWebMvc @Co
- /休息/产品
- /rest/products/{id}
- /商店/产品
- /商店/产品/{id}
@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "com.mypackage.web")
public class WebAppConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/**").setViewName("index");
}
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".html");
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
这样,我想将所有UI URL处理委托给AngularJS
我还希望,如果用户在浏览器中写入错误的url,他将被Spring MVC在index.html文件中重定向,而AngularJS将在错误ui页面上执行重定向。我在web上看到过几个使用单个index.html文件的项目,但是没有人处理这个错误案例
我一直在努力做这个把戏,但我找不到解决办法
所以我的问题是:我该怎么做?更一般地说,我对这个Spring MVC AngularJS想要的配置有错吗
非常重要:我使用SpringMVC3.2和Tomcat7.34,但不使用web.xml(完整的Servlet3.0)
非常感谢。也许可以通过以下方式解决: 当找不到路由时,只需让
$routeProvider
重定向到错误页面
编辑:
在上面的示例中,我添加了一个重定向控制器。此控制器将读取$routeParams
,在本例中为$routeParams.redirectParams
,并使用将路由更改为/error
Spring只是重定向到http://host:port/index.html/#/redirect/error=blabla
。您可以也应该对此进行微调,并支持多个RouteParam
在春天,你基本上会有三个:
- index.html请求映射
- 其他URL请求映射
@RequestMapping(value = "{path}", method = RequestMethod.GET)
public String redirect(@PathVariable String path) {
String route = null;
if (path.equals("/") || path.startsWith("/index.html")) {
// load index.html
} else {
route = "redirect:/index.html/#/redirect/" + path;
}
return route;
}
我认为您可以编写一个拦截器,它允许一些已知的URL,如/rest/、/resources/、/webjars/*,对于任何其他URL,请重定向到index.html尝试使用
这应该能帮你完成工作。您必须在web.xml中配置/添加它,以处理每个url,您可以操纵它重定向到index.html我知道有点晚了,但万一有人遇到同样的问题,下面是放在类路径urlwrite.xml中的url过滤器示例 您可以设置要筛选的某些URL的重定向
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<!-- Configuration file for UrlRewriteFilter http://www.tuckey.org/urlrewrite/ -->
<urlrewrite>
<rule>
<note>
The rule means that requests to /test/status/ will be redirected
to
/rewrite-status
the url will be rewritten.
</note>
<from>/index.ipxs/directory</from>
<to type="redirect">/index.ipxs/marketplace</to>
</rule>
<rule>
<from>/index.ipxs/faq</from>
<to type="redirect">/index.ipxs/marketplace</to>
</rule>
<rule>
<from>/index.ipxs/marketplace</from>
<to type="redirect">/iPlexus/index.ipxs</to>
</rule>
</urlrewrite>
该规则意味着对/test/status/的请求将被重定向
到
/重写状态
url将被重写。
/index.ipxs/directory
/index.ipxs/marketplace
/index.ipxs/faq
/index.ipxs/marketplace
/index.ipxs/marketplace
/iPlexus/index.ipxs
这就是我需要在angularjs配置文件中执行的操作。但是我需要先访问index.html文件。如果我在浏览器中输入'localhost:8080/',或者这样就可以了。但如果我把“localhost:8080/blabla”放进去?我希望Spring MVC在index.html文件上重定向我,然后AngularJS可以做他想做的事情,特别是使用$routeProvider。为什么需要重定向到index.html?是否要在主页上显示错误消息?我希望Spring MVC能够将所有请求URL(restful URL除外)映射到index.html视图。无论请求url是什么,我都希望显示index.html视图。然后,AngularJS,根据请求的url,知道使用$routeProvider机制显示哪个“真实”视图,正如您完美展示的那样。事实上,感谢AngularJS,index.html可以处理多个视图。抱歉,您考虑到您已经在index.html文件上了。只有在键入url“”或“”时,才能访问index.html。但是,如果键入“”这是Spring MVC Dispatcher servlet未知的操作,则在访问index.html文件之前将出现404错误。我的问题是SpringMVCConfigOne:我想要访问index.html,不管url键入什么。这就是我尝试registry.addViewController(“/**”).setViewName(“索引”)的原因,但它不起作用。谢谢,那么?您可以让Spring从“host:port/shop/products”变成“host:port/index.html”。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
"http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
<!-- Configuration file for UrlRewriteFilter http://www.tuckey.org/urlrewrite/ -->
<urlrewrite>
<rule>
<note>
The rule means that requests to /test/status/ will be redirected
to
/rewrite-status
the url will be rewritten.
</note>
<from>/index.ipxs/directory</from>
<to type="redirect">/index.ipxs/marketplace</to>
</rule>
<rule>
<from>/index.ipxs/faq</from>
<to type="redirect">/index.ipxs/marketplace</to>
</rule>
<rule>
<from>/index.ipxs/marketplace</from>
<to type="redirect">/iPlexus/index.ipxs</to>
</rule>
</urlrewrite>