caro
caro

Reputation: 411

Passing parameters from JSP to controller - Spring MVC

I have a little problem when I want to pass parameter to my controller. I have one method in controller:

@RequestMapping("/redirect")

public String print(String type, HttpServletRequest servletRequest)
{
    String path = "redirect:" + servletRequest.getParameter("type");
    return path;
}

And I have some buttons in my jsp file:

<form method="post" action="/WWP/redirect">
<button onclick=" <input type="text" name="type" value="/developer"/>">Developer</button>

<button onclick=<input type="text" name="type" value="/graphic"/>>Graphic</button>

<button onclick=" <input type="text" name="type" value="/cleaner"/>">Cleaner</button>
</form>

In this way I have 3 buttons and when for example I press Developer button variable "type" is "/developer" and I'm sending it to controller. But now my buttons look like this: http://imageshack.us/photo/my-images/854/buttonspx.png/

It works correct, but I can't get rid this HTML tag and it's really nervous. How to do this? Or how send value to controller in different way. Thanks in advance.

Upvotes: 0

Views: 12373

Answers (1)

Usha
Usha

Reputation: 1468

Instead of normal buttons you can have submit buttons and handle them in controller as below

 <input type="submit" value="Developer" name="developer"></input>       
 <input type="submit" value="Graphic" name="graphic"></input>
 <input type="submit" value="Cleaner" name="cleaner"></input>

And you can have different methods in the controller class for each of the buttons clicked using the params attribute like this

@RequestMapping(value = "/redirect", method = {RequestMethod.POST}, params = "developer")
public ModelAndView developerMethod(){
}

 @RequestMapping(value = "/redirect", method = {RequestMethod.POST}, params = "graphic")
public ModelAndView graphicMethod(){
}

 @RequestMapping(value = "/redirect", method = {RequestMethod.POST}, params = "cleaner")
public ModelAndView cleanerMethod(){
}

Upvotes: 4

Related Questions