【问题标题】:Configure Spring MVC with AngularJS使用 AngularJS 配置 Spring MVC
【发布时间】:2012-12-16 05:39:12
【问题描述】:

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

我有几个 REST 网址:

  • /休息/产品
  • /rest/products/{id}

我有几个用户界面的网址:

  • /商店/产品
  • /shop/products/{id}

因为它是 AngularJS 在客户端做的伎俩,我只想能够将所有默认的 ui url(不是其余的)重定向到 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();
    }

}

这样,我想将所有 UI url 处理委托给 AngularJS。

我还希望,如果用户在浏览器中写入错误的 url,Spring MVC 会在 index.html 文件上重定向他,AngularJS 将在错误 ui 页面上进行重定向。我在网上看到过几个项目只有一个 index.html 文件,但没有人处理这种错误情况。

我一直在努力尝试这个技巧,但我找不到解决方案。

所以我的问题是:我该怎么做?更一般地说,我对这个 Spring MVC-AngularJS 想要的配置有错吗?

非常重要:我使用没有 web.xml 的 Spring MVC 3.2 和 Tomcat 7.34(完整的 Servlet 3.0)

非常感谢。

【问题讨论】:

    标签: spring-mvc angularjs tomcat7


    【解决方案1】:

    我知道它有点晚了,但如果有人遇到同样的麻烦,这里是放在你的类路径 urlwrite.xml 中的 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>
    

    【讨论】:

      【解决方案2】:

      尝试使用urlrewritefilter

      这应该为您完成工作。您必须在 web.xml 中配置/添加它,以便处理每个 url,并且您可以操纵它以重定向到 index.html

      【讨论】:

        【解决方案3】:

        我认为您可以编写一个 Interceptor 来允许一些已知的 URL,例如 /rest/、/resources/、/webjars/* 和对于任何其他 URL 重定向到 index.html

        【讨论】:

          【解决方案4】:

          或许可以通过$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'})
          

          在找不到路由的时候让$routeProvider重定向到错误页面。

          编辑:

          我在上面的例子中添加了一个redirectController。此控制器将读取$routeParams,在本例中为$routeParams.redirectParams,并使用$location 将路由更改为/error

          Spring 只是重定向到http://host:port/index.html/#/redirect/error=blabla。您可以而且应该对此进行微调并支持多个 routeParams。

          在春季,您基本上会拥有三个request mappings

          重定向所有其他请求:

          @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;
          }
          

          【讨论】:

          • 这是我需要在 angularjs 配置文件中做的事情。但我需要先访问 index.html 文件。如果我输入浏览器'localhost:8080/',或者那很好。但如果我输入 'localhost:8080/blabla' 呢?我希望 Spring MVC 在 index.html 文件上重定向我,然后 AngularJS 可以做他想做的事,特别是使用 $routeProvider。
          • 为什么需要重定向到index.html?您想在主页上显示错误消息吗?
          • 我希望 Spring MVC 能够将所有请求 url(restful 除外)映射到 index.html 视图。无论请求 url 是什么,我都想显示 index.html 视图。然后,AngularJS 根据请求的 url,知道要显示哪个“真实”视图,使用 $routeProvider 机制,正如您完美展示的那样。事实上,多亏了 AngularJS,index.html 可以处理多个视图。
          • 对不起,但您考虑到您已经“打开”了 index.html 文件。只有输入 url 'host:port' 或 'host:port/index.html' 才能访问 index.html。但是,如果您键入“host:port/shop/products”,这是 Spring MVC Dispatcher servlet 未知的操作,您将在访问 index.html 文件之前收到 404 错误。我的问题是 Spring MVC 配置之一:无论输入什么 url,我都想访问 index.html。这就是为什么我尝试 registry.addViewController("/**").setViewName("index") 但它不起作用。谢谢。
          • 所以?你让 Spring redirect 从 'host:port/shop/products' 到 'host:port/index.html'。
          猜你喜欢
          • 1970-01-01
          • 2014-08-09
          • 2019-05-02
          • 2014-06-08
          • 2015-06-06
          • 2014-05-21
          • 2011-01-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多