Struts ActionSupport 和静态方法和变量

发布于 2024-12-10 05:46:42 字数 2300 浏览 0 评论 0原文

我对 struts 和 jsp 很陌生,我必须做一个分配,其中我必须创建留言簿应用程序,

但当我尝试迭代 arrayList 时,我必须在 jsp 视图中进行分配,其中 gust 消息存储和列表是静态的,始终返回 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;
    }

}

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

i am very new to struts and jsp and i have to make a assignment in which i have to make guestbook app

but place stuck in jsp view when i amm try to iterate over arrayList in which gust message store and list is static always return null
my jspview is

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

and action class is

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

}

If I don't iterate it works fine.
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-12-17 05:46:42

这似乎是 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

This seems to be an issue with Struts 2 configuration. It is said that Struts 2 does not support the access for static variables. But, you can configure it in the properties file. Please refer the links below:

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

In the above link, see the last configuration. This is the thing to be changed.

See the link below:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文