ric*_*ico 5 spring-mvc tomcat7 angularjs
我希望能够在客户端使用Spring MVC作为REST服务器和AngularJS.
我有几个REST的网址:
我有几个UI的网址:
由于AngularJS在客户端执行技巧,我只想将所有默认的UI(而不是其余的)重定向到AngularJS使用的index.html文件.
所以,在Spring MVC配置中,我希望能够做到这样的事情:
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
有了它,我想将所有UI URL处理委托给AngularJS.
我还想要,如果用户在浏览器中写了一个坏URL,他将被Spring MVC重定向到index.html文件,它将是AngularJS,它将在错误页面上进行重定向.我在Web上看到了几个带有单个index.html文件的项目,但没有人处理这个错误情况.
我一直在努力尝试这个技巧,但我无法找到解决方案.
所以我的问题是:我怎么能这样做?更一般地说,我错误的是这个Spring MVC-AngularJS想要的配置?
非常重要:我使用没有web.xml的Spring MVC 3.2和Tomcat 7.34(完整的Servlet 3.0)
非常感谢.
也许可以通过$routeProvider解决它:
$routeProvider.when('/redirect/:redirectParams', {templateUrl: 'partials/homePartial', controller: redirectController});
$routeProvider.when('/home', {templateUrl: 'partials/homePartial', controller: homeController});
$routeProvider.when('/someRoute', {templateUrl: 'partials/somePartial', controller: someController});
$routeProvider.when('/error/', {templateUrl: 'partials/errorPartial', controller: errorController});
$routeProvider.when('/error/:errorMessage', {templateUrl: 'partials/errorPartial', controller: errorController});
$routeProvider.otherwise({redirectTo: '/error'})
Run Code Online (Sandbox Code Playgroud)
$routeProvider当找不到路由时,让重定向到错误页面即可。
编辑:
我在上面的例子中添加了一个redirectController。$routeParams在本例中,该控制器将读取$routeParams.redirectParams, 并使用$location将路由更改为/error。
Spring 只是重定向到http://host:port/index.html/#/redirect/error=blabla. 您可以而且应该对此进行微调并支持多个routeParams。
在 Spring 中,您基本上会有三个请求映射:
重定向所有其他请求:
@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7765 次 |
| 最近记录: |