【问题标题】:How to show different JSP page depending on certain conditions in Spring MVC Controller?如何根据 Spring MVC Controller 中的某些条件显示不同的 JSP 页面?
【发布时间】:2014-04-11 06:36:51
【问题描述】:

我正在开发 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;
}

这有可能以某种方式做到吗?

【问题讨论】:

标签: java spring jsp spring-mvc annotations


【解决方案1】:

正如另一个答案中所述,您绝对可以通过返回不同的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(如果您需要这方面的提示,请告诉我)

【讨论】:

  • 感谢您的建议。您能否提供一个示例,我将如何根据您提出的使用Interceptor 的建议解决我的问题?其实我对此有点陌生,所以如果你能提供一个例子,那将会有很大的帮助。
  • 我会在几天后给你一些代码,因为现在我无法访问我的工作示例
  • 感谢您的建议。在testOperation 方法中,你有if(behindSomething) 这是什么behindSomething
  • 是IsBehindSomethingHandlerMethodArgumentResolver.resolveArgument返回的值
  • Aahh.. 刚刚注意到.. 你能告诉我 HandlerInterceptor 与你刚刚给出的建议有什么区别吗?
【解决方案2】:

只返回相应的页面:

if(ipAddress.equals("something_here")) {
        return new ModelAndView("testOperation.jsp");
    } else {
        return new ModelAndView("error.jsp");
    }

【讨论】:

    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-17
    • 2019-02-04
    • 2015-05-12
    相关资源
    最近更新 更多