与AngularJS配置Spring MVC的 [英] Configure Spring MVC with AngularJS

查看:236
本文介绍了与AngularJS配置Spring MVC的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够使用Spring MVC的REST作为服务器和AngularJS在客户端。

I would like to be able to use Spring MVC as REST server and AngularJS on client side.

我有几个网址,REST:

I have several urls for REST :


  • / REST /产品

  • / REST /产品/ {ID}

和我对UI多个URL:

And i have several urls for the UI :


  • /店/产品

  • /店/产品/ {ID}

既然是AngularJS该做的伎俩在客户端,我只是希望能够将所有默认的UI的URL(而不是其余的)重定向到AngularJS使用的index.html文件。

Since it is AngularJS which do the trick on client side, i just want to be able to redirect all default ui urls (not the rest ones) to the index.html file used by AngularJS.

所以,在Spring MVC的配置,我想能够做这样的事情:

So, in Spring MVC configuration, i would like to be able to do something like that :

@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。

With that, i want to delegate all the UI urls handling to AngularJS.

我也想,如果用户在浏览器中写入一个错误的URL,他将被Spring MVC中的index.html文件重定向,这将是AngularJS它会做错误的UI页面上的重定向。我已经看到了一个index.html文件在网络上几个项目,但没有人处理此错误情况。

I also want that if the user writes a bad url in the browser he would be redirected by Spring MVC on the index.html file and it will be AngularJS which will do the redirect on the error ui page. I have seen several projects on the web with a single index.html file, but no one handle this error case.

我一直在挣扎了很多时间试图做这一招,但我不能找到一个解决方案。

I have been struggling a lot of time trying to do this trick but i can't find a solution.

所以我的问题是:我怎么能做到这一点?更一般地,我错了这个春天MVC-AngularJS想配置?

So my question is : how can i do that ? and more generally, am i wrong with this Spring MVC-AngularJS wanted configuration ?

很重要:我使用Spring MVC的3.2和Tomcat 7.34无的web.xml(完整的Servlet 3.0)

Very important : I use Spring MVC 3.2 and Tomcat 7.34 without web.xml (full Servlet 3.0)

非常感谢。

推荐答案

请尝试使用 urlrewritefilter

这应该做的工作适合你。你必须配置/在你的web.xml将它添加到每个URL将被处理,你可以操纵它重定向到的index.html

This should do the work for you. You'll have to configure/add it in your web.xml to every url would be handled and you can manipulate it to redirect to the index.html

这篇关于与AngularJS配置Spring MVC的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆