简体   繁体   中英

Struts 2: Action classes

So, I have next situation:

I have a page with form -> this form submitted to the server -> On the server, present special struts 2 Action for processing submitted form -> and on the last step for user show again this page with form.

when, I try testing this workflow I see one strange for me moment:

When I first time open page - form fields - empty, but after submitted form on the server and reload page - fields - not empty! Fields contains data, that I fill on previous steps.

Here my test Action:

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport {

    private List<Integer> books;
    private String title;
    private String description;

    public List<Integer> getBooks() {
        return this.books;
    }

    public void setBooks(List<Integer> books) {
        this.books = books;
    }

    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    private static final long serialVersionUID = 1L;

    public String show() {

        // filling books variable

        List<Integer> l = new ArrayList<Integer>();

        l.add(1);
        l.add(2);
        l.add(3);

        this.books = l;
        return Action.SUCCESS;
    }

    public String save() {

        // processing title and description variables here!

        return show();

    }

}

My test jsp file:

<%@ taglib prefix="s" uri="/struts-tags"%>

<html>
<body>

    <s:property value="books" />


    <s:form action="saveAction">
        <s:textfield id="title" label="Title" name="title" />
        <s:textfield id="description" label="Description"
            name="description" />
        <s:submit />
    </s:form>


</body>
</html>

And this small part my struts.xml with action tags:

    <action name="showAction" class="action.TestAction" method="show">
        <result>pages/test.jsp</result>
    </action>

        <action name="saveAction" class="action.TestAction" method ="save">
        <result>pages/test.jsp</result>
    </action>

so, I start investigate this problem, and what I have in result:

Action class instantiate only one time, and after all user request work with this instance of Action class.

so, if in my Action class I have few action methods - all this methods have access to all fields.

It is a good idea?

for example: user send request to the my Action.remove method with some id .

Next, user send request to the my Action.add method but without id parameter. - this situation must be failed! (for example - I forget add Validation).

but - nothing wrong - action instance contains value for field id .

So, my general question next:

1. One Action Class - One Method - it is the best practice?

2. If used few methods in one Action - I need add some service methods for clear modified fields? Or what I must do in this case?

Thanks!

The only way you'd get a single instance of an action class is if (a) you're using Spring, and (b) your actions are declared as default (singleton) scope. Otherwise, without Spring or with prototype scoping, you'll always get a new action instance per-request.

1) It depends. I tend to lump tightly-coupled action methods into a single action class, but I don't know as it's a "best practice" or not--I think it depends on the situation and preference. I don't see many compelling technical benefits to one over another.

For me it's a matter of readability and functional locality.

2) Your configuration is almost certainly wrong if you're getting the exact same action instance over multiple requests--I'd fix that before doing much else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM