2011-09-23 5 views
14

Spring MVC kullanıyorum. Denetleyici yöntemimde aşağıdaki kod parçasının metin kutusu değerini nasıl alabilirim?Denetleyicide form değeri nasıl alınır

<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> 

cevap

19

Böyle @RequestParam kullanabilirsiniz:

@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; 
    }