从spring mvc controller返回json对象列表

时间:2016-08-22 05:35:59

标签: java json spring rest spring-mvc

我想从spring控制器返回json对象列表并在视图中使用它,但它返回空列表! 我无法理解这个问题。 我在minutesController中创建活动对象并将其作为json.But返回。我无法使用jquery在表单中获取它。 但是当我尝试localhost时,我可以在浏览器中看到正确的结果:8080 / FitnessTracker5 / activities.json。 我的服务器是glassfish

addMinutes.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    <%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <html>
    <head>
      <title>Add Minutes Page</title>
      <script type="text/javascript" src="/WEB-INF/pages/jquery-3.1.0.js"></script>
      <script type="text/javascript">
        $(document).ready(
                function(){
                 $.getJSON('localhost:8080/FitnessTracker5/activities.json' , {

                    ajax:'true'
                  }
                  ,
                  function(data){
                    var html = '<option value="">please select one--</option>'
                    var len = data.length;

                    for(var i=0 ; i<len ;i++){
                      html += '<option value="' + data[i].desc + '">'
                      +data[i].desc+ '</option>' ;
                    }
                    html +='</option>';
                  $('#activities').html(html);

                  });
                });

      </script>
    </head>
    <body>
    <h1>Add Minutes Excersisedddd:</h1>


    Language:<a href="?language=en">English</a> | <a href="?language=fa">Persian</a>
    <form:form commandName="exercise">

      <table>
        <tr>

          <td><h1><spring:message code="goal.text"/> </h1> </td>
          <td> <form:input path="minutes"/></td>
          <td>
            <form:select  id="activities" path="activity"></form:select>
          </td>

        </tr>


      </table>
    </form:form>



    </body>
    </html>

activiity.java

public class Activity {
    private String desc ;
    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }


}

MinutesController.java @Controller

public class MinutesController {

    @RequestMapping(value = "/addMinutes")
    public String addMinutes(@ModelAttribute("exercise") Exercise exercise){
        System.out.println("exercise" + exercise.getMinutes());
        System.out.println("exerciseeeee" + exercise.getActivity());
        return "addMinutes" ;
    }

    @RequestMapping(value = "/activities" , method = RequestMethod.GET)
    public @ResponseBody List<Activity> findAllActivities(){
        System.out.print("ACCCCCCCCCCCCCCCCCCCCc");
    List<Activity> activities = new ArrayList<Activity>();

        Activity run = new Activity();
        run.setDesc("Run");
        activities.add(run) ;

        Activity bike = new Activity();
        bike  .setDesc("Bike");
        activities.add(bike) ;

        Activity swim = new Activity();
        swim.setDesc("Swim");
        activities.add(swim) ;

        return activities;


    }

    @RequestMapping(method=RequestMethod.GET, value ="/test")
    public @ResponseBody Activity getMovie( ){

        Activity activity =     new Activity();
        activity.setDesc("aaaaa");
        return activity;
    }
}

2 个答案:

答案 0 :(得分:2)

从上面的评论看来,问题是无法加载JQuery,这可能是由于调度程序servlet配置错误造成的。您可以尝试从CDN访问jquery:

<script type="text/javascript" src="//code.jquery.com/jquery-3.1.0.min.js"></script>

如果你想从你的应用程序提供jquery,你应该谷歌为“服务静态资源Spring MVC”:)

http:// 添加到url

 $.getJSON('http://localhost:8080/FitnessTracker5/activities.json' , {

答案 1 :(得分:0)

使用@RestController代替@Controller