0

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.

1 Answer 1

4

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

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.