【问题标题】:AngularJs doesn't load JSON file from local "Maven Project"AngularJs 不会从本地“Maven 项目”加载 JSON 文件
【发布时间】:2016-02-06 20:43:04
【问题描述】:

我正在使用 AngularJs 构建 Web 应用程序,尝试为我的应用程序使用 angular-translate。 这是文档: http://www.ng-newsletter.com/posts/angular-translate.html

$translateProvider.useStaticFilesLoader({
  prefix: 'assets/resources/',
  suffix: '.json'
});

我的项目目录中有 2 个 json 文件

webapp/assets/resources/locale-en_US.json
webapp/assets/resources/locale-ru_RU.json

当我在浏览器中运行我的应用程序时,我收到了这个错误

Failed to load resource: the server responded with a status of 404 (Not Found) 
http://localhost:8080/assets/resources/locale-ru_RU.json

我尝试从我的 json 文件所在的同一目录中加载 .js 文件和 .jpg

webapp/assets/resources/foo.jpg

一切正常,我不能只从本地加载 JSON 文件。我尝试发出 $http 请求但没有结果。

$http.get('assets/resources/locale-en_US.json' ).success(function(data) {
   console.log(data);
});

看起来我的项目忽略了本地的 json 文件。我进行了研究并找到了一些将映射添加到 web.xml 的建议。没有结果

<mime-mapping>
  <extension>json</extension>
  <mime-type>application/json</mime-type>
</mime-mapping>

知道该怎么做吗?

问候, 加里.E

【问题讨论】:

  • 执行$http.get()的js文件在什么位置?
  • 我找到了我的问题的解决方案,谢谢:)

标签: javascript json angularjs maven web.xml


【解决方案1】:

我的 web.xml

中有 servlet-mapping
<servlet-mapping>
    <servlet-name>shemoGeServlet</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

这是在 WEB-INF 目录中搜索 jsno 文件,但我的 jsno 文件在 assets 目录中.这是我的 servlet-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:resources location="/assets" mapping="/assets/**"/>

    <context:component-scan base-package="ge.shemo"/>

     <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
        <property name="order" value="1" />
        <property name="contentNegotiationManager">
            <bean class="org.springframework.web.accept.ContentNegotiationManager">
                <constructor-arg>
                    <bean class="org.springframework.web.accept.PathExtensionContentNegotiationStrategy">
                        <constructor-arg>
                            <map>
                                <entry key="json" value="application/json"/>
                                <entry key="xml" value="application/xml"/>
                            </map>
                        </constructor-arg>
                    </bean>
                </constructor-arg>
            </bean>
        </property>


        <property name="defaultViews">
            <list>
                <!-- JSON View -->
                <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
                <bean class="org.springframework.web.servlet.view.xml.MarshallingView">
                    <constructor-arg>
                        <bean class="org.springframework.oxm.xstream.XStreamMarshaller">
                            <property name="autodetectAnnotations" value="true" />
                        </bean>
                    </constructor-arg>
                </bean>
            </list>
        </property>
    </bean>

    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" p:basename="messages"></bean>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" p:order="0"/>

</beans>

这就是我得到的原因

Failed to load resource: the server responded with a status of 404 (Not Found) 

我删除了 servlet,问题消失了

<servlet-mapping>
    <servlet-name>shemoGeServlet</servlet-name>
    <url-pattern>*.json</url-pattern>
</servlet-mapping>

【讨论】:

    猜你喜欢
    • 2017-05-01
    • 2016-01-14
    • 2022-08-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-29
    • 2011-11-12
    • 1970-01-01
    相关资源
    最近更新 更多