如何从指定Grails URL映射中记录并追踪$controller、$action、$id?
Absolutely! You can absolutely log your Grails URL mapping configurations and track the $controller, $action, and $id parameters for every incoming request. Here are practical, step-by-step solutions tailored to Grails:
The cleanest way to track these parameters for every request is using a Grails Interceptor (recommended for Grails 3+). Interceptors run before/after request processing and have access to all request parameters bound by the URL mappings.
Create an interceptor class (e.g., grails-app/controllers/com/example/UrlTrackingInterceptor.groovy):
class UrlTrackingInterceptor { // Set highest precedence to ensure this runs before other interceptors int order = HIGHEST_PRECEDENCE UrlTrackingInterceptor() { // Match all incoming requests matchAll() } boolean before() { // Extract values from the request params (populated by URL mappings) String requestUri = request.requestURI String controller = params.controller ?: 'root' // Handle root path case String action = params.action ?: 'index' // Default action if not specified String id = params.id ?: 'N/A' // Handle missing ID parameter // Log the details (customize this to write to files/database if needed) log.info "URL Request Tracked - URI: $requestUri | Controller: $controller | Action: $action | ID: $id" // Return true to proceed with request processing return true } }
This interceptor will log every request, including:
- Requests matching the
/$controller/$action?/$id?pattern (with all parameter combinations) - The root path
/(marked as controller: 'root', action: 'index') - Error pages like
500(though for error pages, params may be empty depending on how the error is triggered)
If you want to log the full set of URL mappings defined in your config (not just per-request), you can access Grails' internal mapping registry during application bootstrapping.
Add this to your grails-app/init/com/example/Bootstrap.groovy:
class Bootstrap { def grailsApplication def init = { servletContext -> // Fetch all registered URL mappings def urlMappings = grailsApplication.mappingHandlerMapping.urlMappings log.info "=== Registered Grails URL Mappings ===" urlMappings.each { mapping -> String pattern = mapping.pattern String controller = mapping.controllerName ?: 'N/A' String action = mapping.actionName ?: 'N/A' log.info "Pattern: $pattern | Controller: $controller | Action: $action" } } def destroy = { // Cleanup logic if needed } }
When your app starts, this will log all mappings from your UrlMappings.groovy, including the default /$controller/$action?/$id?, root /, and 500 error mapping.
If you're still on Grails 2.x, use Filters instead of Interceptors. Create a filter class (e.g., grails-app/conf/UrlTrackingFilters.groovy):
class UrlTrackingFilters { def filters = { // Match all requests all(uri: '/**') { before = { String requestUri = request.requestURI String controller = params.controller ?: 'root' String action = params.action ?: 'index' String id = params.id ?: 'N/A' log.info "URL Tracked - URI: $requestUri | Controller: $controller | Action: $action | ID: $id" } } } }
This works exactly like the interceptor but is compatible with older Grails versions.
内容的提问来源于stack exchange,提问作者Iverson

