如何提前停止处理JSP? [英] How to stop processing a JSP early?

查看:112
本文介绍了如何提前停止处理JSP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSP页面,它调用一个函数并检查它的返回值。如果返回值不为null,则JSP页面继续使用它。如果返回值为空,我希望JSP页面的处理停止(这是因为该函数将在返回null对象之前执行重定向。此外,JSP代码的其余部分显然使用此对象,并且将获得如果函数返回null,则为空指针异常。)

I've got a JSP page, which calls a function and checks its return value. If the return value is not null, the JSP page goes on to use it. If the return value IS null, I want the processing of the JSP page to stop (this is because the function will perform a redirect before returning the null object. Also, the rest of the JSP code obviously uses this object, and will get a null pointer exception if the function returned null).

所以我的问题是,停止加载JSP页面的正确方法是什么?我可以这样做:

So my question is, what is the right way of stopping the load of a JSP page? Can I do something like this:

if (Func() == null) { return; }

或者在JSP中间使用返回不是更简洁的方法来解决这个问题?

Or is using a return in the middle of a JSP not the cleaner way to go about this?

谢谢

推荐答案

无论如何终止JSP的处理早期,我建议在处理显示之前处理servlet中的流控制,并根据函数调用的结果切换到适当的JSP。

Regardless of how to terminate processing of a JSP early, I would suggest handling flow control in a servlet prior to handling the display, and switch to an appropriate JSP depending on the outcome of the function call.

我倾向于使用JSP纯粹用于显示bean(或多个)的内容,并且所有逻辑都包含在调用servlet中。通过这样做,您可以更清晰地分离组件之间的关注点,以及更易于理解和调试的系统。

I tend to use JSPs purely for displaying the contents of a bean (or more than one), and all logic is contained within the invoking servlet. By doing this you get a much cleaner separation of concerns between your components, and a more easily comprehensible and debuggable system.

这篇关于如何提前停止处理JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆