从Angular $ http调用Spring控制器方法时出现404错误

时间:2016-05-07 21:25:40

标签: java angularjs spring spring-mvc

我一直在尝试运行一个使用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>

这是我的春天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>

问题可能听起来有点天真,但我无法找到出路。请帮帮我。

1 个答案:

答案 0 :(得分:0)

我们走了。

首先,您需要创建dispatcher-servlet.xml并在此处输入必要的弹簧配置,然后将其放在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>

a.b.c是控制器MyPortController的基础包。此配置将告诉spring自动从指定的包中扫描bean s(此处为controller)。

请注意,为什么它的名字是dispatcher-servlet.xml?这是因为spring会加载名为以<servlet-name/>开头并以-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>

它应该有效,享受它。