grails 形式的动作不起作用

Action in grails form is not working

我正在尝试创建一个简单的帐户,当单击创建按钮时它应该执行操作 "create" 但我收到一条错误消息说
HTTP 状态 404 - 未找到“/WEB-INF/grails-app/views/users/create.gsp”。

这是我的 index.gsp

代码块
<!-- CREATEFORM -->
<div id="id02" class="modal">
  <g:form class="modal-content animate" controller="users" action="create">
    <div class="imgcontainer">
      <span onclick="document.getElementById('id02').style.display='none'" class="close" title="Close Modal">&times;</span>
        <div class="container" style="text-align:center">
            <b><h style="font-family: Arial, Helvetica, sans-serif; font-size: 30px">CREATE AN ACCOUNT</h></b><br/>
      <input type="text" placeholder="Enter Username" name="uname" required/>

      <input type="password" placeholder="Enter Password" name="psw" required/>

      <input type="text" placeholder="First Name" name="firstName" required/>

      <input type="text" placeholder="Last Name" name="lastName" required/>

      <input type="text" placeholder="Age" name="age" required/>
      <br/>
      <input type="date" placeholder="Birth Date" name="birthdate" required/>
      <br/>
      <input type="text" placeholder="Student Number" name="studno" required/><br/>

      <label>
        <input type="checkbox" checked="checked" name="remember"> Remember me</input>
      </label>
      <br/>
      <button type="submit" style="width: 100px; margin-right:10px;" >Create</input>
      <button type="button" onclick="document.getElementById('id02').style.display='none'" class="cancelbtn">Cancel</button>

    </div>
    </div>
  </g:form>
</div>

这是我的 usersController 代码块。

class usersController {    
    def index(){}
    def create()
    {
        new Users(userid:params.uname,password:params.psw).save()
        new UserInfo(studentno:params.studno,age:params.age,birth_date:params.birthdate,first_name:params.firstName,last_name:params.lastName,user_id:params.uname).save()
    }
}

这是错误

首先你用的是什么版本的grails?如果是最新的 (3.3.4) 那么这个 link 可能会有帮助。

简而言之:如果您没有明确指定要呈现的内容 - grails 会尝试找到视图来显示操作执行的结果:

grails-app/views/<controllerName>/<actionName>.gsp

你好像没有。

UPD

有几种方法可以在 grails 控制器中呈现动作的输出。 将显式内容定义为:

def myAction() {
    ...
    render "Hello World!"
}

这将导致 "white screen" 的左上角带有 "Hello world!" 文本。

另一种选择是在常规位置创建一个 gsp 视图,如 grails-app/views//.gsp - 如果没有指定 render[=37=,它将在操作执行后自动呈现] 方法调用(就像你给出的示例一样)

第三个选项是指定模型贴图:

def myAction() {
    ...
    [param1key: param1value, param2key: param2value, ...]
}

渲染 gsp 视图时将考虑模型。

请阅读我给 link 的文档。非常有用。

HTTP Status 404 - "/WEB-INF/grails-app/views/users/create.gsp" not found.

因为您正在使用创建操作保存数据,但保存数据后没有重定向选项或 create.gsp 可用。所以 grails 将首先查看是否有任何可用的重定向选项并尝试重定向 create.gsp

检查以下代码更改,将 user id 保存到 userInfo table 并重定向到索引页面

所以改变你的usersController

class usersController {
    def index() {}

    def create() {
        def user = new Users(userid: params.uname, password: params.psw).save()
        def userInfo = new UserInfo(studentno: params.studno, age: params.age, birth_date: params.birthdate, first_name: params.firstName, last_name: params.lastName, user_id: user.id).save()
        if (user && userInfo) {
            flash.message = "User created successfully"
            redirect action: 'index'
        } else {
            flash.message = "Problem in user creation"
            redirect action: 'index'
        }

    }
}

并将以下代码添加到您的 index.gsp

${flash.message}

重定向到页面的方法有很多种。您可以使用渲染、重定向、链接、转发等

请查看 grails documentation 了解更多信息

在 Grails for action(非 gsp)中,您需要向客户端呈现一些东西, 否则它会执行该动作中的所有内容,但是 returns 404 gsp not found 响应,如果没有渲染/重定向语句它认为该动作是 gsp 但它实际上不是 gsp,因此它响应 404.

您可以渲染一些数据,例如列表 / JSON 或如下所示的简单字符串。

def create() {
 //your business logic is here
 render "operation performed successfully"

}