java
  • spring
  • spring-mvc
  • knockout.js
  • 2015-02-24 49 views 0 likes 
    0

    我有一個應用程序spring mvc,查看模型是jsp頁面。 以前,我用JSTL爲了得到一個Java對象Knockoutjs從彈簧控制器加載數據

    <select name="parallel" class="task-parallel"> 
        <c:forEach var="listSchoolClasses" items="${listSchoolClasses}"> 
        <option value='${listSchoolClasses.id}'>${listSchoolClasses.name}</option> 
        </c:forEach> 
    </select> 
    

    我的控制器:

    @Controller 
    public class PageController { 
    
        @Autowired 
        SchoolClassesService schoolClassesService; 
    
        @RequestMapping("/tasks.htm") 
        public ModelAndView tasks() { 
    
        ModelAndView model = new ModelAndView("tasks"); 
        model.addObject("listSchoolClasses", schoolClassesService.getAllSchoolClasses()); 
    
        return model; 
    } 
    

    但現在我想用淘汰賽......用我如何在JSP頁面中的對象像這樣?

    <select data-bind="options: ${listSchoolClasses}, value: ${listSchoolClasses.id}, optionsText: ${listSchoolClasses.name}"></select> 
    
    +0

    使用'@ ResponseBody'或'@ RestController'? – chrylis 2015-02-24 12:04:42

    +0

    你能描述一下這些細節嗎? – Mary 2015-02-24 12:11:10

    回答

    0

    我想不出更好的東西,而不是做一個Ajax請求

    $(document).ready(function() { 
    
         function getAllSchoolClasses(){ 
          var schoolClasses = []; 
            $.ajax({ 
             type: "GET", 
             url: cr + "/task/getAllSchoolClasses.action", 
             dataType: "json", 
             success: function (data) { 
              for(var i=0; i<data.length; i++) 
               schoolClasses[i] = {classID:data[i].id, className:data[i].name}; 
              //schoolClasses[i] = {classID:data[i].id, className:data[i].name}; 
             }, 
             async:false 
            }); 
            return schoolClasses; 
           } 
    
        var maxScoreModel = function (id, score, schoolclass) { 
         var self = this; 
    
         self.schoolClasses = getAllSchoolClasses(); 
         self.scores = [ 
          { val: 1 }, 
          { val: 2 }, 
          { val: 3 }, 
          { val: 4 }, 
          { val: 5 } 
         ]; 
         self.id = ko.observable(id); 
         self.selectedMaxScore = ko.observable(score); 
         self.selectedSchoolClass = ko.observable(schoolclass); 
        };   
    
    
         var taskViewModel = function() { 
           var self = this; 
    
    
         self.maxScores = ko.observableArray([ 
          new maxScoreModel(), 
          new maxScoreModel() 
         ]); 
    
         self.addMaxScore = function(id,score, schoolclass) { 
          self.maxScores.push(new maxScoreModel(id,score, schoolclass)); 
         }; 
         self.removeMaxScore = function(maxScore) { self.maxScores.remove(maxScore) }; 
    
    
         }; 
    
    
          ko.applyBindings(new taskViewModel()); 
        }); 
    

    JSP頁面:

    <div class="container" data-bind="foreach: maxScores"> 
          <select data-bind="options: schoolClasses, optionsText: 'className', optionsValue: 'classID', 
              value: selectedSchoolClass, optionsCaption: 'Choose..'"> 
          </select> 
          <select data-bind="options: scores, optionsText: 'val', optionsValue: 'val', 
             value: selectedMaxScore, optionsCaption: 'Choose..'"> 
          </select> 
          <a href="#" data-bind="click: $root.removeMaxScore"><spring:message code="form.field.remove"/> </a> 
          <br/> 
         </div> 
         <button data-bind="click: addMaxScore, enable: maxScores().length < 3"><spring:message code="form.field.add"/> </button> 
         </div> 
    
    相關問題