English 中文(简体)
支撑和固定方法及变数
原标题:Struts ActionSupport and static method and variable
  • 时间:2011-10-15 15:47:57
  •  标签:
  • struts2

i 对闯入和 j来说是非常新的,而且我必须做一项任务,即我必须做客人笔。

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>

表格

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

最佳回答

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:

rel=“nofollow” 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.

见以下链接:

问题回答

暂无回答




相关问题
Getting sitemesh to work with struts2

I m trying to integrate sitemesh into my struts2 app, but i m finding that it s not making any difference and isn t showing any errors (or anything sitemesh related at all) in the logs. I ve started ...

Struts Action Validation

Hey People. I have been recently validating Struts 2 actions. In One of my action class, the save method to be precise must have two id s. That is an assessmenttype id and a course id. this is what is ...

struts2 action not calling properly

On default I want my struts2 app to forward to an action: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"...

How to validate a select tag in struts 2 framework

I am a newbie into programming and i am currently employed as a junior programmer. I am currently having some problems validating the select tags in one of my forms. What i actually trying to do is to ...

Setting the default value in Struts2

I am setting the value(kind of default value) for a drop down select value from action class in a page(given below). When the page loads the value is beig displayed but the other elements of the ...

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

热门标签