playframework, need a good create, update, delete

2019-03-22 08:13发布

The examples in playframework seem to be lacking my example. The booking is the closest but it is an example of CRUD module and uses this parent() call which I am not sure what that does either. Is there an example without the CRUD module?

Also, all the validation sample examples call "render" on the methods for the http POSTs...is that the preferred method instead of redirect to a GET with the errors in the page?

The specific example I am looking for that would answer all these questions would be

  • create new object with editentity.html
  • rerender with errors
  • AND edit old object with editentity.html (same html page as above obviously).

Is there a good example of this?

MORE notes here... We have this from the calling page(or different calling pages) For adding a project

<a href="@{Project.editProject(null)">Add Project</a>

For editing a project

<a href="@{Project.editProject(project.name)">Edit</a>

BUT now if I do that, I struggle with the post now as I have

public static void postProject(ProjectDbo project) {

    if(validation.hasErrors()) {
        Validation.keep();
        params.flash();
        Project.editProject(???????/???); //I need to pass in the project to refill in the form AND the project.name for the routes file???
    }

    SomePage.pageXXXXX();
}

NOTE: the ???? is where I need to pass in projectDbo, but the method only accepts the projectName only. How to get the stuff to render back in the page again?

I think there must be a way to call editProject and set that projectDbo needs to be accessed by the page too or is there not a way to do this? How to do this pattern?

TRY #5: I did try using params.flash and calling Project.editProject(project.name) but unfortunately, none of my form is filled back in and the user loses ALL of his work that he filled in. The only thing I have working is using render(action, {params list not matching the action arguments})

(The booking example is lots of ajax so hard to see the example there).

this is such a common use case, I would have thought there would be an example for it.

thanks, Dean

4条回答
时光不老,我们不散
2楼-- · 2019-03-22 08:31

I have built exactly that example for a presentation at the Java User Group in Buenos Aires.

It's just a basic crud application, featuring validations, one-to-many relationship, fixtures, tests, tags, twitter bootstrap, and deployment to several PaaS cloud computing.

This is the github repo: https://github.com/opensas/play-demo

A detailed step by step tutorial: https://github.com/opensas/play-demo/wiki

The application is up and running on

Openshift, Google application engine, heroku, Cloudbees

Is is based on the zencontact example, which is also what you are looking for.

And about using the same form, this is the code from the app:

public static void form(Long id) {
    final Event event;
    if (id==null) {
        event = new Event();
    } else {
        event = Event.findById(id);
    }
    render(event);
}

public static void save(@Valid Event event) {
    if (validation.hasErrors()) {
        render("@form", event);
    }
    event.save();
    flash.success("event successfully saved!");
    list();
}

I hope it's useful...

Saludos

Sas

查看更多
小情绪 Triste *
3楼-- · 2019-03-22 08:35

About the "redirect after post" approach, I wrote an article on that subject (in spanish, sorry, you can try with google translate)

https://github.com/opensas/RedirectAfterPost/blob/master/README.md

And here's the git repo of a sample application: https://github.com/opensas/RedirectAfterPost

Basically there are two approaches:

Render again the same page without redirecting (like it's done on zentalk example)

pros:

  • we spare ourselves a redirect
  • we have no cookie size limit problem
  • the code is cleaner and more intuitive
  • it's easier to instantiare an object and show it in it's initial state, when we are creating an item

cons:

  • if after an error the user presses F5, the browser asks for confirmation for resubmitting

Save all the data and errors in flash, and force a redirect

pros:

  • the main advantage, is to prevent the user from issuing another post if, after receiving an error, the user presses F5 (it would just repeat the last GET to which it was redirected)

to test the sample just issue:

git clone git@github.com:opensas/RedirectAfterPost.git
cd RedirectAfterPost/ 
play run 
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-22 08:35

I ended up doing my own example that I can steal from with ajax included here

https://github.com/deanhiller/timecardz/blob/master/app/controllers/OurPattern.java

The view https://github.com/deanhiller/timecardz/tree/master/app/views/OurPattern

The important routes for POST are all generic so they can be used for ALL controllers not just this one

# Routes for all ajaxAddEdit and delete stuff
GET     /{controller}/ajaxAddEdit/{id}          {controller}.ajaxAddEdit
GET     /{controller}/ajaxDelete/{id}           {controller}.ajaxDelete

# Catch all
POST       /{controller}/{action}                  {controller}.post{action}

The GET route for the page is the only specific route then.

which for a bit of time I have a live version running at

http://myextremestore.com/ourpattern

The only thing that is kind of missing from that example is validation of the owning entity on POST calls(notice in my controller, methods that are post are called postXXX)

查看更多
在下西门庆
5楼-- · 2019-03-22 08:55

Is there a reason why

http://www.playframework.org/documentation/1.2.4/validation

Does not tell you everything you need to know regarding this? In particular the "validation.keep()" method.

查看更多
登录 后发表回答