public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response) throws Exception { // 这里需要声明request的实际类型,否则会报错 request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); HandlerExecutionChain chain = handlerMapping.getHandler(request); ModelAndView model = null; try { model = handlerAdapter.handle(request, response, chain.getHandler()); } catch (Exception e) { e.printStackTrace(); } return model; }
/** * This method finds the handler for a given request URI, or throws an exception if none were * found. * * <p>It will also ensure that the URI Parameters i.e. /context/test/{name} are added to the * request * * @param request the request for which to find a handler * @return The handler that agreed to handle the specified request. * @throws NoSuchMethodException if no acceptable handlers could be found */ protected Object getHandler(final MockHttpServletRequest request) throws NoSuchMethodException { HandlerExecutionChain chain = null; // NOPMD by jon.adams on 5/14/12 final Map<String, HandlerMapping> map = applicationContext.getBeansOfType(HandlerMapping.class); final Iterator<HandlerMapping> itt = map.values().iterator(); while (itt.hasNext()) { final HandlerMapping mapping = itt.next(); try { chain = mapping.getHandler(request); } catch (final HttpRequestMethodNotSupportedException exc) { // ignore and try next LOGGER.info( mapping.getClass().getName() + " handler determined it will not handle the request. Message: " + exc.getMessage(), exc); } catch (final Exception exc) { throw new RuntimeException(exc); // NOPMD } if (chain == null) { // ignore and try next LOGGER.debug( mapping.getClass().getName() + " handler determined it will not handle the request."); } else { // found one. quit looking for more. break; } } if (chain == null) { throw new NoSuchMethodException( "Unable to find handler for request URI: " + request.getRequestURI()); } return chain.getHandler(); }
@Override protected Object getHandlerInternal(HttpServletRequest request) throws Exception { Object temp = request.getAttribute(DaspConstants.REQ_ATTR_REQUEST_PARSER); if (!(temp instanceof IRequestParser)) { LOGGER.warn("No instance of [" + IRequestParser.class + "] found!"); return null; } IRequestParser rp = (IRequestParser) temp; String moduleName = rp.getRequestModule(); if (StringUtils.isBlank(moduleName)) { moduleName = "home"; } IOsgiBootstrap osgiBootstrap = DaspGlobal.getOsgiBootstrap(); if (osgiBootstrap == null) { String msg = "Instance of [" + IOsgiBootstrap.class + " not found]!"; LOGGER.warn(msg); return null; } Map<String, String> filter = new HashMap<String, String>(); filter.put("Module", moduleName); HandlerMapping handlerMapping = osgiBootstrap.getService(SERVICE_CLASS, filter); if (handlerMapping == null) { String msg = "No handler mapping found for module [" + moduleName + "]!"; LOGGER.warn(msg); return null; } Object result = handlerMapping.getHandler(request); if (LOGGER.isDebugEnabled()) { String msg = "Found [" + result + "] for module [" + moduleName + "]."; LOGGER.debug(msg); } return result; }