【问题标题】:Submit form with action="."使用 action="." 提交表单
【发布时间】:2014-06-03 23:11:57
【问题描述】:

我正在查看 Spring In Practice 中的一个示例。我有以下控制器:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/users")
public class AccountController {
    private static final Logger LOG = LoggerFactory.getLogger(AccountController.class);

    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String getRegistrationForm(Model model) {
      model.addAttribute("account", new AccountForm());
      return "users/registrationForm";
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public String postRegistrationForm(AccountForm form) {
        LOG.info("Created registration: {}", form);
        return "redirect:/users/registration_ok.html";
    }
}

此控制器中的 URL“/main/users/new”创建一个新的 AccountForm 对象并将其返回到视图 /main/users/registrationForm。这是那个jsp:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1  /DTD/xhtml1-strict.dtd">

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>New user registration</title>
</head>
<body>
          <h1>New user registration</h1>

          <form:form cssClass="main" action="." modelAttribute="account">
          <p>All fields are required.</p>

          .... Form fields here ....

    </form:form>
</body>
</html>

这本书指示使用 action="."将表单提交发布到 /main/users。我想知道原因 action="."发布到 /main/users 是因为此表单被映射到 /main/users 和“.”的控制器中的方法“调用”。指定发布到此 URL?这本书没有解释。提前谢谢你。

【问题讨论】:

    标签: forms spring post


    【解决方案1】:

    . 是一个相对 URI,与任何其他 URI 一样;它指向当前的“目录”。在/main/users/new 中,就是/main/users/.. 将是 /main/

    考虑动作是create的情况;你最终会得到/main/users/create. 变成/main/users/. 根据section 5.2.4 of RFC 3986,它变成/main/users/。 (您也可以在此处找到解析相对 URI 的每个步骤的完整说明。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-01
      • 1970-01-01
      相关资源
      最近更新 更多