Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7972941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:58:55+00:00 2026-06-04T07:58:55+00:00

I’m gonna try to explain at my best. I use Play Framework 2, and

  • 0

I’m gonna try to explain at my best.

I use Play Framework 2, and I will do a lot of CRUD actions. Some of them will be identitcal, so I’d like to KISS and DRY so at first I was thinking about an abstract class containing the list, details, create, update and delete methods, with generic object, and extend this class by specifying which object to use (Model & Form) :

public abstract class CrudController extends Controller {
    protected static Model.Finder<Long, Model> finder = null;
    protected static Form<Model> form = null;

    public static Result list() {
        // some code here
    }

    public static Result details(Long id) {
        // some code here
    }

    public static Result create() {
        // some code here
    }

    public static Result update(Long id) {
        // some code here
    }

    public static Result delete(Long id) {
        // some code here
    }
}

And a class that will use CRUD :

public class Cities extends CrudController {
    protected static Model.Finder<Long, City> finder = City.find;
    protected static Form<City> form = form(City.class);

    // I can override a method in order to change it's behavior :
    public static Result list() {
        // some different code here, like adding some where condition
    }
}

This would work if I wasn’t in a static context.

But since it’s the case, how can I do ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:58:57+00:00Added an answer on June 4, 2026 at 7:58 am

    This can be achieved using delegation: define a regular Java class containing the CRUD actions logic:

    public class Crud<T extends Model> {
    
        private final Model.Finder<Long, T> find;
        private final Form<T> form;
    
        public Crud(Model.Finder<Long, T> find, Form<T> form) {
            this.find = find;
            this.form = form;
        }
    
        public Result list() {
            return ok(Json.toJson(find.all()));
        }
    
        public Result create() {
            Form<T> createForm = form.bindFromRequest();
            if (createForm.hasErrors()) {
                return badRequest();
            } else {
                createForm.get().save();
                return ok();
            }
        }
    
        public Result read(Long id) {
            T t = find.byId(id);
            if (t == null) {
                return notFound();
            }
            return ok(Json.toJson(t));
        }
    
        // … same for update and delete
    }
    

    Then you can define a Play controller having a static field containing an instance of Crud<City>:

    public class Cities extends Controller {
        public final static Crud<City> crud = new Crud<City>(City.find, form(City.class));
    }
    

    And you’re almost done: you just need to define the routes for the Crud actions:

    GET     /                     controllers.Cities.crud.list()
    POST    /                     controllers.Cities.crud.create()
    GET     /:id                  controllers.Cities.crud.read(id: Long)
    

    Note: this example produces JSON responses for brevety but it’s possible to render HTML templates. However, since Play 2 templates are statically typed you’ll need to pass all of them as parameters of the Crud class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.