2011-09-23 64 views
14

我正在使用Spring MVC。如何在我的控制器方法中獲得以下代碼片段的文本框值?如何在控制器中獲取表單值

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

回答

19

您可以使用@RequestParam這樣的:

@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; 
    } 
相關問題