[英]controllers in web frameworks ? [ruby, Grails, etc..] How do they work?
因此,如果要输入用户名“ admin”并且输入密码“ pass”,我想做的就是将登录页面定向到管理页面(例如:“ admin.gsp”)。 或将其定向到用户页面(如果输入了用户名“ user”和密码“ pass”)
但是我不明白控制器是如何工作的。
因此,当您将页面重定向到特定的控制器和特定的方法时,如何使它重定向到特定的页面。 您在该方法内实现了什么代码?
更好地解释我的问题。 下面是控制器User的代码,其中包含if else语句,具体取决于用户在登录框中键入的内容。
package file_download
class UserController {
def index() {
//code that directs the page to username page, let's call it or user.gsp
}
def login = {
if(params.username == "admin" && params.password == "pass")
{
session.user = "admin"
redirect(controller:"admin",action:"admin")
}
else if(params.username == "user" && params.password == "pass")
{
session.user="user"
redirect(controller:"user",action:"index")
}
else
{
flash.message = "login failed"
}
redirect(controller:"user",action:"login")
}
def logout = {
session.user = null
redirect(action: 'index')
}
}
现在,在这里,我们有了带有操作索引的管理控制器,该控制器应具有实现将页面定向到...的代码。admin.gsp或admin.html,称为admin的页面
package file_download
class AdminController {
def index() {
//code that takes us to admin page let's call it admin.gsp
}
}
我们到底该怎么做? 我需要对控制器进行一些说明。
谢谢。 请询问您是否需要进一步澄清该问题,因为我会对其进行编辑。
我认为您的假设有误。 在Grails(我不知道Rails)中,当您转到登录页面时,假设
/app/user/login
然后,首先调用该控制器方法( def login()
),因此您的逻辑不应进入该位置,它实际上不应执行任何操作,并且按照惯例,它将最终呈现login.gsp
。 接下来,您的用户填写其用户名/密码并提交表单,然后第二个请求让我们说:
/app/user/auhtenticate
这是您的逻辑应该去的地方,重定向将按预期工作
因此,基本上-控制器方法首先被调用,并且(除非您指定要渲染的gsp或执行重定向),在执行该方法后,将渲染gsp视图
我对rails一无所知,但是在grails中,您正在寻找的是render
。
渲染:( http://grails.github.io/grails-doc/latest/ref/Controllers/render.html )
所以代码看起来像这样
def index() {
render view: "/directory/inside/views/admin" model: [thing1: "Thing One"]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.