Thank you very much. That worked for me. One last question do i have to always have this default method to handle GETS in my controller?
For everyone, in case you have struggled like me to make this controller work.
Case
How do we go about using a MultiActionController to provide a simple CRUD JSP.
JSP PAGE
Code:
<!-- Form & Javascript -->
<script type="text/javascript">
function dispatch(_param) {
document.eirform.param.value = _param;
}
</script>
<form:form name="eirform" method="POST">
<input type="hidden" name="param" />
<!-- HTML Snippet -->
<TABLE border=0 cellpadding=2 cellspacing=1 align="center">
<TR>
<TD align="center" noWrap>
<input class=button type="submit" name="addAccount" value="Add New Account" onclick="dispatch('addAccount')"/>
</TD>
<TD align="center" noWrap>
<input class=button type="submit" name="updateAccount" value="Update Existing Account" onclick="dispatch('updateAccount')"/>
</TD>
<TD align="center" noWrap>
<input class=button type="submit" name="disableAccount" value="Disable Account" onclick="dispatch('disableAccount')"/>
</TD>
<TD align="center" noWrap>
<input class=button type="submit" name="clear" value="Clear">
</TD>
<TD align="center" noWrap>
<input class=button type="submit" value="Exit">
</TD>
</TR>
</TABLE>
Spring Controller Config File
<bean name="/actadmin.htm" class="eirapp.controller.AccountController">
<property name="accountManager" ref="accountManager" />
<property name="methodNameResolver">
<ref bean="paramResolver" />
</property>
</bean>
<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiac tion.ParameterMethodNameResolver">
<property name="defaultMethodName" value="showForm" />
<property name="methodParamNames">
<list>
<value>addAccount</value>
<value>updateAccount</value>
<value>disableAccount</value>
</list>
</property>
</bean>
Note: The line in RED was required to make this work.
I think this is where the problem is. The controller is getting a "GET" request without any parameters & is unable to resolve a method from the method resolver to execute the action. What you could do is in your bean definition add a default method as below & let it handle all the "GET" requests similar to the showForm in SimpleFormController.
Controller Code
Code:
package eirapp.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import eirapp.service.AccountManager;
public class AccountController extends MultiActionController {
protected final Log logger = LogFactory.getLog(getClass());
private AccountManager accountManager;
public ModelAndView showForm(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
String now = (new java.util.Date()).toString();
Map<String, Object> myModel = new HashMap<String, Object>();
myModel.put("now", now);
myModel.put("accounts", this.accountManager.getAccounts());
return new ModelAndView("actadmin", "model", myModel);
}
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
logger.info("Inside handleRequestInternal");
ModelAndView modelAndView = super.handleRequestInternal(request,
response);
String now = (new java.util.Date()).toString();
modelAndView.getModel().put("now", now);
modelAndView.getModel().put("accounts", accountManager.getAccounts());
return modelAndView;
}
public ModelAndView addAccount(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
logger.info("******Inside AccountController addAccount Method *******");
return new ModelAndView("actadmin");
}
public ModelAndView updateAccount(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
logger
.info("******Inside AccountController updateAccount Method *******");
return new ModelAndView("actadmin");
}
public ModelAndView disableAccount(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
logger
.info("******Inside AccountController disableAccount Method *******");
return new ModelAndView("actadmin");
}
public void setAccountManager(AccountManager _accountManager) {
this.accountManager = _accountManager;
}
public AccountManager getAccountManager() {
return accountManager;
}
}
Many thanks again.