2
votes

i have code like this.

@RequestMapping(value="/persons",method=RequestMethod.POST) public @ResponseBody String addUser(@RequestParam String name){

returnText=name;
 //List<Person> personList = personService.getAllzonedetails(returnText);
System.out.println(returnText);
 //model.addAttribute("personsajax", personList);
 return **returnText**;

}

@RequestMapping(value = "/persons", method = RequestMethod.GET)
public String getPersons(Model model) {

    newvariable = returnText;
    System.out.println(newvariable); 
 logger.debug("Received request to show all persons");
 // Retrieve all persons by delegating the call to PersonService
 List<Person> persons = personService.getAll();
 List<Person> persons1 = personService.getAllzonedetails(newvariable);
     // Attach persons to the Model
 model.addAttribute("persons", persons);
 model.addAttribute("personsajax", persons1);
 // This will resolve to /WEB-INF/jsp/personspage.jsp
 return "personspage";
}

i want to get the returnText post value in System.out.println(newvariable); in get method to pass the jsp page.

any other way to pass the post value in jsp.

Thanks...

1

1 Answers

0
votes

I think what you are trying to do is when the user is added in the post method then you want to redirect to the GET method and on the persons list page you want to show the new added user's name.

What you have tried here is not proper. First of all you need to change the @ResponseBody annotation from the POST method and simple return the "redirect:/persons" string from that function. That will redirect to your GET method. Now to use the newly added user name in the GET method you need to use RedirectAttributes to return the name to the GET method.

For that in your post method signature add one more parameter, RedirectAttributes redirectAttributes. Then insert below line just before your return statement.

redirectAttributes.addFlashAttribute("newUserName", returnText);

Then you can access the value in your JSP using EL ${newUserName}.

I hope this will help you.