1

私はginライブラリを使用して単純なcrud webappを作成しています.paramをチェックするルート設定がありid、そのaddaadmin-employee-add.htmlをレンダリングする必要がある場合は、存在する場合はidを持つ従業員を返しますgetsadmin-employee-add.htmlのエラーメッセージのテンプレートをレンダリングしている場合404 not foundその中に漏れました。ここにスナップショットがありますここに画像の説明を入力

admin-employee-add.html

{{template "pageStart.html" .}}
<form class="form-horizontal admin-employee">
  <div class="row form-group">
    <label for="employeeNumber" class="col-lg-2 control-label text-right">Employee #</label>
    <div class="col-lg-4">
      <span>new</span>
    </div>
    <label for="status" class="col-lg-2 control-label text-right">Status</label>
    <div class="col-lg-4">
      <span>new</span>
    </div>
  </div>
  <div class="row form-group">
    <label for="firstName" class="col-lg-2 control-label text-right">Name</label>
    <div class="col-lg-2">
      <input type="text" id="firstName" name="firstName" class="form-control">
    </div>
    <div class="col-lg-2">
      <input type="text" id="lastName" name="lastName" class="form-control">
    </div>
  </div>
  <div class="row form-group">
    <label for="startDate" class="col-lg-2 control-label text-right">Start Date</label>
    <div class="col-lg-2">
      <input type="date" id="startDate" name="startDate" class="form-control">
    </div>
    <label for="pto" class="control-label col-lg-2 col-lg-offset-2 text-right">PTO</label>
    <div class="col-lg-3">
      <input type="number" id="pto" name="pto" class="form-control">
    </div>
    <div class="col-lg-1">
      days
    </div>
  </div>
  <div class="row form-group">
    <label for="position" class="col-lg-2 control-label text-right">Position</label>
    <div class="col-lg-2">
      <select name="position" id="position" class="form-control">
        <option value="CEO">CEO</option>
        <option value="CTO">CTO</option>
        <option value="COO">COO</option>
        <option value="WorkerBee">Worker Bee</option>
      </select>
    </div>
  </div>
  <div class="col-lg-3 col-lg-offset-8">
    <button type="submit" class="btn btn-lg admin-primary">Create</button>
  </div>
</form>

エラーを作成しているルート

r.GET("/employee/:id/", func(c *gin.Context) {
        id := c.Param("id")
        if id == "add" {
            c.HTML(http.StatusOK, "admin-employee-add.html", nil)
        }

        employee, ok := employees[id]

        if !ok {
            c.String(http.StatusNotFound, "404 not found", nil)
        } else {
            c.HTML(http.StatusOK, "admin-employee-edit.html", map[string]interface{}{
                "Employee": employee,
            })
        }

    })

ginがリダイレクトしようとしているためにエラーが発生しているようです/add->/add/しかし、私はすでに/add/ブラウザーでルートを使用しています。

ginのデバッグログ

[GIN-debug] GET    /login                    --> main.registerRoutes.func2 (3 handlers)
[GIN-debug] GET    /employee/:id/            --> main.registerRoutes.func3 (3 handlers)
[GIN-debug] GET    /employee/:id/vacation    --> main.registerRoutes.func4 (3 handlers)
[GIN-debug] GET    /admin/                   --> main.registerRoutes.func5 (4 handlers)
[GIN-debug] Listening and serving HTTP on :3000
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with
404
[GIN] 2016/05/01 - 14:12:13 | 404 |    1.101426ms | 127.0.0.1 |   GET     /employee/add/

ルートを変更してみまし/:idたが、エラーが表示されました。

redirecting request 301: /employee/add/ --> /employee/add
[GIN-debug] [WARNING] Headers were already written. Wanted to override status code 200 with
404

return注: このエラーは、.txtの末尾にa を追加することで簡単に解決できますif id == "add"。しかし、このパターンにより、コードがドライに見えなくなります。これはもっとhttprouter問題のようです。

4

1 に答える 1

0

returnあなたが言ったように、このエラーはの最後にa を追加することで簡単に解決できますif id == "add"

c.Stringc.HTML同じを使用しContext cます。内部的には、同じソケットに書き込みます (ファイル ポインターと考えることができます)。したがって、呼び出しc.Stringてから呼び出した場合、c.HTMLまたはその逆の場合、呼び出された順序で出力が追加されます。

/employee/addあなたのコードを見ると、それを行うためのhtmlページを表示したときに従業員を追加したいと思います。そして、/employee/<something-other-than-add-as-id>それを取得して表示します - この場合、従業員情報を編集するための html です。

if id == "add"ここでは、2 つのシナリオで生成された html が異なる (異なるテンプレートを使用している) ため、 の後に return を追加するのが理にかなっています。純粋に別のハンドラーを追加すること/employee/add/も別のオプションです。

于 2016-05-02T12:27:53.873 に答える