春天不见控制器

时间:2015-12-06 15:30:32

标签: java angularjs spring spring-mvc

我的spring MVC应用程序出了问题。当我尝试在弹簧控制器和角度控制器之间传递数据时,我的应用程序看不到我的控制器。

Spring配置:

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>PiManager</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

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

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

调度-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:jpa="http://www.springframework.org/schema/data/jpa"
 xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
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/data/jpa 
                    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
                    http://www.springframework.org/schema/context 
                    http://www.springframework.org/schema/context/spring-context-4.0.xsd
                    http://www.springframework.org/schema/jdbc 
                    http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
                    http://www.springframework.org/schema/mvc 
                    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd ">
<mvc:annotation-driven />
<context:component-scan base-package="org.inz.controller" />

<jpa:repositories base-package="org.inz.repositories"/>

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

我的services.js

PiManager.services.main = [ '$http', '$rootScope', '$state', function($http, $rootScope, $state) {
var self = this;

self.getTestEntity = function(id) {
    return $http.get("/PiManager/index/"+id);
}
} ];

app.service('mainService', PiManager.services.main);

我的controller.js

"use strict"

PiManager.controllers = {};

PiManager.controllers.main =
['mainService', '$scope', '$state', '$http', function(mainService, $scope, $state, $http) {

$scope.getTest = function(id) {
    mainService.getTestEntity(id).then(function(response) {
        $scope.testEntityPart = response;
    });
}

}];
 app.controller('mainController', PiManager.controllers.main);

我的春季主控制器

@Controller
@RequestMapping("/PiManager/")
public class MainController {

@Autowired
private TestEntityRepository testEntityRepository;

@RequestMapping(value = "/main")
public String index() {
    return "/index.jsp";
}

@ResponseBody
@RequestMapping(value = "/index/{id}", method = RequestMethod.GET)
public List<TestEntity> getTestEntity(@PathVariable("id") Long id) {
    return testEntityRepository.findById(id);
}
}

所以当我输入http://localhost:8080/PiManager/index/1时,我得到了

GET http://localhost:8080/PiManager/index/1 404 (Not Found)

我并不知道如何使其发挥作用。

1 个答案:

答案 0 :(得分:0)

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