So I am starting out using the play framework with Mongo and have an issue with trying to get something working using the simple MVC pattern. I can’t see the wood for the trees with this and know the solution is bound to be simple.
I have a user object defined as an entity:
package models;
import play.modules.mongo.MongoEntity;
import play.modules.mongo.MongoModel;
@MongoEntity("users")
public class User extends MongoModel
{
public String username;
public String email;
public String password;
}
And I have a controller with the following two methods in it:
package controllers;
import models.User;
import play.mvc.Controller;
import play.Logger;
public class UserController extends Controller
{
public static void createUser()
{
User user = new User();
render(user);
}
public static void insertUser(User iUser)
{
Logger.info("Paul 1: " + iUser + " :: " + (iUser == null));
}
}
And the routes defined as follows:
POST /insertUser UserController.insertUser
GET /users UserController.createUser
So I have the following page view defined and when I click save, the code in the controller is telling me that the User object is null:
#{extends 'main.html' /}
#{set title:'Create User' /}
<form action="@{UserController.insertUser(user)}" method="POST"/>
Username: <input type="text" value="${user.username}" /><br/>
Password: <input type="text" value="${user.password}" /><br/>
Email: <input type="text" value="${user.email}" /><br/>
<input type="submit" value="Add User" />
</form>
I have tried a ton of permutations on the acton and can’t figure out why it is not working. Any ideas? All help is greatly appreciated, I know it is somethign stupid I am missing, it is just what.
I think you’re at least missing the
nameatrributes on the HTML forum input elements, which Play! will use to map the form values to your controller parameter(s). Try the following:Alternatively, you can use the template
#{form}tag to have the templating system generate the form for you. This might also be useful to cross-check the output of the#{form}tag with your own form to see what is different.Update: you may also have to delete the
userparameter in the form action attribute@{UserController.insertUser(user)}, which would then be more like@{UserController.insertUser}.