簡體   English   中英

從Angular $ http調用Spring控制器方法時出現404錯誤

[英]404 error when calling Spring controller method from Angular $http

我一直在嘗試運行一個使用Spring 4.2.5和AngularJs的示例單頁應用程序。 我試圖從Angular的$http服務中調用Controller中的方法之一。

在某些控制器內:

$scope.goTosearchList = function() {
        $http.get("/rest/getSearchList").success(function(data){
            $scope.data=data;
            alert("success "+data);
        })
        $location.path("/rest/searchList");
    };

嘗試在控制器中捕獲此請求:

@Controller

public class MyPortController {

    @Autowired
    DatabaseDao dao;

    @RequestMapping(value="/getSearchList",method=RequestMethod.GET)
    public void searchbyid() {

        System.out.println("hello, we r in java");
        dao.databaseConnect();

    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyPort</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>

  <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

這是我的spring servlet:

<?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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="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.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.mmt.controller" />
    <mvc:annotation-driven></mvc:annotation-driven>
   <!--  <bean id="searchbyid" class="com.mmt.dao.DatabaseDao">
    </bean> -->

    </beans>

問題聽起來有點天真,但我找不到出路。 請幫我。

開始了。

首先,您需要創建dispatcher-servlet.xml並在此處放置必要的spring配置,然后將其放在classpath:WEB-INF/ ,因此完整路徑為classpath:WEB-INF/dispatcher-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema    /mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-    context.xsd">

    <context:component-scan base-package="a.b.c" />
</beans>

其中abc是控制器MyPortController基本包。 這個配置將告訴spring從指定的包中自動掃描bean s(這里是controller )。

請注意,為什么它的名字是dispatcher-servlet.xml 這是因為spring將加載以<servlet-name/>開頭和-servlet.xml結尾的-servlet.xml 否則,您應該在servlet設置中指定它,如下所示:

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/applicationContext.xml</param-value>
    </init-param>
</servlet>

它應該工作,享受它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM