开发者

I would like to be able to let people login/logout on the first page of my grails application

I'd like to have login and logout on the first page of my grails application and have any error messages displayed there. I have an authentication function coded in my User domain along with a logout function. After someone logs in in the authenticate function, I manage to get them back to the main screen by doing a redirect:

redirect(uri:"")

If I do the same thing with the logout, I get an error when the page is rendered, saying it can't find the user object. Below is the output.

Error 500: Executing action [logout] of controller [com.tho开发者_JS百科rnglatch.pingo.UserController] caused exception: No such property: name for class: com.thornglatch.pingo.User Possible solutions: games Servlet: grails URI: /Pingo/grails/user/logout.dispatch Exception Message: No such property: name for class: com.thornglatch.pingo.User Possible solutions: games Caused by: No such property: name for class: com.thornglatch.pingo.User Possible solutions: games Class: UserController At Line: [25]

Here is the stuff it's trying to render, I believe:

<div id="menu">
  <nobr>
    <g:if test="${session.user}">
      <b>${session.user?.firstName}&nbsp;${session.user?.lastName}</b> |
        <g:link controller="user" action="logout">Logout</g:link>
    </g:if>
    <g:else>
      <g:link controller="user" action="login">Login</g:link>
    </g:else>
  </nobr>
</div>

Thank you for your time and attention.


Here's the Authenticate code:

def authenticate = {
    println "Authenticate Entered"
    def user = User.findByLoginAndPassword(params.login, params.password)
    println "User = [" + user + "]"
    if (user) {
        session.user = user
        def name = user.firstName ?: user.lastName
        flash.message = "Hello ${user.login}"
        redirect(uri:"")
    } else {
        println "Should be saying sorry"
        flash.message = "Sorry, ${params.login}. Please try again.}"
        redirect(uri:"")
    }
} // authenticate

This version works. In the one that didn't, the flash.message line was:

flash.message = "Hello ${name}"

I can live with this, but would like to know why using name didn't work.


This doesn't look as a functional error, like the problem you've described. In fact the snippet seems okay, it's being invoked correctly.

The problem seems to be that you're attempting to access a property that doesn't exists (name) at runtime in the class com.thornglatch.pingo.User.

Have a look at com.thornglatch.pingo.UserController line 25 and check that the name you're accessing has a setter and getter or it is a public field in com.thornglatch.pingo.User. Or check that, if it's a variable, the declaration exists.

Remember that Groovy is a dynamic language, thus you have to be careful since member field access checks will happen at runtime.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜