ListItems中的JSP onclick [英] JSP inside ListItems 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页面被调用,按以下顺序发生: 因此,你的语句: 如何验证它? 在 在浏览器中单击右键,查看视图源。在
session.setAttribute
在HTML会发送给用户,而且 secondPage
。
div
之前添加下面的行,并查看它打印的内容。
<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%>
< c:out value =$ {sessionScope.currentPage}/>
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?
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 }" />
Right click in the browser and look at the view source. There is nothing inside
onclick
attribute because thesetAttribute
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屋!