ListItems中的JSP onclick [英] JSP inside ListItems onclick

查看:92
本文介绍了ListItems中的JSP onclick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < div id =菜单中的

>
< ul>
< li class =menuClassonclick =<%session.setAttribute(currentPage,firstPage);%>>第一页< / a>< / li>
< li class =menuClassonclick =<%session.setAttribute(currentPage,secondPage);%>>< a href =/ index.jsp>第二页< / a>< / li>
< / ul>



然而,这会导致第二页一直显示,即使没有点击。这种方法甚至是合法的,还是有另一种方法来更改会话变量并刷新页面内的页面? 当解决方案JSP页面被调用,按以下顺序发生:


  • 服务器检查是否已经编译了.jsp,以及是否它没有改变,因为它是最后编译。
  • 服务器通过Jasper编译器运行jsp,它将jsp解释为Java代码,任何不是Java的(CSS,HTML,JavaScript,等等)被放置在一个字符串中。

  • Java代码被编译并执行。

  • 结果放入响应并发送给用户。


    因此,你的语句: session.setAttribute 在HTML会发送给用户,而且 currentPage 总是设置为 secondPage






    如何验证它?


    1. div 之前添加下面的行,并查看它打印的内容。

       <%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%> 
      < c:out value =$ {sessionScope.currentPage}/>


    2. 在浏览器中单击右键,查看视图源。在 onclick 属性中没有任何内容,因为 setAttribute 被制作为一个单独的命令并在页面发送给您之前执行。

       < li class =menuClassonclick => 







    <您可以尝试按如下所示将它作为查询字符串而不是会话属性传递。

     < a href =/ index.jsp?currentPage = firstPage>第一页< / a> 
    < a href =/ index.jsp?currentPage = secondPage>第二页< / a>

    注意:务必避免使用 Scriplet元素并使用 JSP标准标记库


    I am trying to set session parameter inside a list items onclick call such as:

    <div id="menu"> 
    <ul>
        <li class="menuClass" onclick="<% session.setAttribute( "currentPage", "firstPage"); %>">First page</a></li>
        <li class="menuClass" onclick="<% session.setAttribute( "currentPage", "secondPage"); %>"><a href="/index.jsp">Second page</a></li>
    </ul>
    

    However this results in secondPage being displayed all the time, even without a click. Is this approach even legitimate or is there another way to change session variable and refresh the page within a list item?

    解决方案

    When a JSP page is called, the following happens, in this order:

    • Server checks to see if the .jsp has already been compiled and whether or not it has changed since it was last compiled.
    • Server runs the jsp through the Jasper compiler, which interprets the jsp into Java code, anything that is not Java (CSS, HTML, JavaScript, etc) is placed in a String.
    • The Java code is compiled and executed.
    • The results are placed in the response and sent to the user.

    So, your statement: session.setAttribute is executed before the the HTML is sent to the user, and does exactly and currentPage is always set to secondPage already.


    How to verify it?

    1. add below line before div and look what it prints.

      <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
      <c:out value="${sessionScope.currentPage }" />
      

    2. Right click in the browser and look at the view source. There is nothing inside onclick attribute because the setAttribute was made into a separate command and executed before the page was sent to you.

      <li class="menuClass" onclick="">
      


    You can try as shown below to pass it as query string instead of session attribute.

    <a href="/index.jsp?currentPage=firstPage">First Page</a>
    <a href="/index.jsp?currentPage=secondPage">Second Page</a>
    

    Note: Always try to avoid Scriplet elements and use JSP Standard Tag Library

    这篇关于ListItems中的JSP onclick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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