在我的网站上,我在主导航栏上有一个下拉菜单。我希望这个下拉列表中的所有页面都受到限制,因此需要登录才能查看它们,如果用户没有登录,用户将被重定向到登录屏幕。我把所有的剧本都整合了!对我的项目中的代码进行身份验证,并查看示例项目播放-身份验证-使用情况。在他们的示例中,他们有一个在Application.java中调用此方法的受限页面:
@Restrict(Application.USER_ROLE)
public static Result restricted() {
final User localUser = getLocalUser(session());
return ok(restricted.render(localUser));
}此方法返回要查看的呈现页。我尝试复制此方法,以便返回我想要返回的受限页面:
@Restrict(Application.USER_ROLE)
public static Result restrictedCreate() {
final User localUser = getLocalUser(session());
return ok(journeyCreator.render(localUser));
}我添加了这个新方法--路由文件:
GET /restricted controllers.Application.restrictedCreate()并通过下拉代码进行修改,以便调用我的新方法:
<li><a href="@routes.Application.restrictedCreate()"><i class="icon-plus-sign"></i> @Messages("journeys.dropdown.option1")</a></li>在这个阶段,我得到了一个编译错误:error: method render in class journeyCreator cannot be applied to given types;,所以我检查了正在呈现journeyCreator.scala.html的页面,并添加了localUser: models.User = null论证。我的journeyCreator.scala.html现在如下所示:
@(localUser: models.User = null, listJourneys: List[Journey], journeyForm: Form[Journey])
@import helper._
@main("Journey Creator", "journeys") {
......
}
}然而,这样做会导致各种错误:error: method render in class journeyCreator cannot be applied to given types;在其他与journeyCreator.scala.html有关的方法中。任何帮助都是非常感谢的。
发布于 2012-11-28 10:07:18
您声明了视图的参数(这是函数),但是没有传递它们,所以它导致了问题。
虽然在Scala函数(类似于PHP)中,您可以为params设置默认值,但是Java有问题,所以您需要在这个地方传递一些东西,它可能只是.null
public static Result restrictedCreate() {
final User localUser = getLocalUser(session());
return ok(journeyCreator.render(localUser, null, null));
}稍后,在视图中使用@if(localUser!=null){ ... }条件确保您有所需的东西。
https://stackoverflow.com/questions/13592830
复制相似问题