/** * Set the view name to return. Optional. * <p>If not specified, the view controller will return {@code null} as the * view name in which case the configured {@link RequestToViewNameTranslator} * will select the view name. The {@code DefaultRequestToViewNameTranslator} * for example translates "/foo/bar" to "foo/bar". * @see org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator */ public void setViewName(String viewName) { this.controller.setViewName(viewName); }
@Override public long getLastModified(HttpServletRequest request, Object handler) { if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); } return -1L; }
/** * Returns view name based on the URL filename, * with prefix/suffix applied when appropriate. * @see #extractViewNameFromUrlPath * @see #setPrefix * @see #setSuffix */ @Override protected String getViewNameForRequest(HttpServletRequest request) { String uri = extractOperableUrl(request); return getViewNameForUrlPath(uri); }
@Test public void servletForwardingController() throws Exception { ServletForwardingController sfc = new ServletForwardingController(); sfc.setServletName("action"); doTestServletForwardingController(sfc, false); }
/** * Returns a {@link SimpleControllerHandlerAdapter} for processing requests * with interface-based controllers. */ @Bean public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() { return new SimpleControllerHandlerAdapter(); }
/** * Build the full view name based on the given view name * as indicated by the URL path. * <p>The default implementation simply applies prefix and suffix. * This can be overridden, for example, to manipulate upper case * / lower case, etc. * @param viewName the original view name, as indicated by the URL path * @return the full view name to use * @see #getPrefix() * @see #getSuffix() */ protected String postProcessViewName(String viewName) { return getPrefix() + viewName + getSuffix(); }
@Test public void servletForwardingControllerWithBeanName() throws Exception { ServletForwardingController sfc = new ServletForwardingController(); sfc.setBeanName("action"); doTestServletForwardingController(sfc, false); }
@Test public void settingPrefixToNullCausesEmptyStringToBeUsed() throws Exception { UrlFilenameViewController ctrl = new UrlFilenameViewController(); ctrl.setPrefix(null); assertNotNull("For setPrefix(..) with null, the empty string must be used instead.", ctrl.getPrefix()); assertEquals("For setPrefix(..) with null, the empty string must be used instead.", "", ctrl.getPrefix()); }
/** * Returns a {@link HttpRequestHandlerAdapter} for processing requests * with {@link HttpRequestHandler HttpRequestHandlers}. */ @Bean public HttpRequestHandlerAdapter httpRequestHandlerAdapter() { return new HttpRequestHandlerAdapter(); }
@Override @Nullable public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return ((Controller) handler).handleRequest(request, response); }
/** * Set the status code to set on the response. Optional. * <p>If not set the response status will be 200 (OK). */ public ViewControllerRegistration setStatusCode(HttpStatus statusCode) { this.controller.setStatusCode(statusCode); return this; }
private String formatStatusAndView() { StringBuilder sb = new StringBuilder(); if (this.statusCode != null) { sb.append("status=").append(this.statusCode); } if (this.view != null) { sb.append(sb.length() != 0 ? ", " : ""); String viewName = getViewName(); sb.append("view=").append(viewName != null ? "\"" + viewName + "\"" : this.view); } return sb.toString(); } }
public RedirectViewControllerRegistration(String urlPath, String redirectUrl) { Assert.notNull(urlPath, "'urlPath' is required."); Assert.notNull(redirectUrl, "'redirectUrl' is required."); this.urlPath = urlPath; this.redirectView = new RedirectView(redirectUrl); this.redirectView.setContextRelative(true); this.controller.setView(this.redirectView); }
@Test(expected = IllegalArgumentException.class) public void throwsExceptionWithNullPathMatcher() throws Exception { WebContentInterceptor interceptor = new WebContentInterceptor(); interceptor.setPathMatcher(null); }
/** * Map a simple controller to the given URL path (or pattern) in order to * set the response status to the given code without rendering a body. * @since 4.1 */ public void addStatusController(String urlPath, HttpStatus statusCode) { ViewControllerRegistration registration = new ViewControllerRegistration(urlPath); registration.setApplicationContext(this.applicationContext); registration.setStatusCode(statusCode); registration.getViewController().setStatusOnly(true); this.registrations.add(registration); }
protected void setApplicationContext(@Nullable ApplicationContext applicationContext) { this.controller.setApplicationContext(applicationContext); }
public ParameterizableViewController() { super(false); setSupportedMethods(HttpMethod.GET.name(), HttpMethod.HEAD.name()); }
@Override public String toString() { return "ParameterizableViewController [" + formatStatusAndView() + "]"; }
@Test public void servletForwardingControllerWithInclude() throws Exception { ServletForwardingController sfc = new ServletForwardingController(); sfc.setServletName("action"); doTestServletForwardingController(sfc, true); }
@Override public long getLastModified(HttpServletRequest request, Object handler) { if (handler instanceof LastModified) { return ((LastModified) handler).getLastModified(request); } return -1L; }