Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I configure a POST route in Play Framework v1.2.7 with a dynamic id attribute that will load a JPA entity
    text
    copied!<p>I would like to have an http route "POST /users/2" that submits a form to a controller with action signature Application.saveUser(User user) that will update the details for an existing user based on the entity id specified a dynamic argument in the URL. The type User is a JPA entity and the Play support for JPA will automatically load the entity if a parameter "user.id" is specified in the request/form. Up until now I've been specifying the "user.id" as a hidden input but I really want the id to be part of the URL.</p> <p>My first idea to include the user id in the URL was to define a route like this:</p> <pre><code>POST /users/{user.id} Application.saveUser() </code></pre> <p>Unfortunately this gives the following exception:</p> <pre><code>Oops: PatternSyntaxException An unexpected error occured caused by exception PatternSyntaxException: unknow class: {user.id} play.exceptions.UnexpectedException: Unexpected Error at play.Invoker$Invocation.onException(Invoker.java:244) at play.Invoker$Invocation.run(Invoker.java:286) at Invocation.HTTP Request(Play!) Caused by: jregex.PatternSyntaxException: unknow class: {member.id} at jregex.CharacterClass.parseName(jregex/CharacterClass.java:361) at jregex.Term.append(jregex/Term.java:482) at jregex.Term.makeTree(jregex/Term.java:259) at jregex.Term.makeTree(jregex/Term.java:219) at jregex.Term.makeTree(jregex/Term.java:206) at jregex.Pattern.compile(jregex/Pattern.java:164) at jregex.Pattern.&lt;init&gt;(jregex/Pattern.java:150) at jregex.Pattern.&lt;init&gt;(jregex/Pattern.java:108) at play.mvc.Router$Route.compute(Router.java:755) </code></pre> <p>Another option I tried was to just use an 'id' argument name hoping Play might be able to match that to the user id attribute:</p> <pre><code>POST /users/{id} Application.saveUser() </code></pre> <p>But this gives the expected NoRouteFoundException because 'id' does not match the action method argument name 'user'. The final obvious option is to use the argument name 'user' and again hoping Play might use some intelligence to work out this is the id:</p> <pre><code>POST /users/{user} Application.saveUser() </code></pre> <p>This does enable the page to render with the correct route specified in the form but upon submit the correct JPA entity is not loaded:</p> <pre><code>&lt;form action="/users/3" method="post" accept-charset="utf-8" enctype="application/x-www-form-urlencoded"&gt; </code></pre> <p>The final way I did try was to do a mapping but not sure if this is supported:</p> <pre><code>POST /users/{user} Application.saveUser(user.id: user) </code></pre> <p>Again the correct JPA entity is not loaded. I've searched high and low but found no example of how this can be done correctly.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload