Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在PlayFrameworkV1.2.7中使用动态id属性配置POST路由,以加载JPA实体_Java_Jpa_Playframework_Routes_Playframework 1.x - Fatal编程技术网

Java 如何在PlayFrameworkV1.2.7中使用动态id属性配置POST路由,以加载JPA实体

Java 如何在PlayFrameworkV1.2.7中使用动态id属性配置POST路由,以加载JPA实体,java,jpa,playframework,routes,playframework-1.x,Java,Jpa,Playframework,Routes,Playframework 1.x,我希望有一个http路由“POST/users/2”,该路由使用动作签名应用程序将表单提交给控制器。saveUser(用户用户用户)将根据URL中动态参数指定的实体id更新现有用户的详细信息。用户类型是JPA实体,如果在请求/表单中指定了参数“User.id”,那么JPA的播放支持将自动加载该实体。到目前为止,我一直将“user.id”指定为隐藏输入,但我确实希望该id成为URL的一部分 我在URL中包含用户id的第一个想法是定义如下路由: POST /users/{user.id} App

我希望有一个http路由“POST/users/2”,该路由使用动作签名应用程序将表单提交给控制器。saveUser(用户用户用户)将根据URL中动态参数指定的实体id更新现有用户的详细信息。用户类型是JPA实体,如果在请求/表单中指定了参数“User.id”,那么JPA的播放支持将自动加载该实体。到目前为止,我一直将“user.id”指定为隐藏输入,但我确实希望该id成为URL的一部分

我在URL中包含用户id的第一个想法是定义如下路由:

POST /users/{user.id}   Application.saveUser()
public static void save(Long id) {
    User user = User.findById(id);  // here you load the entity you want to edit by it's id
    user.edit("user", params.all());  // here the entity will be edited
    validation.valid(user);
    if(validation.hasErrors()) {
        edit(id);
    } else{
        user.save(); // explicit save here
        show(id);
    }
}
不幸的是,这会导致以下例外情况:

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.<init>(jregex/Pattern.java:150)
    at jregex.Pattern.<init>(jregex/Pattern.java:108)
    at play.mvc.Router$Route.compute(Router.java:755)
但这会产生预期的NoRouteFoundException,因为“id”与操作方法参数名称“user”不匹配。最后一个明显的选项是使用参数名“user”,并再次希望Play可以使用一些智能来确定这是id:

POST /users/{user}   Application.saveUser()
这确实使页面能够使用表单中指定的正确路由进行呈现,但在提交时未加载正确的JPA实体:

<form action="/users/3" method="post" accept-charset="utf-8" enctype="application/x-www-form-urlencoded">

同样,未加载正确的JPA实体。我到处搜索,但没有找到如何正确执行的示例。

播放文档中有一个示例显示了解决问题的方法: (本章中的第二个代码段)

您需要一个路由(如您在第二种方法中所建议的),其中您只需传递要编辑的模型的id:

POST /users/{id}   Application.saveUser()
相应的操作方法可能如下所示:

POST /users/{user.id}   Application.saveUser()
public static void save(Long id) {
    User user = User.findById(id);  // here you load the entity you want to edit by it's id
    user.edit("user", params.all());  // here the entity will be edited
    validation.valid(user);
    if(validation.hasErrors()) {
        edit(id);
    } else{
        user.save(); // explicit save here
        show(id);
    }
}

[Hi tazmaniax],只需尝试同时包含id和型号,但仅在路线中包含id

POST /users/{id}   Application.saveUser()

Application.saveUser(Long id, User user)

<form action="@{Application.saveUser(user.id)}" method="post".....
POST/users/{id}Application.saveUser()
Application.saveUser(长id,用户)

好的,这种方法更加明确,需要更多的样板文件——准确地说,前三行——来匹配Play JPA插件所完成的自动加载,如果实体id是以插件可以理解的格式提供的。我希望有一个稍微优雅一点的解决方案,这似乎是可能的,但这肯定是有效的,不是一个巨大的负担。谢谢你的提示,看起来真的很有趣,我很快会尝试的