如何在JSP页面中有条件地显示div的内容而不是另一个div? [英] How conditionally show the content of a div rather than another div in a JSP page?

查看:619
本文介绍了如何在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>

我必须根据状态<的值显示其中一个div / strong>变量推入 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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