1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.web.servlet.mvc;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import org.springframework.web.servlet.ModelAndView;
  20. /**
  21. * Controller that transforms the virtual filename at the end of a URL
  22. * to a view name. Example: "/index.html" -> "index"
  23. * @author Alef Arendsen
  24. */
  25. public class UrlFilenameViewController implements Controller {
  26. public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
  27. String uri = request.getRequestURI();
  28. int begin = uri.lastIndexOf('/');
  29. if (begin == -1) {
  30. begin = 0;
  31. }
  32. else {
  33. begin++;
  34. }
  35. int end;
  36. if (uri.indexOf(";") != -1) {
  37. end = uri.indexOf(";");
  38. }
  39. else if (uri.indexOf("?") != -1) {
  40. end = uri.indexOf("?");
  41. }
  42. else {
  43. end = uri.length();
  44. }
  45. String fileName = uri.substring(begin, end);
  46. if (fileName.indexOf(".") != -1) {
  47. fileName = fileName.substring(0, fileName.lastIndexOf("."));
  48. }
  49. return new ModelAndView(fileName);
  50. }
  51. }