【问题标题】:URL mappings not working properly in grailsURL 映射在 grails 中无法正常工作
【发布时间】:2014-08-24 05:19:41
【问题描述】:

我正在做一个应用程序,它是来自 the-definitive-guide-to-grails definitive-guide-to grails 的 gTunes

我使用自定义 url 重定向到存储控制器,即

UrlMappings.groovy
class UrlMappings {

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

所以当我启动应用程序时,它将使用默认存储控制器 我有用户域类以及具有注册和登录操作的 UserController 类

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

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

USerController 在 user/register.gsp 和 store/index.gsp 下有对应的视图页面

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>

我想向整个应用程序显示登录页面,我在 main.gsp 中放置了登录名和密码字段

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!

上面的 main.gsp 包含具有登录文本字段和密码文本字段以及一个按钮的表单,它还包含一个链接标签,当我运行应用程序时,该标签用于注册过程,应用程序转到存储控制器默认索引页面,基于默认 url 映射,此索引页面还包含登录文本字段和密码字段以及登录按钮,它还包含注册链接,当我按下登录按钮时它工作正常,但当单击标志时向上链接即使 register.gsp 和用户控制器包含注册操作,它也不会进入用户控制器的注册页面,但是当我从 url Mappings 类中删除“/”(控制器:“存储”)代码时,我执行应用程序显示控制器列表的应用程序我单击存储控制器它移动到它的索引页面我单击注册链接现在注册链接工作正常它转到用户控制器的注册 gsp 页面r 但是当我使用上面的自定义 url 映射时它不起作用

【问题讨论】:

    标签: grails


    【解决方案1】:

    您需要在自定义映射之前添加“默认”映射,

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

    这个映射:

      "/"(controller:"store")
    

    只映射“/”所以任何请求都会去存储控制器,因为你没有任何其他规则,所以如果你尝试访问 /user/login 它将尝试在商店中搜索用户视图控制器。

    【讨论】:

      【解决方案2】:

      试试这个..它对我有用。(Grails 2.4.5 / java 8 和 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"]
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多