javascript 带有 Onclick 方法的 JSP 和 JSTL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11383020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 13:00:07  来源:igfitidea点击:

JSP and JSTL with the Onclick method

javascriptspringjspjstl

提问by Alex Dowining

I have 2 links. In a session i store some data. I want if a user click in a specific link to look into the session and if the appropriate value contained, to continue. Else an AJAX pop up block will appears.

我有2个链接。在会话中,我存储了一些数据。我希望用户单击特定链接以查看会话,如果包含适当的值,则继续。否则会出现一个 AJAX 弹出块。

JAVASCRRIPT pop up block

JAVASCRIPT 弹出块

 function displaymessage(title,message,close)
 {

        resultReturn= $.confirm({
        'title'     : title, 
        'message'   : message
        'buttons'   : {
        close/*'Yes'*/  : {
                'class' : 'blue',
                                    'caption': close,
                'action': function(){}
                        }           
                        }
                          });
   }

JSP with JSTL tags:

带有 JSTL 标签的 JSP:

    <div class="header">
        <a href="<c:url value='/home'/>">
        <a href="<c:url value='/further'/>">
    </div>

     <c:if test="${not fn:containsIgnoreCase(data, 'mykey')}"> 
            <a id="get" class="right keepScrollPosition" 
               onclick="return displaymessage('<spring:message code="access.denied.title"/>'
                   ,'<spring:message code="access.denied.message"/>','<spring:message code="access.denied.close"/>');" 
               href="#"  /></a> 
        </c:if>

For example, if in the session contained the value 'mykey',then can go into the link home, else a pop up will appears. In case, in the session contained the value 'mypassword' and select the further link can continue, but if select the home link a pop up block will appears. I'm stuck and i need some help on how can i do this.Please help me

例如,如果会话中包含值“mykey”,则可以进入链接主页,否则会出现一个弹出窗口。如果会话中包含值 'mypassword' 并选择进一步的链接可以继续,但如果选择主页链接,则会出现一个弹出块。 我被卡住了,我需要一些关于如何做到这一点的帮助。请帮助我

UPDATE

更新

I'm trying the following script but it doesn't work:

我正在尝试以下脚本,但它不起作用:

  <li><a href="<c:url value='/home/'/>"<spring:message code="home_page"/>
        onclick="if(<c:if test="${not fn:containsIgnoreCase(data, 'mykey')}">){
                    return displaymessage('<spring:message code="access.denied.title"/>' 
               ,'<spring:message code="access.denied.message"/>','<spring:message code="access.denied.close"/>')" href="#" ;
       }
       </a>
   </li>

回答by atrain

You can't use client-side JS to check a server-side value held in the session. Instead, you need to use JS to make a GET call to a request mapping which runs servers-side code to check the session. Flow could be as follows:

您不能使用客户端 JS 来检查会话中保存的服务器端值。相反,您需要使用 JS 对运行服务器端代码以检查会话的请求映射进行 GET 调用。流程可能如下:

  1. User clicks button/image/something on page
  2. JS fires from click, makes AJAX call (e.g. $.get) to request mapping /check_session.do
  3. Request mapping checks session for value, returns true or false as JSON
  4. Client-side JS processes returned JSON, blocks or proceeds based on returned value
  1. 用户点击页面上的按钮/图像/某物
  2. JS 从点击触发,进行 AJAX 调用(例如$.get)以请求映射/check_session.do
  3. 请求映射检查会话的值,以 JSON 形式返回真或假
  4. 客户端JS处理返回的JSON,根据返回值阻塞或继续

Alternatively, use JSTL to access the session and write the session field's value to a hidden form field on the page, which the client-side JS then reads. Example: http://www.steve-farmer.com/examples/el/ui/ses-attr-name.jsp

或者,使用 JSTL 访问会话并将会话字段的值写入页面上的隐藏表单字段,然后客户端 JS 读取该字段。示例:http: //www.steve-farmer.com/examples/el/ui/ses-attr-name.jsp