0

I want to go with an object to another page and show it there

ViewControllers

    @Override
public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/songs")
                .setViewName("/song.html");
        registry.addViewController("/")
                .setViewName("/index.html");
}

Controller

@RestController
@RequestMapping("/songs")
public class SongController {
@RequestMapping("/getSong")
public Track getSong(String id){
    System.out.println("Song id " + id);
    Optional<Track> byId = trackService.findById(Integer.parseInt(id));
    if(!byId.isPresent()) throw new NullPointerException();
    return byId.get();
}

and JQuery

  $.ajax({
            type: "POST",
            url: "/songs/getSong",
            data: {
                id: songId
            }
        });

it calls method but i dont go to another page) what should i do to reach another page with this object or with id without using jsp or thymeleaf?

  • If you want to transfer to another page with the result of a request, do not use ajax. The primary point of using ajax is to avoid a page transfer. Use a normal form submit. – Taplar May 08 '18 at 20:19
  • ['code'](window.location = "/song?id="+songId;) i have tryed like this, it does hange page but i couldnt get id, it was null( – Chocologocal May 08 '18 at 20:25
  • That might could work, given that your endpoint mapping doesn't show that it must be a POST request. The if songId is null, then you have another issue related to that, which is not identifiable with the logic you have provided so far. – Taplar May 08 '18 at 20:26
  • it was null only in controllers method, in brouser it was a number. @Taplar it must be a POST request Result the same with GET – Chocologocal May 08 '18 at 20:30
  • https://stackoverflow.com/questions/19627961/spring-requestmapping You have to configure where the parameter is coming from for the request mapping – Taplar May 08 '18 at 20:31
  • i know) i did like this @RequestMapping("/getSong{id}") public Track getSong(@PathVariable("id") String id)' – Chocologocal May 08 '18 at 20:34
  • `@RequestMapping("/getSong{id}") ` does not match `/song?id=` you said you tried with the window.location – Taplar May 08 '18 at 20:35
  • should it be like this `@RequestMapping("/getSong/{id}")`? – Chocologocal May 08 '18 at 20:36
  • It has to match whatever pattern you are putting in the url. Make it match exactly the same, putting `{id}` where ever the variable is in the url – Taplar May 08 '18 at 20:37
  • `window.location = "/songs/getSong"+songId; `got it) ty) but i didnt get how it works (not about url or method. i just get an emty page with json and dont know what to do with it) – Chocologocal May 08 '18 at 20:47
  • I mean is there is a way to do it like when you return ModelAndView ? – Chocologocal May 08 '18 at 21:00
  • You're getting into spring territory there. I'll have to direct you towards other users who are using spring more frequently. Sadly I haven't done spring in a few years. – Taplar May 08 '18 at 21:01
  • @taplar i did it this way `$('#songs').on("click", function (event) { window.location = "/song?id="+$(event.target).closest('tr').attr('id'); });` and on song page `var songId = $.urlParam('id'); $.ajax({ type: "POST", url: "/songs/getById", data: { id: songId }, success: function (response) { $("#songTitle").text(response.title); } });` is it looks weird? – Chocologocal May 09 '18 at 17:03

0 Answers0