• 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)
    		redirect ""
    		redirect ""


    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!


Tags: grails