david david - 4 months ago 29
Java Question

How to show different JSP page depending on certain conditions in Spring MVC Controller?

I am working on Spring MVC controller project. I have a JSP page which contains certain forms and people will type certain entries in it and then press submit button.

Now below is my code base - As soon as I hit this url on the browser -

http://localhost:8080/testweb/testOperation


It automatically goes to below method if I put a breakpoint and then it shows me my
testOperation
jsp page on the browser and it works fine.

@RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testOperation() {
final Map<String, String> model = new LinkedHashMap<String, String>();
return model;
}


Now what I am trying to do is - suppose in the below method as soon as the call comes, I will extract IP Address from the headers where the call is coming, if the IP Address doesn't matches, then I would like to show error JSP page but if the IP Address matches then I would show
testOperation
jsp page.

@RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testOperation(final HttpServletRequest request) {
final Map<String, String> model = new LinkedHashMap<String, String>();

//is client behind something?
String ipAddress = request.getHeader("X-FORWARDED-FOR");
if (ipAddress == null) {
ipAddress = request.getRemoteAddr();
}

if(ipAddress.equals("something_here")) {
// then load testOperation jsp page
} else {
// otherwise load some error jsp page
}

return model;
}


Is this possible to do somehow?

Answer

As noted in another answer, you can definitely do what you are asking just by returning a different ModelAndView (or even just the model name will do), but if are planning to use the IP lookup strategy I would suggest you take a look at Spring MVC's HandlerInterceptor. Take a look at this blog post for more details

EDIT

Actually I am revising my thinking... I am now suggesting that you should use a HandlerMethodArgumentResolver to inject the a boolean that shows whether or not the user is behind something, into your controller method. The relevant code would be :

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface BehindSomething{
}

import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
@Component
public class IsBehindSomethingHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        return methodParameter.getParameterAnnotation(RemoteAddress.class) != null
                && methodParameter.getParameterType().equals(Boolean.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        final String ipAddress = nativeWebRequest.getHeader("X-FORWARDED-FOR");
        if (ipAddress == null) {
            ipAddress =(((ServletWebRequest) nativeWebRequest).getRequest()).getRemoteAddr();
        }

        return ipAddress.equals("something_here");
    }
}

@RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testOperation(@BehindSomething Boolean behindSomething) {
       final Map<String, String> model = new LinkedHashMap<String, String>();

       if(behindSomething) {
           // then load testOperation jsp page
       } else {
           // otherwise load some error jsp page
       }

       return model;
}

Also, dependending on how you have setup Spring, you will have to register IsBehindSomethingHandlerMethodArgumentResolver (if you need tips on that, let me know)