Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Views Customization in Java #99

Closed
mfirry opened this Issue Nov 19, 2012 · 23 comments

Comments

Projects
None yet
8 participants

mfirry commented Nov 19, 2012

it'd be great to have even just a glance at the java code neede do customize views.

public class MyViews implements TemplatesPlugin {

    @Override
    public <A> Html getLoginPage(Request<A> arg0,
            Form<Tuple2<String, String>> arg1, Option<String> arg2) {
        return ???
    }

...
Owner

jaliss commented Nov 19, 2012

The TemplatesPlugin is a Scala plugin. But, since the implementation is just a one line invoking the right template you can base your own plugin in BaseTemplatePlugin and just change the routes that are rendered. Something like the sample here should work: http://securesocial.ws/guide/views-customization.html.

Let me know how it goes.

mfirry commented Nov 19, 2012

thanks for your quick reply. I had read that guide page, but it is somehow not helping.

how would views.custom.html.login(form, msg) be translated in java?

Owner

jaliss commented Nov 19, 2012

You don't need to translate that to Java. That plugin is only exposed in Scala, but the implementation is really easy so there's no need for Scala knowledge.

If you follow the sample in the doc or the DefaultTemplatesPlugin in the module sources you can see that you only need to implement a line for each method. That line is just a reference to your own template file and that should work.

Let me know if this helps and if all works ok.

mfirry commented Nov 19, 2012

alright. i'll try implementing it in scala (stupid eclipse won't accept a single scala class inside a java project... i'm now back to sublime). thanks a lot.

@mfirry mfirry closed this Nov 19, 2012

@mfirry mfirry reopened this Nov 19, 2012

mfirry commented Nov 19, 2012

ok i've got http://pastie.org/5402875
so this DefaultTemplatesPlugin.scala file inside my controllers folder, next to Application.java.

and i've got

! @6ccpggnoi - Internal server error, for request [GET /] ->

play.api.PlayException: Cannot load plugin [Plugin [controllers.DefaultTemplatesPlugin] cannot been instantiated.]
    at play.api.PlayException$.apply(Exceptions.scala:122) ~[play_2.9.1-2.0.4.jar:2.0.4]
    at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:171) ~[play_2.9.1-2.0.4.jar:2.0.4]
    at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:160) ~[play_2.9.1-2.0.4.jar:2.0.4]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
    at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:0.11.3]
Caused by: java.lang.NoSuchMethodException: controllers.DefaultTemplatesPlugin.<init>(play.Application)
    at java.lang.Class.getConstructor0(Class.java:2706) ~[na:1.6.0_37]
    at java.lang.Class.getConstructor(Class.java:1657) ~[na:1.6.0_37]
    at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:167) ~[play_2.9.1-2.0.4.jar:2.0.4]
    at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:160) ~[play_2.9.1-2.0.4.jar:2.0.4]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
    at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]

I'm trying to do the same thing in Java, would appreciate any updates if you get it to work!

Owner

jaliss commented Nov 24, 2012

I think you are missing the public constructor. Add it and let me know if it works.

jaliss, great plugin.

To customize views, I am also getting the "Plugin cannot be initialized" error and not clear on adding a public constructor. Any help will be appreciated.

Owner

jaliss commented Nov 26, 2012

Check the source of the default implementation and add a constructor like
that. It should work.

Let me know.

On Sunday, November 25, 2012, suresh12 wrote:

jaliss, great plugin.

To customize views, I am also getting the "Plugin cannot be initialized"
error and not clear on adding a public constructor. Any help will be
appreciated.


Reply to this email directly or view it on GitHubhttps://github.com/jaliss/securesocial/issues/99#issuecomment-10701411.

I do have it similar to the default implementation, shared the source at http://pastie.org/5434014. Still the same error.

Owner

jaliss commented Nov 26, 2012

Can you share the stack trace?

On Sunday, November 25, 2012, suresh12 wrote:

I do have it similar to the default implementation, shared the source at
http://pastie.org/5434014. Still the same error.


Reply to this email directly or view it on GitHubhttps://github.com/jaliss/securesocial/issues/99#issuecomment-10701837.

Please see the stacktrace below.

play.api.PlayException: Cannot load plugin [Plugin [controllers.security.SecureViewsPlugin] cannot been instantiated.]
        at play.api.PlayException$.apply(Exceptions.scala:122) ~[play_2.9.1.jar:2.0.4]
        at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:171) ~[play_2.9.1.jar:2.0.4]
        at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:160) ~[play_2.9.1.jar:2.0.4]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
        at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) ~[scala-library.jar:0.11.3]
Caused by: java.lang.NoSuchMethodException: controllers.security.SecureViewsPlugin.(play.Application)
        at java.lang.Class.throwNoSuchMethodException(Class.java:286) ~[na:1.6.0]
        at java.lang.Class.getConstructor(Class.java:333) ~[na:1.6.0]
        at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:167) ~[play_2.9.1.jar:2.0.4]
        at play.api.Application$$anonfun$7$$anonfun$apply$7.apply(Application.scala:160) ~[play_2.9.1.jar:2.0.4]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
        at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194) ~[scala-library.jar:0.11.3]
Owner

jaliss commented Nov 26, 2012

I think I know what's going on. Can you try changing your app main language to Scala? This is just a test but want to see if it works. You change that in your Build.scala file.

Changed the main language to SCALA, did a clean, compile and run. I am getting the same error.

Owner

jaliss commented Nov 26, 2012

Will try to reproduce it locally.

On Sunday, November 25, 2012, Suresh Unnikrishnan wrote:

Changed the main language to SCALA, did a clean, compile and run. I am
getting the same error.


Reply to this email directly or view it on GitHubhttps://github.com/jaliss/securesocial/issues/99#issuecomment-10702534.

Owner

jaliss commented Nov 28, 2012

I implemented a view plugin using the following constructor and worked fine:

class CustomTemplates(application: play.Application) extends TemplatesPlugin 

Make sure you use play.Application because you get that object and not the scala class when running from a Java app.

@jaliss jaliss closed this Nov 28, 2012

That solved the issue. Thanks.

I work with play 2.1.1. And have next issue (very similar to previous issues):
Plugin [controllers.plugin.CustomTemplatesPlugin] cannot been instantiated.

This is stack trace:

[error] application -
! @6emalpck2 - Internal server error, for (GET) [/] ->

play.api.PlayException: Cannot load plugin[Plugin [controllers.plugin.CustomTemplatesPlugin] cannot been instantiated.]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:156) ~[play_2.10.jar:2.1.1]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10.jar:2.1.1]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:na]
at scala.collection.immutable.List.foreach(List.scala:309) ~[scala-library.jar:na]
at scala.collection.TraversableLike$class.map(TraversableLike.scala:244) ~[scala-library.jar:na]
Caused by: java.lang.ClassNotFoundException: controllers.plugin.CustomTemplatesPlugin
at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.7.0_25]
at java.net.URLClassLoader$1.run(Unknown Source) ~[na:1.7.0_25]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_25]
at java.net.URLClassLoader.findClass(Unknown Source) ~[na:1.7.0_25]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_25]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.7.0_25]

Could you help me?

Contributor

magro commented Jun 27, 2013

Do you have created a controllers.plugin.CustomTemplatesPlugin in your app?

I missed "plugin". My CustomPluginTemplate is in "controllers.CustomTemplatesPlugin".
App is working good. Thank you.

bichocj commented Sep 26, 2014

I getting the same with play 2.3.1 and securesocial-master
Plugin [controllers.CustomViews] cannot be instantiated.

I have in the file play.plugins
9997:controllers.CustomViews

and my scala class have
class CustomViews(application:play.Application) extends ViewTemplates{

override def getLoginPage(form: Form[(String, String)], msg: Option[String])(implicit request: RequestHeader, lang: Lang): Html = {
new Html("hello world")
}
...
}

Please help me

Contributor

jeantil commented Sep 29, 2014

bichocj commented Sep 29, 2014

excelent @jeantil , many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment