Just make your own. I have a BasicViewController that does exactly what you need. If you have a view of /secure/product/addProduct.html if will load a JSP from /secure/product/addProduct.jsp
Code:
/*
* MPSC-Spring - A library of common code to use with the Spring framework
* Copyright (C) 2005 Matt Parker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package uk.co.mpcontracting.modules.spring.controller;
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.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
/**
* A simple view controller implementation that calculates the concrete view from the request
*
* @author Matt Parker (matt@mpcontracting.co.uk)
*/
public class BasicViewController extends AbstractViewController
{
private static Log log = LogFactory.getLog(BasicViewController.class);
/**
* Processes the view request
*
* @param request The request
* @param response The response
* @return The model and view
* @throws Exception If there are any problems with the view request
*/
protected ModelAndView process(HttpServletRequest request, HttpServletResponse response,
Errors errors) throws Exception
{
return (getViewFromRequest(request));
}
}
Code:
/*
* MPSC-Spring - A library of common code to use with the Spring framework
* Copyright (C) 2005 Matt Parker
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package uk.co.mpcontracting.modules.spring.controller;
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.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import uk.co.mpcontracting.modules.spring.model.ControlModel;
import uk.co.mpcontracting.modules.spring.support.ErrorUtils;
/**
* An abstract Spring controller that deals with resolving views
*
* @author Matt Parker (matt@mpcontracting.co.uk)
*/
public abstract class AbstractViewController implements Controller
{
private static Log log = LogFactory.getLog(AbstractViewController.class);
private static final String DEFAULT_MESSAGE = "error.standard.defaultMessage";
/**
* A hook for a sub-class to process the view request
*
* @param request The request
* @param response The response
* @return The model and view
* @throws Exception If there are any problems with the view request
*/
protected abstract ModelAndView process(HttpServletRequest request, HttpServletResponse response,
Errors errors) throws Exception;
private AbstractFormController associatedFormController;
private String successView;
private String failureView;
/**
* Sets the associated form controller. This allows a view to be associated with a form which in
* turn allows a form backing object to be created for the view to enable the Spring bind tags
* to function correctly
*
* @param associatedFormController The associated form controller
*/
public void setAssociatedFormController(AbstractFormController associatedFormController)
{
this.associatedFormController = associatedFormController;
}
/**
* Sets the success view
*
* @param successView The success view
*/
public final void setSuccessView(String successView)
{
this.successView = successView;
}
/**
* Retrieves the success view
*
* @return The success view
*/
protected final String getSuccessView()
{
return (successView);
}
/**
* Sets the failure view
*
* @param failureView The failure view
*/
public final void setFailureView(String failureView)
{
this.failureView = failureView;
}
/**
* Retrieves the failure view
*
* @return The failure view
*/
protected final String getFailureView()
{
return (failureView);
}
/**
* Creates a form backing object if required then passes control to the process method
*
* @param request The request
* @param response The response
* @return The returned model and view
* @throws Exception If there are any problems with the view request
*/
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception
{
// Create a form backing object if we have an associated form
if (associatedFormController != null)
{
associatedFormController.createFormBackingObject(request);
}
BindException errors = new BindException("*", "*");
ModelAndView modelAndView = process(request, response, errors);
if (errors.hasErrors())
{
modelAndView.addAllObjects(errors.getModel());
}
return (modelAndView);
}
/**
* Creates a model and view from the success view and request
*
* @param request The request
* @return The created model and view
*/
protected ModelAndView getSuccessView(HttpServletRequest request)
{
return (getViewFromURI(request, successView));
}
/**
* Creates a model and view from the failure view and request
*
* @param request The request
* @return The created model and view
*/
protected ModelAndView getFailureView(HttpServletRequest request)
{
return (getViewFromURI(request, failureView));
}
/**
* Creates a model and view from the request and a given control model
*
* @param request The request
* @param controlModel The control model
* @return The created model and view
* @throws Exception If the control model is unable to be stored in the view
*/
protected ModelAndView getViewFromRequestAndModel(HttpServletRequest request, ControlModel controlModel)
throws Exception
{
controlModel.storeControlModel(request);
ModelAndView modelAndView = getViewFromRequest(request);
if (controlModel != null)
{
Map internalMap = controlModel.getControlModelMap();
if (internalMap != null)
{
modelAndView.addAllObjects(controlModel.getControlModelMap());
}
}
return (modelAndView);
}
/**
* Creates a model and view from the request
*
* @param request The request
* @return The created model and view
*/
protected ModelAndView getViewFromRequest(HttpServletRequest request)
{
return (getViewFromURI(request, request.getRequestURI()));
}
/**
* Converts a given URI into a model and view given a request context
*
* @param request The request
* @param uri The given URI
* @return The model and view
*/
protected ModelAndView getViewFromURI(HttpServletRequest request, String uri)
{
String contextPath = request.getContextPath() + "/";
int startIndex = uri.lastIndexOf(contextPath);
if (startIndex == -1)
{
startIndex = 0;
}
else
{
startIndex += contextPath.length();
}
int endIndex;
if (uri.indexOf(";") != -1)
{
endIndex = uri.indexOf(";");
}
else if (uri.indexOf("?") != -1)
{
endIndex = uri.indexOf("?");
}
else
{
endIndex = uri.length();
}
String fileName = uri.substring(startIndex, endIndex);
if (fileName.indexOf(".") != -1)
{
fileName = fileName.substring(0, fileName.lastIndexOf("."));
}
if (log.isDebugEnabled())
{
log.debug("Request URI - " + uri + " - translated to - " + fileName);
}
return (new ModelAndView(fileName));
}
/**
* Convenience method to add an error into the current errors collection
*
* @param errors The current errors collection
* @param errorMessage The message or key to add to the errors collection
* @param arguments Any arguments that need to be resolved and added into the error message
*/
protected void addError(Errors errors, String errorMessage, Object[] arguments)
{
if (errorMessage != null)
{
ErrorUtils.addCustomError(errors, errorMessage, arguments);
}
else
{
ErrorUtils.addCustomError(errors, DEFAULT_MESSAGE, arguments);
}
}
/**
* Convenience method to add an error into the current errors collection
*
* @param errors The current errors collection
* @param errorMessage The message or key to add to the errors collection
* @param resolvableArguments Any resolvable arguments that need to be added into the error message
*/
protected void addErrorWithResolvableArguments(Errors errors, String errorMessage,
String[] resolvableArguments)
{
if (errorMessage != null)
{
ErrorUtils.addCustomErrorWithResolvableArguments(errors, errorMessage, resolvableArguments);
}
else
{
ErrorUtils.addCustomErrorWithResolvableArguments(errors, DEFAULT_MESSAGE, resolvableArguments);
}
}
}
There's a lot of extra stuff in the AbstractViewController that you probably don't want, but I'm sure you can work out what you need.