2014-11-04 45 views
0

我想沒有任何模板引擎Spring MVC的角度整合的js沒有模板引擎

我查了幾個答案的Spring MVC與角JS整合,但我不解決這個問題。下面我列出了Xml,Controller和Html文件。我在這裏完全錯過了什麼?

APP-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd 
     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"> 
    <annotation-driven /> 
    <context:component-scan base-package="com.angular.app" /> 
    <resources mapping="/views/**" location="/WEB-INF/views/" /> 
    <resources mapping="/resources/**" location="/resources/" /> 
    <beans:bean 
     class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
     <beans:property name="order" value="1" /> 
     <beans:property name="mediaTypes"> 
      <beans:map> 
       <beans:entry key="html" value="text/html" /> 
      </beans:map> 
     </beans:property> 
    </beans:bean> 
    <beans:bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="order" value="2" /> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value="" /> 
    </beans:bean> 
</beans:beans> 

控制器

@RequestMapping(value = "/", method = RequestMethod.GET) 
    public String home(Locale locale, Model model) { 
     logger.info("Welcome home! The client locale is {}.", locale); 

     Date date = new Date(); 
     DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, 
       DateFormat.LONG, locale); 

     String formattedDate = dateFormat.format(date); 

     model.addAttribute("serverTime", formattedDate); 
      return "home.html"; 
    } 
    @RequestMapping(value="/app/welcome",method = RequestMethod.GET) 
    public @ResponseBody String welcome() {  
     logger.info("entering into original controller"); 
     System.out.println("entering into original controller"); 

     String text="Hello World!!!! Welcome"; 

     return text; 

    } 

home.html的

<html> 
<head> 
<script 
    src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script> 
</head> 
<body> 
    <div ng-app=""> 
     <div> 
      Name: <input type="text" ng-model="name"> 
     </div> 
     <p ng-bind="name"></p> 
     <div ng-controller="helloworldcontroller"> 
      <h1>{{title}}</h1> 
     </div> 
    </div> 
    <script> 
     function helloworldcontroller($scope, $http) { 
      $http.get("http://localhost:8080/app/welcome").success(
        function(response) { 
         $scope.title = response; 
         alert("sucess"); 
        }); 
     } 
    </script> 
</body> 
</html> 

回答

0

我工作了一些p-使用體系結構Spring MVC + AngularJs,可以給你提供以下反饋:

你試圖通過Spring MVC控制器來控制html頁面的路由,我認爲這不是最好的方法,因爲AngularJs有它自己的引擎通過ng-route或ui-router控制路由和頁面。

在我的項目體系結構中,使用Spring MVC項目提供了一個完整的REST API,供Web AngularJs應用程序使用。

這個項目的結構如下:

parent-maven-project: 
|_project-core: Have all bussiness logic, data source config, persistence config, Services, Repositories, Entities, etc. 
|_project-web: Have a angularJs App (that controls the html pages routing using ui-router), API REST (Spring MVC Controllers) to be consumed and give the business layer access (project-core). 
|_project-ear: Pack core and web projects to deploy on JBoss EAP 6.2 

在Spring MVC REST層(項目網)|建立一個Spring OAuth2服務器,用於認證用戶並通過Spring Security進行會話訪問控制。當用戶登錄到應用程序時,angularjs應用程序會收到一個訪問令牌承載,該訪問令牌承載用於在用戶請求保持登錄狀態的所有時間內發出請求。所有REST服務都在請求範圍內,是無狀態的,則使用該令牌檢索Spring Security會話並進行所有用戶訪問驗證。