我正在研究 Spring MVC Controller 项目。我有一个 JSP 页面,其中包含某些表单,人们将在其中键入某些条目,然后按提交按钮。
现在下面是我的代码库 - 当我在浏览器上点击此网址时 -
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;
}
现在我想做的是 - 假设在下面的方法中,一旦调用到来,我将从调用到来的 header 中提取 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 将显示用户是否在某些内容后面的 boolean 值注入(inject)到您的 Controller 方法中。相关代码为:
@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/