2011-09-23 13 views
14

Sto utilizzando Spring MVC. Come posso ottenere il valore della casella di testo del seguente snippet nel mio metodo di controllo?Come ottenere un valore di modulo in un controller

<form name="forgotpassord" action="forgotpassword" method="POST" > 
    <ul> 
     <li><label>User:</label> <input type='text' name='j_username' /></li> 
     <li><label>&nbsp;</label> <input type="submit" value="OK" class="btn"></li> 
    </ul> 
</form> 

risposta

19

È possibile utilizzare @RequestParam come questo:

@RequestMapping(value="/forgotpassword", method=RequestMethod.POST) 
public String recoverPass(@RequestParam("j_username") String username) { 
    //do smthin 
} 
-1
1. Use Form tag library 
Just add 

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 

<form:form name="forgotpassord" action="forgotpassword" method="POST"> 
<ul> 
    <li><label>User:</label> <input type='text' name='j_username' /></li> 
    <li><label>&nbsp;</label> <input type="submit" value="OK" class="btn"></li> 
</ul> 
</form:form> 


2. Now in controller 

    @RequestMapping(value="/forgotpassword", method = RequestMethod.POST) 
    public ModelAndView forgotpassword(@ModelAttribute("FormJSP_Name") User user,BindingResult result) { 

     String user = user.getjUsername(); //use it further 
     ModelAndView model1 = new ModelAndView("NextJSP_Name"); 
     return model1; 
    } 
Problemi correlati