gjosh
gjosh

Reputation: 135

need to pass value from javascript to Spring mvc controller

I am doing encryption and decryption of message at javascript and want to send result message to the spring controller mvc.

@RequestMapping(value = "/token")
public @ResponseBody String getAllBooks(@RequestParam boolean isAuth, @ModelAttribute("somedata") Token data) {
    ModelAndView mav = new ModelAndView();
    mav.addObject("encryptedToken", data.getValue()); return token;}

Hence the token should hold the value from javascript. I am not using jquery. How to get that value from javascript to Spring mvc controller.

<script>
function Encrypt(msg) {
...................
...................
return encryptedFinally;}
</script>

I want to use this variable (i.e. encryptedFinally) in java controller class.

Upvotes: 2

Views: 3239

Answers (2)

MaVVamaldo
MaVVamaldo

Reputation: 2535

I think that you have a number of choices:

  1. Add the variable into the Model object (you can follow the @WilQu suggestion for this)
  2. Pass the variable asynchronously with AJAX
  3. pass the variable as a URI parameter like so .../my/path/{var}/something/else/if/it/makes/sense
  4. pass the variable as a query parameter like so /my/path?var=value

At first glance, for your problem I'd choose option number one.

Upvotes: 1

WilQu
WilQu

Reputation: 7383

Add a hidden input into your form and set the value of this input to the result of the encryption from your javascript code.

Upvotes: 0

Related Questions