2011-12-13 70 views
0

我有一個表單,我在控制器上使用handleRequest填充,返回一個帶有正確形式和模型的新ModelAndView。該表單正確填充。處理表單填充和提交的Spring MVC

在同一個控制器上,我有一個onSubmit方法,它應該處理我創建的表單的提交。這從來沒有被稱爲。當我提交表單時,頁面重新加載,就好像我已經調用了handleRequest一樣,沒有任何反應。

bean定義:

<bean name="/update-user.htm" class="com.leadx.web.usermaintenance.controllers.UpdateUserController" > 
    <property name="sessionForm" value="true" /> 
    <property name="commandName" value="updateUserSelectCommand" /> 
    <property name="commandClass" value="com.leadx.web.usermaintenance.commands.UpdateUserSelectCommand" /> 
    <property name="formView" value="update_user" /> 
</bean> 

控制器:

public class UpdateUserController extends SimpleFormController implements Controller { 

protected final Log logger = LogFactory.getLog(getClass()); 

@Autowired 
private UserService userService; 

@Override 
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, 
     IOException { 

    final List<User> users = this.userService.retrieveAllUsers(); 

    final Map<String, Object> userMaintenanceModel = new HashMap<String, Object>(); 
    userMaintenanceModel.put("users", users); 

    this.logger.info("Count of users: " + users.size()); 
    this.logger.info("Returning Update User view"); 

    return new ModelAndView("update_user", "userMaintenanceModel", userMaintenanceModel); 
} 

@Override 
public ModelAndView onSubmit(final Object command) throws ServletException { 

    final int userId = ((UpdateUserSelectCommand) command).getId(); 

    this.logger.info("Preparing to update user: " + userId); 

    final User user = this.userService.retrieveUserById(userId); 

    final Map<String, Object> userMaintenanceModel = new HashMap<String, Object>(); 
    userMaintenanceModel.put("user", user); 

    return new ModelAndView("update_user_details", "userMaintenanceModel", userMaintenanceModel); 
} 

}

,而JSP:

<%@ include file="/WEB-INF/jsp/include.jsp"%> 

<html> 
<head> 
<title>User Maintenance :: Update User</title> 
</head> 
<body> 
    <h1>Select a User to Update</h1> 
    <form:form method="post" commandName="updateUserSelectCommand"> 
     <table class="form"> 
      <select> 
       <c:forEach var="user" items="${userMaintenanceModel.users}"> 
        <option value="${user.getId()}">${user.getUsername()}</option> 
       </c:forEach> 
      </select> 
     </table> 
     <br> 
     <input type="submit" align="center" value="Submit"> 
    </form:form> 
</body> 
</html> 

我完全沉迷於此,所以任何幫助將非常感激,謝謝。

回答

1

當使用SimpleFormController時,您應該重寫referenceData()方法以使用初始數據填充表單。您不應該覆蓋handleRequest(),因爲它可以同時處理GET和POST請求:

@Override 
public Map referenceData(HttpServletRequest request) { 
    final List<User> users = this.userService.retrieveAllUsers(); 
    final Map<String, Object> userMaintenanceModel = new HashMap<String, Object>(); 
    userMaintenanceModel.put("users", users); 
    return userMaintenanceModel; 
} 
+0

axtavt,工作完美。謝謝。 – BombTodley

+0

有沒有辦法做到這一點與基於註解的控制器,或者你只是在initBinder中處理? – Scott

+0

@Scott:你可以使用'@ ModelAttribute'註釋的方法來創建預先填充的屬性。 – axtavt