使用action =“。”提交表单。

时间:2014-06-03 23:11:58

标签: forms spring post

我正在看一个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";
    }
}

网址" / main / users / new"在此控制器中创建一个新的AccountForm对象并将其返回到view / 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>

本书指出使用动作=&#34;。&#34;将表单提交发布到/ main / users。我想知道行动的原因是什么?&#34;。&#34;发布到/ main / users是因为这个表单被称为&#39;通过控制器中的方法映射到/ main / users和&#34;。&#34;指定发布到此URL?这本书没有解释这一点。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

考虑行动是create的情况;你最终会得到/main/users/create. /main/users/.根据section 5.2.4 of RFC 3986/main/users/成为{{1}}。 (您也可以在那里找到解析相对URI的每个步骤的完整描述。)