@RequestMapping("/definitionView") public String test(){ System.out.println("definitionView"); return"hello:time"; }
3. 自定义视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassMyViewimplementsView{
@Override public String getContentType(){ return"text/html"; }
@Override publicvoidrender(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.getWriter().write("This is MyView, And Time is " + new Date().toString()); }
@Override protected View createView(String viewName, Locale locale)throws Exception { // If this resolver is not supposed to handle the given view, // return null to pass on to the next resolver in the chain. if (!canHandle(viewName, locale)) { returnnull; } // Check for special "redirect:" prefix. /********/ if (viewName.startsWith(REDIRECT_URL_PREFIX)) { String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length()); RedirectView view = new RedirectView(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible()); return applyLifecycleMethods(viewName, view); } // Check for special "forward:" prefix. if (viewName.startsWith(FORWARD_URL_PREFIX)) { String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length()); returnnew InternalResourceView(forwardUrl); } // Else fall back to superclass implementation: calling loadView. returnsuper.createView(viewName, locale); }
// Determine which request handle to expose to the RequestDispatcher. HttpServletRequest requestToExpose = getRequestToExpose(request);
// *** Expose the model object as request attributes. //将模型的数据放到request域中 exposeModelAsRequestAttributes(model, requestToExpose);
// Expose helpers as request attributes, if any. exposeHelpers(requestToExpose);
// Determine the path for the request dispatcher. String dispatcherPath = prepareForRendering(requestToExpose, response);
// Obtain a RequestDispatcher for the target resource (typically a JSP). RequestDispatcher rd = getRequestDispatcher(requestToExpose, dispatcherPath); if (rd == null) { thrownew ServletException("Could not get RequestDispatcher for [" + getUrl() + "]: Check that the corresponding file exists within your web application archive!"); }
// If already included or response already committed, perform include, else forward. if (useInclude(requestToExpose, response)) { response.setContentType(getContentType()); if (logger.isDebugEnabled()) { logger.debug("Including resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.include(requestToExpose, response); }
else { // Note: The forwarded resource is supposed to determine the content type itself. if (logger.isDebugEnabled()) { logger.debug("Forwarding to resource [" + getUrl() + "] in InternalResourceView '" + getBeanName() + "'"); } rd.forward(requestToExpose, response); } }