简体   繁体   English

Web框架中的控制器? [红宝石,Grails等。]它们如何工作?

[英]controllers in web frameworks ? [ruby, Grails, etc..] How do they work?

So the thing that I want to do is direct the login page to the admin page(eg: "admin.gsp") if username "admin" is entered and password "pass" is entered. 因此,如果要输入用户名“ admin”并且输入密码“ pass”,我想做的就是将登录页面定向到管理页面(例如:“ admin.gsp”)。 or direct it to the user page if username "user" and password "pass" is entered 或将其定向到用户页面(如果输入了用户名“ user”和密码“ pass”)

However I do not understand how exactly controller works. 但是我不明白控制器是如何工作的。

So when you redirect the page to a specific controller and specific method, how do you make it redirect it to a specific page. 因此,当您将页面重定向到特定的控制器和特定的方法时,如何使它重定向到特定的页面。 What code do you implement inside that method ? 您在该方法内实现了什么代码?

to explain my question better. 更好地解释我的问题。 Below we have the code for controller User, with an if else statement depending on what the user typed in the login boxes. 下面是控制器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')

    }



}

now here, we have the admin controller, with the action index, that should have a code implemented to direct the page to... admin.gsp, or admin.html, watever the admin page is called 现在,在这里,我们有了带有操作索引的管理控制器,该控制器应具有实现将页面定向到...的代码。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 
    }
}

How do we exactly do that ? 我们到底该怎么做? I need a little bit of an explanation on controllers. 我需要对控制器进行一些说明。

Thank you. 谢谢。 Please ask if you need more clarification on the question as I will edit it. 请询问您是否需要进一步澄清该问题,因为我会对其进行编辑。

I think you are having wrong assumptions. 我认为您的假设有误。 In Grails (I don't know rails) when you go to the login page, let's say 在Grails(我不知道Rails)中,当您转到登录页面时,假设

/app/user/login

then, at FIRST the controller method is being called ( def login() ), so your logic shouldn't go there, it should actually do nothing and by convention it will end up rendering login.gsp . 然后,首先调用该控制器方法( def login() ),因此您的逻辑不应进入该位置,它实际上不应执行任何操作,并且按照惯例,它将最终呈现login.gsp Next, your user fills in his username/pass and submits form, there goes the second request let's say to 接下来,您的用户填写其用户名/密码并提交表单,然后第二个请求让我们说:

/app/user/auhtenticate

and here's where your logic should go and the redirects will work as you expect 这是您的逻辑应该去的地方,重定向将按预期工作

So basically - controller method is called first and (unless you specify gsp to be rendered or do a redirect) after the method is executed, the gsp view is being rendered 因此,基本上-控制器方法首先被调用,并且(除非您指定要渲染的gsp或执行重定向),在执行该方法后,将渲染gsp视图

I know nothing of rails, however in grails what you are looking for is render . 我对rails一无所知,但是在grails中,您正在寻找的是render

Render:( http://grails.github.io/grails-doc/latest/ref/Controllers/render.html ) 渲染:( http://grails.github.io/grails-doc/latest/ref/Controllers/render.html

so the code would look something like this 所以代码看起来像这样

 def index() { 
    render view: "/directory/inside/views/admin" model: [thing1: "Thing One"] 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM