我正在研究Spring MVC控制器项目。我有一个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;
}
现在我要做的是 - 假设在下面的方法中,一旦呼叫到来,我将从呼叫来的标头中提取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;
}
这可能以某种方式吗?
答案 0 :(得分:0)
只需返回相应的页面:
if(ipAddress.equals("something_here")) {
return new ModelAndView("testOperation.jsp");
} else {
return new ModelAndView("error.jsp");
}
答案 1 :(得分:0)
正如另一个答案中所指出的那样,你可以通过返回不同的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
(如果你需要提示,请告诉我)