如何从JSP访问静态操作属性?班
我正在使用Struts2框架。我不知道为什么我不能在JSP页面中获取Action类的静态属性的值。在我的代码中,我指的静态属性是:如何从JSP访问静态操作属性?班,jsp,struts2,Jsp,Struts2,我正在使用Struts2框架。我不知道为什么我不能在JSP页面中获取Action类的静态属性的值。在我的代码中,我指的静态属性是:nbreAppelAction。结果,我在映射到execute()方法的stayIndexAction操作的所有调用中都得到了aa。在第一次打开我的index.jsp时,我无法获取0 这里是Action类: public class UserAction extends ActionSupport{ private static int nbreAppe
nbreAppelAction
。结果,我在映射到execute()
方法的stayIndexAction
操作的所有调用中都得到了aa
。在第一次打开我的index.jsp
时,我无法获取0
这里是Action类:
public class UserAction extends ActionSupport{
private static int nbreAppelAction = 0;
public String execute(){
utilisateur = new User();
nbreAppelAction++;
return SUCCESS;
}
public static int getNbreAppelAction() {
return nbreAppelAction;
}
public static void setNbreAppelAction(int nbreAppelAction) {
UserAction.nbreAppelAction = nbreAppelAction;
}
}
这里是index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>
<a href="<s:url action='stayIndexAction' />" >Stay in index.jsp </a>
</p>
<p>a<s:property value="nbreAppelAction" />a</p>
</body>
</html>
aa
您需要使用静态属性OGNL表示法,并允许访问静态属性:
@some.package.ClassName@FOO_PROPERTY
@some.package.ClassName@someMethod()
我也不确定你想在这里完成什么。IMO如果要保留应用程序范围的数据,请将其保留在所属的应用程序上下文中,并同步访问。空白太多;当静态变量与问题无关时进行修剪。您可以在操作中通过实例getter/setter公开静态变量,看看它是否解决了问题。我在struts.xml:中添加了这一行,在page.jsp:aa中添加了这一行,我也尝试了这一行:aa,但结果仍然是“aa”,而不是“a0a”..@user1459961它不是公共属性,不确定您希望调用的访问器是什么。我可以从JSP页面访问Action类的其他属性,而无需使用任何getter或setter,尽管它们声明为private。