0

How to send data from jsp to controller in spring using form ?

<form:form  action="${searchUrl}" method="post">

    <div class="form-group">
        <label class="control-label"> search by ID </label> 
        <br>
           <input type="text" id="ticketId" placeholder=" Enter ticket ID ">                
    </div>

</form:form>

Could not able to get ticketId using this method

baitmbarek
  • 2,440
  • 4
  • 18
  • 26

2 Answers2

0

You need to declare name attribute (name="ticketId") and access the same on the controller. Like below.

JSP:

<input type="text" name="ticketId" id="ticketId" placeholder=" Enter ticket ID">

Controller:

@RequestParam(value = "ticketId", required = false) String ticketId
HybrisHelp
  • 5,518
  • 2
  • 27
  • 65
0

I hope it is helpful to you, for me it is working fine. If you want to use spring forms, make sure to follow the below steps :

Step:1.In the spring controller, you should return the bean object like below to respected JSP.

Class User {
       private String ticketId;
       // setter & getter
     }
      @RequestMapping(value = "/test", method = RequestMethod.GET)
      public String init(Model model) {
            model.addAttribute("msg", "Please Enter Your Login Details");
                model.addAttribute("loginBean", new User()); 
            return "login";
        }    

Step:2 add a model attribute and add taglib in the JSP page.

        <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
        <form:form  action="${searchUrl}" method="post" 
         modelAttribute="loginBean">

                <div class="form-group">
                    <label class="control-label"> search by ID </label> 
                    <br>
                         <form:input type="text" id="ticketId" path = "ticketId"  
                          placeholder=" Enter ticket ID " />          
                </div>

            </form:form>

Step : 3

    @RequestMapping(value = "/test", method = RequestMethod.Post)
        public String init(Model model,
                @ModelAttribute("user") User user,BindingResult 
          result) {
               sout("user"+user);
                return "home";
            }   
Ranjith Bokkala
  • 379
  • 1
  • 10