9

I am using a spring mvc controller. Inside controller i am putting some value lets say string inside model. Now i would like to retrive that value or lets just say print that value inside a javascript. How do i do it? Here is my controller class. I am adding "movie" as key. Now i want to display that name of the movie inside java script (Not inside JSP. However Inside JavaScript)

@Controller
@RequestMapping("/movie")
public class MovieController {

    @RequestMapping(value="/{name}", method = RequestMethod.GET)
    public String getMovie(@PathVariable String name, ModelMap model) {

        model.addAttribute("movie", name);
        return "list";

    }

}

here is my JSP

<html>
<head>
//I want to print movie name inside java script not inside jSP body tag.
<script type="text/javascript">
var movie_name = ${movie};
alert("movies name"+ movie_name);
</script>
</head>
<body>
    <h3>Movie Name : ${movie}</h3>//When i print here its working fine. 
</body>
</html>

3 Answers 3

18

Use this:

var movie_name = "${movie}";

instead of:

var movie_name = ${movie};

When using ${movie}, the value gets placed on the page without quotes. Since I'm guessing it's a string, Javascript requires strings be surrounded by quotes.

If you checked your browser's console, you probably would've seen an error like Unexpected identifier or ___ is not defined.

Sign up to request clarification or add additional context in comments.

9 Comments

Lan i got a small question what if the model.addAttribute("login", new Login("uname","pass")); have some object inside map and i want to print just one attribute of that object inside javascript. Like the object is of Login Class and it has username and password as attribute. Now i want to print just user name inside javascript. How do i do it? For example var login = "${SomeOject}"; alert(login.uname); ?? how do i get individual values of Login Class?
@DheerajVarne What is SomeObject? An Object? In your JSP, you can use ${movie.attribute}, but it depends on how SomeObject is defined
@DheerajVarne In your specific Login example, you'd have to define a method in your Login class called "getUsername", and in your JSP, you'd use ${login.username}. Note that it's all case sensitive
model.addAttribute("someObject", new Login("uname","pass")); //This is what i added in model key value attribute. Now i would like to print just uname inside javascript rather than printing all Login object.
@DheerajVarne Yes, so go back to your Login class. Add 2 new methods like this: public String getUsername() { return this.username; } and public String getPassword() { return this.password; }. Then, in your JSP, use this: var username = "${login.username}"; and `var password = "${login.password}";
|
1

Try this...

If you had added the object into the model as:

model.addAttribute("someObject", new Login("uname","pass"))

Then you get the properties of the model object as

var user_name = ${someObject.uname}; // This is assuming that the Login class has getter as getUname();
var user_pass = ${someObject.pass};

Comments

0
<html>
jsp code ...

<script>

some js code ..
..
var formProperty = <c:out value="${fromBean.property}" />

</script>

..

</html>

This worked for me where formBean is the name of form backing object and property is filed of the form class

Comments

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.