简体   繁体   中英

URL mappings not working properly in grails

I am doing an application which is gTunes from the-definitive-guide-to-grails definitive-guide-to grails

In that I used custom url to redirect to store controller which is

UrlMappings.groovy
class UrlMappings {

    static mappings = {
        "/"(controller:"store")
}
}

so when I start the application it will to the default store controller and I have User domain class as well as UserController class which have actions register and login

User.groovy
class User {
String login
String password
...
}
UserController.groovy
class userController.groovy
{
def register(){
...}
def login(){
..}

StoreController.groovy
class StroeController{
def index(){
..}

The USerController have corresponding view pages under user/register.gsp and store/index.gsp the

store/index.gsp is
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<meta name="layout" content="main"/>
<title>gTunes Store</title>
</head>
<body id="body">
Your Online music store and storage service!
<p>Manage your own library, browse music and purchase new tracks as
they become available</p>
</body>
</html>

and I want to display the login page to entire application I placed login and password fields in main.gsp which is under

layouts/main.gsp
main.gsp is
<g:form name="loginForm"  url="[controller:'user',action:'login']">
Username:
 <g:textField name="login"
 value="${fieldValue(bean:loginCmd, field:'login')}">
 </g:textField><br>
Password:
 <g:passwordField name="password"></g:passwordField> 
 <br/>
 <input type="image"
 src="${createLinkTo(dir:'images', file:'login-button.gif')}"
 name="loginButton" id="loginButton" border="0"></input>
 </g:form>
<g:link controller="user" action="register" >Signup now</g:link>
 to start your own personal Music collection!

the above main.gsp contains the form which have log in text field and password text field and a button and Its also contains a link tag which is used for registration process when I run the application the application goes to the store controller default index page ,based on the default url mapping and this index page also contains the log in text field and password fields and log in button and it also contains registration link when I pressed log in button its working fine but when click the sign up link it do not going to the register page of the User controller even though the register.gsp and User controller contains the register action,but when I remove the "/"(controller:"store") code from url Mappings class then I executed the application the application showing the list of controllers I clicked on the Store controller its moving to its index page in that I clicked sign up link to register now the link working fine its going to the register gsp page of user controller but it was not working when I used above custom url mapping

You need to add the "default" mapping before your custom mapping,

"/$controller/$action?/$id?"{ constraints {
                // apply constraints here
            } }

This mapping:

  "/"(controller:"store")

only maps "/" so any request will go to store controller, because you don't have any other rule, so if you try to access to /user/login it will try to search for a user view in the store controller.

Try this..It works for me.( Grails 2.4.5 / java 8 and java 7)

class UrlMappings { 
   static mappings = {
    "/$controller/$action?/$id?"{
            // apply constraints here   
    }
    "/intialize/$id?" (controller: "xyz") {
            action = [GET:"abc"]
    }
    "/checksession" (controller: "xyz") {
            action = [GET:"mno"]
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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