gpt4 book ai didi

jsp - 当属性无效时,我可以在自定义 JSP 标记中使用 JSTL 抛出异常吗?

转载 作者:行者123 更新时间:2023-12-04 19:25:28 25 4
gpt4 key购买 nike

我正在制作一些 JSP 标记。我将类型参数传递给标记。

代码如下:

<%@ attribute name="type" require="true" %>
<c:choose>
<c:when test="${type eq 'even'}">
<c:set var="remainder" value="0" />
</c:when>
<c:when test="${type eq 'odd'}">
<c:set var="remainder" value="1" />
</c:when>
<c:otherwise>
<%-- Want to throw exception!! --%>
</c:otherwise>
</c:choose>

如果我传递了一个错误的值,我想抛出一个异常。当我搜索这个主题时,我刚找到 this .我不能在 JSTL 中抛出一个正常的异常吗?

最佳答案

我决定复活编辑a deleted answer看起来部分正确。

You can throw an exception by adding it in a scriptlet like so:

<c:choose>
<c:when test="${type eq 'even'}">
<c:set var="remainder" value="0" />
</c:when>
<c:when test="${type eq 'odd'}">
<c:set var="remainder" value="1" />
</c:when>
<c:otherwise>
<% if (true) throw new Exception("Wrong type"); %>
</c:otherwise>
</c:choose>

However, your jsps are supposed to be part of your "view" layer so you should really try to keep your logic (including throwing the exception) in your servlets.

注意 if (true) 语句。这是一个技巧,因为如果您只是将异常 scriptlet 放入您的代码中,编译器会提示“无法访问的代码”。

关于jsp - 当属性无效时,我可以在自定义 JSP 标记中使用 JSTL 抛出异常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8845884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com