简体   繁体   English

我是否可以在同一应用程序中运行RESTful服务和angularJS以避免交叉浏览器

[英]Am I able to run a RESTful Service and angularJS in the same app to avoid CROSS Browser

i've done a simple RESTful service using Resteasy and JAXRS in Java. 我已经使用Java中的Resteasy和JAXRS完成了一个简单的RESTful服务。 And then i'm trying to call to the service from the "front-end" by using angularJS $http.get. 然后,我尝试通过使用angularJS $ http.get从“前端”调用服务。

By the way, using tomcat 7 to run this. 顺便说一句,使用tomcat 7运行它。

For some reason, with my current web.xml i can't access to the angularJS view with a 404 not found error, but i can access the restful service. 出于某种原因,使用我当前的web.xml,我无法访问404 not found错误,但无法访问angularJS视图,但可以访问Restful服务。

Now, if i add a simple default web.xml, the restful service will no longer work, but the angularJS view will be found. 现在,如果我添加一个简单的默认web.xml,restful服务将不再起作用,但将找到angularJS视图。

Can you guys tell me what's wrong? 你们能告诉我怎么了吗? been looking for this for hours. 一直在寻找这个小时。

This is my pom.xml 这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>angular-js</groupId>
    <artifactId>angular-js</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <properties>
        <java-version>1.7</java-version>
    </properties>

    <repositories>
        <repository>
            <id>org.jboss.resteasy</id>
            <url>http://repository.jboss.org/maven2/</url>
        </repository>
        <repository>
            <id>jboss</id>
            <url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webXml>src\main\webapp\WEB-INF\web.xml</webXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.1.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jaxrs</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jettison-provider</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-multipart-provider</artifactId>
            <version>2.3.6.Final</version>
        </dependency>
        <dependency>
            <groupId>net.sf.scannotation</groupId>
            <artifactId>scannotation</artifactId>
            <version>1.0.2</version>
        </dependency>
        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jettison</groupId>
            <artifactId>jettison</artifactId>
            <version>1.3.4</version>
        </dependency>

        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
            <version>0.11</version>
        </dependency>
    </dependencies>

</project>

This is my web.xml 这是我的web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>AngularJS Application</display-name>

    <welcome-file-list>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
    </listener>

    <servlet>
        <servlet-name>Resteasy</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <context-param>
        <param-name>resteasy.scan</param-name>
        <param-value>true</param-value>
    </context-param>

    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/</param-value>
    </context-param>
</web-app>     

This is my angularJS code, pretty basic: 这是我的angularJS代码,非常基本:

var teamsApp = angular.module('teamsApp', ['ngRoute']);

teamsApp.config(function($routeProvider) {
    $routeProvider
    .when('/view', {
        controller: 'testCtrl',
        templateUrl: 'partials/view1.htm'
    })
    .otherwise({redirectTo: '/view'});
});

teamsApp.controller('testCtrl', function($scope, $http) {
    $scope.title = "This is a testing title";

    $http({
        url: '/service/getAllPersonsJSON',
        method: "GET",
    }).success(function (data, status) {
            $scope.data = data;
    }).error(function (data, status) {
            $scope.status = status;
    });
});

My index.htm is this one 我的index.htm是这个

<!DOCTYPE html>
    <html ng-app="teamsApp">
        <head>
            <title>Testing AngularJS</title>
            <!-- <link rel="stylesheet" type="text/css" href="style/main.css" media="screen" /> --> 
            <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.min.css" media="screen" /> 
        </head>
        <body>
            <div class="view-container">
                <!-- Place holder for views-->
                <div ng-view></div>

            </div>

        <script src="angular/angular.min.js" type="text/javascript"></script>
        <script src="angular/angular-route.js" type="text/javascript"></script>

        <script src="scripts/controllers/controllers.js" type="text/javascript"></script>
        </body>
    </html>

view1.htm is only a div container with {{title}} value in it. view1.htm只是其中具有{{title}}值的div容器。

This is my project structure http://puu.sh/5GEuu.png 这是我的项目结构 http://puu.sh/5GEuu.png

Thanks in advance. 提前致谢。

I think the problem is that the RestEasy servlet is trying to handle your request for index.htm. 我认为问题在于RestEasy Servlet试图处理您对index.htm的请求。

Try changing your web.xml as follows (adding a prefix): 尝试如下更改web.xml(添加前缀):

<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/service/*</url-pattern>
</servlet-mapping>

and your controller: 和您的控制器:

teamsApp.controller('testCtrl', function($scope, $http) {
    $scope.title = "This is a testing title";

    $http({
        url: '/getAllPersonsJSON',
        method: "GET",
    }).success(function (data, status) {
            $scope.data = data;
    }).error(function (data, status) {
            $scope.status = status;
    });
});

This should put index.htm outside of the controller. 这应该将index.htm放在控制器之外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我没有使用angularjs将数据传输到我的宁静Web服务上 - I am not getting data on to my restful web service using angularjs 我无法使用 Selenium 启动浏览器 - I am not able to launch the browser by using Selenium 宁静的服务尝试给出了我无法破译的构建错误 - restful service attempt gives build error that i am unable to decipher 我不能运行我的Maven项目 - I am Not Able to run my Maven Project 当我在 localhost 上运行我的应用程序时,我能够在 html 上显示图像,但是当我部署到云平台时,图像显示为 404 - when I run my app on localhost am able to display images on html but when I deploy to a cloud platform am getting 404 for the images 使用AngularJS消费RESTful Web服务 - Consuming a RESTful Web Service with AngularJS 为什么我不能在Eclipse 3.7(indigo)上运行/调试Google应用程序引擎“ hello,world”? - Why am I not able to run/debug google app engine “hello, world” on eclipse 3.7 (indigo)? 嗨,我遇到了跨浏览器测试的问题。 我不会同时启动两个浏览器 - Hi, I am facing the problem with cross browser testing. I am not getting launched two browser at simultaneously 我可以编译Java程序,但不能运行Java程序 - I am able to compile the java program but not able to run a java program Android-关闭活动后,当我再次运行该应用程序时,两个活动同时运行。 我该如何避免呢? - Android - After closing an activity, when I run the app again, two activities run at the same time. How can I avoid it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM