所以我成功登录了我的 grails 应用程序。但我想将一些功能分离到不同的控制器中。
例子:
package MyApp
class CustomerController {
/*Keeping the scaffold for admin purposes.*/
static scaffold = Customer
//write redirects and make exceptions for scaffolds
// def index() { }
/*
* Access controllers are ordered after application flow.
**/
def login(){
}
/*
* Authentication based on email adress as the identifier.
*
**/
def authenticate() {
def authPwd
def authUser
if(User.findByEmail(params.email) == null){
flash.message = "login failed"
redirect(action: "login")
}else{
authUser = User.findByEmail(params.email)
authPwd = authUser.getPassword()
if(authPwd == params.pwd){
session["user"] = authUser
session["customer"] = authUser.getCompany()
println "current user is: " + session["user"]
println "current customer is: " + session["customer"]
redirect(controller: "portal", action: "start")
}else{
flash.message = "login failed"
redirect(action: "login")
}
}
}
def logout() {
session.invalidate()
redirect(action: "login")
}
}
所以我想要做的是把 authenticate() 从这里拿出来,把它放在一个新的控制器中,叫做 AuthenticateController()
只是,作为 grails 的新手,我的重定向不起作用,我希望有人能给我一个想法。
所以我以为你会拿出
def authenticate {
/* some code*/
}
把它放在新的控制器中。然后只需使用
redirect(controller: "authenticate", action: "authenticate")
但这不起作用,我得到一个错误异常。
任何帮助将非常感激
编辑:出于以下评论的目的...
class CustomerController {
/*Keeping the scaffold for admin purposes.*/
static scaffold = Customer
//write redirects and make exceptions for scaffolds
// def index() { }
/*
* Access controllers are ordered after application flow.
**/
def login(){
redirect(controller:"authenticate", action:"authenticate")
}
/*
* Authentication based on email adress as the identifier.
*
**/
def logout() {
session.invalidate()
redirect(action: "login")
}
}
然后是 AuthenticateController
class AuthenticateController {
def authenticate() {
def authPwd
def authUser
if(User.findByEmail(params.email) == null){
flash.message = "login failed"
redirect(contoller:"customer", action: "login")
}else{
authUser = User.findByEmail(params.email)
authPwd = authUser.getPassword()
if(authPwd == params.pwd){
session["user"] = authUser
session["customer"] = authUser.getCompany()
println "current user is: " + session["user"]
println "current customer is: " + session["customer"]
redirect(controller: "portal", action: "start")
}else{
flash.message = "login failed"
redirect(contoller:"customer", action: "login")
}
}
}
}
是代码不起作用。基本上是复制粘贴。
Tomcat 运行但首先出现 100 个错误
type Status report
message /ClipEdit_v1/authenticate/login
description The requested resource is not available.