user1459961
user1459961

Reputation:

How can I access a static action property from JSP? class

I'm working with Struts2 Framework. I don't know why I can't get the value of a static attribute of an Action class in a JSP page. In my code the static attribute I mean is : nbreAppelAction. As a result, I get aa in all the calls of stayIndexAction action which is mapped to execute() method. I can't get either 0 in the first time I open my index.jsp.

Here it is the Action class :

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;
    }       
}

An here it is 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>

Upvotes: 0

Views: 1439

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

You need to use static property OGNL notation, and allow access to static properties:

@some.package.ClassName@FOO_PROPERTY
@some.package.ClassName@someMethod()

http://struts.apache.org/2.x/docs/ognl-basics.html

I'm not really sure what you're trying to accomplish here, either. IMO if you want to keep app-wide data, keep it in the application context where it belongs, and synchronize access.

Upvotes: 2

Related Questions