Why play use static method in Controller?

1,365 views
Skip to first unread message

leo hu

unread,
Nov 11, 2011, 12:33:47 AM11/11/11
to play-fr...@googlegroups.com
Hey folks,

Why play use static method ?

public class Application extends Controller {
    public static void index() {
        render();
    }
}


I think if we may remove the static, it's more elegant and cleaner.

public class Application extends Controller {
    public void index() {
        render();
    }
}

green

unread,
Nov 11, 2011, 12:45:22 AM11/11/11
to play-fr...@googlegroups.com
Play guys think action methods in controller is somehow the void main(String[]) method in console app, they are just entries. People are NOT encouraged to put complex logic inside controller methods. And action method overwriting via inheritance is not a good practice.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

leo hu

unread,
Nov 11, 2011, 1:04:57 AM11/11/11
to play-fr...@googlegroups.com

Thanks very much, Geen. I understand you. But I think it's still a bit odd to use static method. 
I don't know whether play2.0 release version will amend it or not, I wanna hear other guys' sound.

Yann Simon

unread,
Nov 11, 2011, 2:22:50 AM11/11/11
to play-fr...@googlegroups.com
2011/11/11 leo hu <superle...@gmail.com>:

> Hey folks,
> Why play use static method ?

I like using static methods for controlers, because a controler is stateless.
A non static method can access the private attributes, that represent the state.
A static method not.

For example, in JSF, a statefull framework, a managed bean has
non-static actions, that use the state saved in private attributes.

I do not know whether that would be the explanation for Play!
creators, but that is how I understood this.

Yann

green

unread,
Nov 11, 2011, 3:37:29 AM11/11/11
to play-fr...@googlegroups.com
+1 for stateless

Tom Carchrae

unread,
Nov 11, 2011, 3:56:11 AM11/11/11
to play-fr...@googlegroups.com
why do you think the removal of 'static' is more elegant and cleaner?

I think the use of static is perfect here.

leo hu

unread,
Nov 11, 2011, 4:01:02 AM11/11/11
to play-fr...@googlegroups.com
Maybe I am a newbie in Play. I have used some other frameworks before, most of them use non-static as controller.

sas

unread,
Nov 11, 2011, 5:04:38 AM11/11/11
to play-framework
play favours extension thru composition (using the @With annotation)
instead of thru inheritance

have a look at guillaume's anser to this SO question

http://stackoverflow.com/questions/5192904/play-framework-uses-a-lot-of-statics

Play uses static methods only when it makes sense:

- in the controller layer, because controllers are not object
oriented. Controllers act as mapper between the HTTP world (that is
stateless, and request/response based) and the Model layer that is
fully object oriented.
- in the model layer for factory methods, like findAll(), count(),
create() which of course don't depend of any particular instances
- in some play.libs.* classes that provides purely utility
functions

this question is also quite interesting

http://stackoverflow.com/questions/7412958/are-there-any-side-effect-of-using-to-many-static-function

I think that static controllers is a way to encourage stateless and
simple controllers...

Anyway I think it would be interesting know the opinion of the rest
about the pros/cons of having static controllers...

saludos

sas

ps: in play 2.0, the java version, controllers are also static -
https://github.com/playframework/Play20/blob/master/samples/java/zentasks/app/controllers/Application.java

in the scala version controllers are singletons...




On Nov 11, 6:01 am, leo hu <superleo.pek...@gmail.com> wrote:
> Maybe I am a newbie in Play. I have used some other frameworks before, most
> of them use non-static as controller.
>
>
>
>
>
>
>
> On Fri, Nov 11, 2011 at 4:56 PM, Tom Carchrae <t...@carchrae.net> wrote:
> > why do you think the removal of 'static' is more elegant and cleaner?
>
> > I think the use of static is perfect here.
>
> > On 11/11/11, green <greenlaw...@gmail.com> wrote:
> > > +1 for stateless
>
> > > On Fri, Nov 11, 2011 at 6:22 PM, Yann Simon <yann.simon...@gmail.com>
> > wrote:
>
> > >> 2011/11/11 leo hu <superleo.pek...@gmail.com>:

Adam Brimo

unread,
Nov 11, 2011, 5:04:54 AM11/11/11
to play-fr...@googlegroups.com
Well you're in for a pleasant surprise because Play is way better than any of those other frameworks you've used before :)

Adam

leo hu

unread,
Nov 11, 2011, 10:48:27 AM11/11/11
to play-fr...@googlegroups.com
Currently maybe you are right.  But no best, only better. I will still pay more attention to play and other frameworks.
Because most of the pending projects still use SSH and other similar techniques. I suffer them every day. LOL

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/vcU0hzBgASUJ.
Reply all
Reply to author
Forward
0 new messages