Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@RequestMapping annotation in Spring MVC

I have a controller with request mapping as @RequestMapping("/**") What does it mean?

If I want to exclude certain url pattern from the above mapping how would I do that?

Could someone please shed some light on it?

like image 817
Chaitanya MSV Avatar asked Jan 25 '12 05:01

Chaitanya MSV


1 Answers

I was able to achieve "url exclusion" or "not matching url" through the use of the Regex "negative lookahead" construct.

I want my handler to handel everything other than static resources, i.e. CSS/Images/JS, and error pages.

To prevent that handeling of error pages i.e. resourceNotFound you will need to

  1. Edit the web.xml /web-app/error-page to prefix the error url with /error/
  2. Edit the WEB-INF/spring/webmvc-config.xml /beans/mvc:view-controller/@path handel your new mappings
  3. Edit the WEB-INF/spring/webmvc-config.xml /beans/bean[@class=**SimpleMappingExceptionResolver] to map all exceptions to error/...

In your controller use the below

@Controller
@RequestMapping(value = { "/" })
public class CmsFrontendController {

  @RequestMapping(value = { "/" }, headers = "Accept=text/html")
  public String index(Model ui) {
      return addMenu(ui, "/");
  }

  @RequestMapping(value = { "{path:(?!resources|error).*$}", "{path:(?!resources|error).*$}/**" }, headers = "Accept=text/html")
  public String index(Model ui, @PathVariable(value = "path")String path) {
      try {
          path = (String) request.getAttribute(
                  HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
          return addMenu(ui, path);
      } catch (Exception e) {
          log.error("Failed to render the page. {}", StackTraceUtil.getStackTrace(e));
          return "error/general";
      }
  }
}
like image 136
concept Avatar answered Oct 09 '22 04:10

concept