1

I want to return json object list from spring controller and use it in view but it returns empty list! I can not understand the problem. I create activities objects in minutesController and return it as json.But I can't get it in form using jquery. But I can see correct result in browser when I try localhost:8080/FitnessTracker5/activities.json. My server is 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;
    }
}
5
  • Any specific reason why you are using '.json' in your ajax call?? Commented Aug 22, 2016 at 6:07
  • 1
    Have you tried a breakpoint in your Javascript? What is the value of "data" in your ajax callback? Commented Aug 22, 2016 at 6:09
  • @marthursson it seems it can not get "jquery-3.1.0.js"! I checked chrome console and it has error...Should I do something else to add it? Commented Aug 22, 2016 at 6:31
  • @AbdullahWasi I removed it but it is not problem Commented Aug 22, 2016 at 6:31
  • @marthursson please help me...how can I add jquery correctly? Commented Aug 22, 2016 at 6:46

2 Answers 2

2

From your comment above it appears the problem is that JQuery cannot be loaded, which is probably due to a misconfigured dispatcher servlet. You might try accessing jquery from the CDN instead:

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

If you want to serve jquery from your application you should google for "serve static resources Spring MVC" :)

And add http:// to url.

 $.getJSON('http://localhost:8080/FitnessTracker5/activities.json' , {
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You...But now I get this erro in chrome :XMLHttpRequest cannot load localhost:8080/FitnessTracker5/activities.json?ajax=true. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-..........and yet empty list in firefox without error
0

Use @RestController instead @Controller.

2 Comments

Thank's But I understood the problem is loading jqueru..I see error in chrome console in the line that is adding jquery...Can you help me ?
not related to the overall answer, but you need to also annotate the method with @ResponseBody to set controller to avoid looking at a jsp page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.