如何在JSP页面中有条件地显示div的内容而不是另一个div?

编程入门 行业动态 更新时间:2024-10-27 20:36:18
本文介绍了如何在JSP页面中有条件地显示div的内容而不是另一个div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是JSP开发的新手,我有以下疑问。

I am very new in JSP development and I have the following doubt.

如果进入JSP页面我有2个 div 就像这样:

If into a JSP page I have 2 div like this:

<div id="succes"> <p>SUCCESS</p> </div> <div id="failure"> <p>FAILURE</p> </div>

我必须根据状态变量推入 Http Session ,只有2个值:确定和 KO 。

and I have to show only one of these div according to the value of a status variable putted into the Http Session that can have only 2 value: OK and KO.

我可以这样做:

<% if(request.getSession(false).getAttribute("status")=="OK"> { %> <div id="succes"> <p>SUCCESS</p> </div> <% } %> <% else { %> <div id="failure"> <p>FAILURE</p> </div> <% } %>

它有效吗?可以吗?或者有更好的方法来实现这个任务?(也许我可以使用JavaScript \JQuery做这样的事情吗?)

It works? Is it ok? or there are some better way to achieve this task? (maybe can I do something like this using JavaScript\JQuery?)

推荐答案

使用JSP,正确的方法是使用JSTL标签.Google用于jstl教程并阅读什么是d。您的示例使用scriptlet。虽然这样做也可以,但不是正确的方法。一个示例如下所示:

With JSPs the right way to do it is using JSTL tags. Google for 'jstl tutorial' and read up what is offered.Your example uses scriptlets instead. Though that would get it done too but is not the right way to do it. One example is shown below:

<c:choose> <c:when test= "${request.getSession().getAttribute("userName").equals("Guest")}"> <div> Welcome Guest</div> </c:when> <c:otherwise> <div> Welcome Real User</div> </c:otherwise> </c:choose>

更多推荐

如何在JSP页面中有条件地显示div的内容而不是另一个div?

本文发布于:2023-11-22 04:12:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615933.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中有   而不是   条件   页面   内容

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!