我正在从事Spring MVC控制器项目。我有一个JSP页面,其中包含某些表格,人们会在其中键入某些条目,然后按Submit按钮。
现在,下面是我的代码库-在浏览器中点击该网址后,
http://localhost:8080/testweb/testOperation
如果我放置一个断点,它将自动转到下面的方法,然后在浏览器上显示我的
testOperation
jsp页面,它可以正常工作。@RequestMapping(value = "testOperation", method = RequestMethod.GET)
public Map<String, String> testOperation() {
final Map<String, String> model = new LinkedHashMap<String, String>();
return model;
}
现在我想做的是-假设在呼叫到达后立即在下面的方法中,我将从呼叫即将到来的标头中提取IP地址,如果IP地址不匹配,那么我想显示错误的JSP页面,但是如果IP地址匹配,那么我将显示
testOperation
jsp页面。@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;
}
这有可能吗?
最佳答案
如另一个答案中所述,您绝对可以通过返回一个不同的ModelAndView
来完成您要问的事情(甚至只是型号名称也可以),但是如果打算使用IP查找策略,我建议您看一下在Spring MVC的HandlerInterceptor
中。
查看this博客文章以了解更多详细信息
编辑
实际上,我正在改变自己的想法...现在,建议您使用HandlerMethodArgumentResolver将一个布尔值注入到控制器方法中,该布尔值显示用户是否在背后。相关代码为:
@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;
}
另外,取决于您如何设置Spring,您将必须注册
IsBehindSomethingHandlerMethodArgumentResolver
(如果您需要有关此的提示,请告诉我)关于java - 如何在Spring MVC Controller中根据特定条件显示不同的JSP页面?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23005192/