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.

2
  • It seems that you want to call a rest service from javascript. visit stackoverflow.com/questions/7112672/… Commented Dec 10, 2018 at 20:28
  • I put an ajax as you suggested, but data seem to be not going to javascript Commented Dec 10, 2018 at 20:40

1 Answer 1

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);
    }
});
Sign up to request clarification or add additional context in comments.

9 Comments

thanks.I did it. How would I retrieve it at javascript?
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.
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?
Please look at my edit and check any of the two examples
I think I'll go with first one. It works fine with request param removed.
|

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.