如何在刷新页面时增加.jsp页面中的变量
How to increase a variable in a .jsp page when the page is refreshed
在我的 Liferay portlet view.jsp 页面中,我有 int i =0;
(我知道在 jsp 页面中包含 java 代码并不好,但我必须这样做)现在我需要在刷新页面时增加它。我添加了<META HTTP-EQUIV="refresh" CONTENT="4">
来刷新页面。
也许可以选择将此变量存储在 portlet 会话范围内并在页面请求时递增。
你必须在会话对象中初始化计数
在此页面之前使用 setattribute 函数在会话对象中设置它
并在您要检查它的页面中使用以下代码
<%
HttpSession session = request.getSession(false);
if(session!=null){
Integer Count=
(Integer)session.getAttribute("Count");
if( Count==null || Count== 0 ){
/* First visit */
out.println("Welcome to my website!");
Count=count+ 1;
}else{
Count=count+ 1;
out.println("Welcome back again for the "+Count+"Time");
}
session.setAttribute("Count", Count);
%>
尝试使变量静态化
<% static int i = 0; %>
试试这个代码:
<%! int i =1; %>
<%
System.out.println(i++);
%>
但是正如您自己所说,不建议在 .jsp 页中使用 java 代码。
在我的 Liferay portlet view.jsp 页面中,我有 int i =0;
(我知道在 jsp 页面中包含 java 代码并不好,但我必须这样做)现在我需要在刷新页面时增加它。我添加了<META HTTP-EQUIV="refresh" CONTENT="4">
来刷新页面。
也许可以选择将此变量存储在 portlet 会话范围内并在页面请求时递增。
你必须在会话对象中初始化计数
在此页面之前使用 setattribute 函数在会话对象中设置它
并在您要检查它的页面中使用以下代码
<%
HttpSession session = request.getSession(false);
if(session!=null){
Integer Count=
(Integer)session.getAttribute("Count");
if( Count==null || Count== 0 ){
/* First visit */
out.println("Welcome to my website!");
Count=count+ 1;
}else{
Count=count+ 1;
out.println("Welcome back again for the "+Count+"Time");
}
session.setAttribute("Count", Count);
%>
尝试使变量静态化
<% static int i = 0; %>
试试这个代码:
<%! int i =1; %>
<%
System.out.println(i++);
%>
但是正如您自己所说,不建议在 .jsp 页中使用 java 代码。