Struts ActionSupport和静态方法和变量

时间:2011-10-15 15:47:58

标签: struts2

我对struts和jsp很新,我必须做一个我必须制作留言簿应用的作业

但是当我试图迭代在arrayList中的gust消息存储和列表是静态的时候卡在jsp视图中总是返回null 我的jspview是

<body>
    <a href="<s:url action="GuestBook"/>">Click </a>here for sign guest book<br/><br/>
    <table>
        <s:iterator value="messages">
            <tr>
                <td><s:property value="when"/><br/></td>
                <td><s:property value="guest"/><br/></td>
                <td><s:property value="message"/><br/></td>
            </tr>
        </s:iterator>
    </table>
</body>

和动作类是

public class GuestBookAction extends ActionSupport {
    private String guest, message;
    private String when = new Date().toString();
    private static  ArrayList<GuestBook> messages = new ArrayList<GuestBook>();

    static {
        messages.add(new GuestBook("1","1","1"));
        messages.add(new GuestBook("2","1","1"));
        messages.add(new GuestBook("3","1","1"));
        messages.add(new GuestBook("4","1","1"));
        messages.add(new GuestBook("5","1","1"));
    }

    @Override
    public String execute(){
        boolean add = messages.add(new GuestBook(this.getGuest(), this.getMessage(), this.getWhen()));
        if (add) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String getGuest() {
        return guest;
    }

    public void setGuest(String guest) {
        this.guest = guest;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public  ArrayList<GuestBook> getMessages() {
        return messages;
    }

    public  void setMessages(ArrayList<GuestBook> messages) {
        GuestBookAction.messages = messages;
    }

    public static Logger getLOG() {
        return LOG;
    }

    public static void setLOG(Logger LOG) {
        ActionSupport.LOG = LOG;
    }

    public String getWhen() {
        return when;
    }

    public void setWhen(String when) {
        this.when = when;
    }

}

如果我不迭代它工作正常。 感谢

1 个答案:

答案 0 :(得分:0)

这似乎是Struts 2配置的问题。据说Struts 2不支持访问静态变量。但是,您可以在属性文件中配置它。请参考以下链接:

http://struts.apache.org/2.0.14/docs/strutsproperties.html

在上面的链接中,请参阅上一个配置。这是要改变的事情。

请参阅以下链接:

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