I'm using the Play Framework for a web app in Java.
I'd like to put an Account controller in an "account" subpackage, for example:
|- controllers
|- account
|- Account.java
While my views are organized like:
|- views
|- Account
|- index.html
The Account.java file contains:
package controllers.account;
import play.mvc.Controller;
public class Account extends Controller {
public static void index() {
render();
}
}
I'd like to have the following behavior:
when a request is made to http://localhost/account/{action}
, it's redirected to the Account.java
controller that shows the view in the Account
folder.
Any tips?
Have you tried putting your views in a structure that matches your controller structure?
Beside that, you can always pass in the view name to the render() call:
I personally would always stick to the built-in structure that is supplied with play. Otherwise you could easily end up in refactoring hell, when you rearrange your controller structure somewhere down the road...
If you want to reference a controller located in a sub package from a view and use some structure like this
and a route configured in conf/routes as
one can use following to create a link to the method hello from the view
You need to define the route on conf/routes. Something like: * /account/${action} account.Account.${action}
If you do create a package structure, note that there's a few new syntaxes that are non-obvious:
Reverse lookup of controller
Put the package name between 'controllers' and 'routes':
E.g. in a view
View references in controller
Package name follows 'views.html':