Best Practices for Struts 2 CRUD

So, I found a bunch of Struts 2 CRUD examples on the Internet:

Struts 2 CRUD Demo

and a few books:

Apache Struts 2 Web Application Development ISBN: 978-1847193391

Struts 2 Design and Programming ISBN: 978-0980331608

But they are all a little different than how to make a band.

Some suggest implementing ModelDriven or Prepareable Java interfaces to invoke the prep function to pre-populate all the required data elements.

Others suggest creating their own PrepareForUpdate action, which calls the prefill function and then redirects to the main editing window.

They are also very versed in how to pass the identifier of an object to indicate which object to receive for editing. SOme offers interceptors that others throw it in URL parameters and retrieve them through an ActionContext or pass them through s: hidden.

Is there a way with which you can perform bulk configuration in Struts 2?

What are the advantages / disadvantages of the above methods?

+3
source share
2 answers

I don’t know any documented best practices, but I have been using Webwork and Struts2 for about three years, so I can tell you what I used in my projects. By the way, the CRUD demo documentation you are involved with amazes me as a bit dated (I understand it from the project website).

CRUD :

  • , . .
  • , . prepare() ..
  • , .

Java- ModelDriven Prepareable , .

, , ModelDriven. . Struts2 ModelDriven, . ModelDriven , . , , , bean.

PrepareForUpdate, , .

, . HTTP- , prepare() .

, , , .

URL- . -.

+6

Struts 2 3 . ModelDriven Prepareable . () struts, , id . , , , Ajax. Ajax , . , , HTML .

, . , , . :

public class ApplicationAction extends MyBaseAction 
implements ModelDriven<Application>, Preparable {

    private static final long serialVersionUID = 7242685178906659449L;
    private ApplicationService applicationService;
    private Application application;
    private Integer id;
    List<Application> allApplications;

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }

    public Application getModel() {
        return application;
    }

    public void prepare() throws Exception {
        if(id == null || id.intValue() == 0){
            application= new Application();
        }else{
            application= applicationService.getApplication(id);
        }
    }

    @SkipValidation
    public String list() throws Exception {
        allApplications = applicationService.getApplications();
        return SUCCESS;
    }

    @Validations( visitorFields = {@VisitorFieldValidator(message = "Validation Error", fieldName = "model", appendPrefix = false)})
    public String update() throws Exception {
        applicationService.saveApplication(application);
        addActionMessage("Application Saved Successfully.");
        return SUCCESS;
    }

    public void setApplicationService(ApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    public List<Application> getAllApplications() {
        return allApplications;
    }

}
0

Source: https://habr.com/ru/post/1788288/


All Articles