我正在使用Struts2 Framework。我不知道为什么我不能在JSP页面中获得Action类的静态属性的值。在我的代码中,静态属性的意思是: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>
您需要使用静态属性OGNL表示法,并允许访问静态属性:
@some.package.ClassName@FOO_PROPERTY
@some.package.ClassName@someMethod()
http://struts.apache.org/2.x/docs/ognl-basics.html
我也不太确定你想在这里完成什么。IMO如果您想保留应用程序范围内的数据,请将其保存在所属的应用程序上下文中,并同步访问。