0

my java class:

@RequestMapping(value = "/front", method = RequestMethod.GET) public String onemethod(@RequestParam String name, Model model) { String str = "something"; model.addAttribute("str", str); return "jsppage"; }

jsp page:

        var arrayCollection = ${str}

With this code, I'm getting 404 exception on Tomcat. I'm unable to send java variable to a different jsp page. Any help would be appreciated for this case.

user10274438
  • 123
  • 1
  • 2
  • 10
  • It seems that you want to call a rest service from javascript. visit https://stackoverflow.com/questions/7112672/jquery-ajax-call-to-rest-service – Mohsen Dec 10 '18 at 20:28
  • I put an ajax as you suggested, but data seem to be not going to javascript – user10274438 Dec 10 '18 at 20:40

1 Answers1

1

Ok to wrap this up:

2 choices:

  1. add variable to the model and access it directly in JSP
  2. make it a rest method and call from ajax

Examples:

Ad.1.:

Controller

import org.springframework.ui.Model;

@RequestMapping(value = "/front", method = RequestMethod.GET)
public String onemethod(Model model) throws IOException, ParseException {
    String str = "something";
    model.addAttribute("str", str);
    return "jsppage";
}

JSP ("jsppage")

var test = '${str}';

Ad.2.:

Controller

// just to show JSP
@RequestMapping(value = "/front", method = RequestMethod.GET)
public String onemethod() throws IOException, ParseException {
    return "jsppage";
}

// REST
@ResponseBody
@RequestMapping(value = "/rest", method = RequestMethod.GET)
public String secondmethod() {
    return "something";
}

JSP ("jsppage")

$.ajax({
    method : "get",
    url : "rest",
    dataType : 'text',
    success : function(data) {
        console.log(data);
    },
    error : function(e){
        console.log(e);
    }
});

If you want to also send "name" parameter, add @RequestParam String name to the controller method and call ajax like this:

$.ajax({
    method : "get",
    url : "rest",
    dataType : 'text',
    data : {"name" : "some name"},
    success : function(data) {
        console.log(data);
    },
    error : function(e){
        console.log(e);
    }
});
Konrad Malik
  • 76
  • 1
  • 6
  • thanks.I did it. How would I retrieve it at javascript? – user10274438 Dec 10 '18 at 20:39
  • Ok, now your problems looks different, you have 2 choices here: return to the previous example with "var something = '${str}' and you should have your string "str" in JSP, or do things like you do now but add @ResponseBody to the controller method and send your "name" parameter in "data" attribute in ajax (or set it as @RequestParam(required=false) String name). Then you'll have whatever you have under the "jsppage" in javascript. You can also try changing "dataType" to text since you return a String. – Konrad Malik Dec 10 '18 at 20:42
  • I did as you said, with @responsebody I'm getting data at localhost. but it's not going to javascript. It seems something's wrong in my ajax. can you look at it? – user10274438 Dec 10 '18 at 20:53
  • Please look at my edit and check any of the two examples – Konrad Malik Dec 10 '18 at 21:01
  • I think I'll go with first one. It works fine with request param removed. – user10274438 Dec 10 '18 at 21:26
  • import org.springframework.ui.Model; @RequestMapping(value = "/front", method = RequestMethod.GET) public String onemethod(@RequestParam String name, Model model) throws IOException, ParseException { String str = "something"; model.addAttribute("str", str); return "jsppage"; } – user10274438 Dec 10 '18 at 21:26
  • with request param, added its not working fine for the first way. – user10274438 Dec 10 '18 at 21:28
  • I've modified my question. please look at it. – user10274438 Dec 10 '18 at 21:34
  • Of course, because then you need to provide it on initial GET, for example: localhost:8080/front?name=test, or make it "not required" by: **@RequestParam(required=false) String name** – Konrad Malik Dec 10 '18 at 21:34