【发布时间】:2016-05-07 21:25:39
【问题描述】:
我一直在尝试运行一个使用 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>
问题可能听起来有点幼稚,但我无法找到出路。请帮帮我。
【问题讨论】:
-
尝试将请求路径更改为 localhost:8080/rest/getSearchList(如果您在 localhost 上运行它)。
-
你还有其他工作处理程序吗?
-
@AlexandreFillatre 不,实际上我是第一次尝试它。我是否缺少处理程序配置?
-
@ElenaChubukina 我尝试使用它,但它会引发 404 错误。我想配置一些也可以在全球范围内工作的东西,而不仅仅是在本地。
-
我敢打赌,您错过了包扫描配置,因此您的处理程序从未注册。你能分享你的
spring-config.xml文件吗?
标签: java angularjs spring spring-mvc