Redirecting Request To Another Action According To The Request Client Detected
Posted By : Shakil Pathan | 30-Sep-2015
Hi,
In this blog I am going to explain you about redirecting request according to the device detected(destop/mobile) to another action or controller in your grails or java application.
Now a days responsive web design is used for handling the styles of the website, But if we want to redirect or want to render the view according to the device detected from the request, then here the solution.
def mobileDetectService
def redirectAccordingToRequest() {
boolean isMobile = mobileDetectService.isMobile(request)
if(isMobile)
redirect "www.example.com"
else
redirect "m.example.com"
}
Above is the controller code which is use for the redirecting the request or used for render the correct view according to the device detected.
Below is the code from the mobileDetectService for detecting the correct device.
boolean isMobile( request ) {
def device = request.getAttribute('currentDevice')
boolean detected = device.isMobile()
if (detected) {
def userAgent = request.getHeader('User-Agent')
if (userAgent?.contains('iPad')) {
// skip iPads
detected = false
} else if (userAgent?.contains('Android') && !userAgent?.contains('Mobile')) {
// and android tables
detected = false
}
}
return detected
}
In the above code we are detecting the device according to the request parameters.
Hope it helps!
THANKS
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shakil Pathan
Shakil is an experienced Groovy and Grails developer . He has also worked extensively on developing STB applications using NetGem .