Spring MVC's core components—HandlerMapping, HandlerAdapter, and Controller—each have distinct responsibilities.
Responsibilities
-
HandlerMapping (Mapper): Maps incoming HTTP requests to appropriate handlers based on the request URL. It associates request paths with controller beans.
-
HandlerAdapter (Adapter): Invokes the actual handler method found by the mapper. It supports different controller types (e.g., those implementing
Controller,HttpRequestHandler, or using@Controllerannotations) through dedicated adapters. -
Controller: Contains the core business logic for handling requests.
Selected Features
HandlerMapping Implementations
BeanNameUrlHandlerMapping
Routes reqeusts to controllers whose bean name matches the request path.
<bean name="/beanurl.action" class="com.example.mvc.BeanNameUrlHandlerMapping.HelloHAE" />
<bean name="/delete.action" class="com.example.mvc.BeanNameUrlHandlerMapping.HelloHAE" />
<bean name="/update.action" class="com.example.mvc.BeanNameUrlHandlerMapping.HelloHAE" />
<bean name="/find.action" class="com.example.mvc.BeanNameUrlHandlerMapping.HelloHAE" />
SimpleUrlHandlerMapping
Maps multiple URL patterns to a single controller bean, defined via a properties collection.
<!-- Register the controller bean first -->
<bean id="userAction" class="com.example.mvc.BeanNameUrlHandlerMapping.HelloHAE" />
<!-- Configure the mapper -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/beanurl.action">userAction</prop>
<prop key="/delete.action">userAction</prop>
<prop key="/update.action">userAction</prop>
<prop key="/find.action">userAction</prop>
</props>
</property>
</bean>
HandlerAdapter: SimpleControllerHandlerAdapter
Spring defines a separate adapter interface for each type of controller. This design allows easy extension: to support a new controller type, simply add a corresponding adapter class. For more details on the adapter pattern, see this article or this blog post (credit to the original authors).
Controllers
ParameterizableViewController
Used to forward from one view to another without any custom controller logic.
<bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/index.jsp" />
</bean>
AbstractCommandController
Collects request parameters directly into a command object (entity).
<bean name="/add.action" class="com.example.mvc.controller.AdminAction" />
public class AdminAction extends AbstractCommandController {
public AdminAction() {
this.setCommandClass(Admin.class);
}
@Override
protected ModelAndView handle(HttpServletRequest request,
HttpServletResponse response,
Object obj,
BindException bindException) throws Exception {
System.out.println("AdminAction::handle");
ModelAndView mav = new ModelAndView();
Admin admin = null;
if (obj instanceof Admin) {
admin = (Admin) obj;
}
mav.addObject("username", admin.getUsername());
mav.addObject("gender", admin.getGender());
mav.addObject("hiredate", admin.getHiredate());
mav.setViewName("/jsp/success.jsp");
return mav;
}
}