@Override public String pathFor(ControllerMethod method) { logger.debug("Resolving path for {}", method); String format = resolver.getAcceptFormat(); String suffix = ""; if (format != null && !format.equals("html")) { suffix = "." + format; } String name = method.getController().getType().getSimpleName(); String folderName = extractControllerFromName(name); String path = getPrefix() + folderName + "/" + method.getMethod().getName() + suffix + "." + getExtension(); logger.debug("Returning path {} for {}", path, method); return path; }
protected String extractControllerFromName(String baseName) { baseName = lowerFirstCharacter(baseName); if (baseName.endsWith("Controller")) { return baseName.substring(0, baseName.lastIndexOf("Controller")); } return baseName; }
@Test public void shouldUseResourceTypeAndMethodNameToResolveJsp(){ when(formatResolver.getAcceptFormat()).thenReturn(null); String result = resolver.pathFor(method); assertThat(result, is("/WEB-INF/jsp/dog/bark.jsp")); }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Before public void config() throws Exception { MockitoAnnotations.initMocks(this); resolver = new DefaultPathResolver(formatResolver); when(method.getController()).thenReturn(controller); when(method.getMethod() ).thenReturn(DogController.class.getDeclaredMethod("bark")); when(controller.getType() ).thenReturn((Class) DogController.class); }
@Test public void shouldUseTheFormatIfSupplied() throws NoSuchMethodException { when(formatResolver.getAcceptFormat()).thenReturn("json"); String result = resolver.pathFor(method); assertThat(result, is("/WEB-INF/jsp/dog/bark.json.jsp")); }
@Test public void shouldIgnoreHtmlFormat() throws NoSuchMethodException { when(formatResolver.getAcceptFormat()).thenReturn("html"); String result = resolver.pathFor(method); assertThat(result, is("/WEB-INF/jsp/dog/bark.jsp")); }