首页 > 解决方案 > 前往铭文页面

问题描述

我在按 id 加载页面时遇到问题。我在 Spring 中创建简单的论坛,当我创建主题时,我得到了这样的页面 /topic/10(10 是 id 主题),接下来我想为这个主题添加题词,所以点击按钮后提交应该是 /inscription/topic/ 10(10 是 id 主题)。现在是问题所在,当我单击按钮时出现错误 405,但是当我拿起这一行并手写或复制地址并再次粘贴时,我得到了题词页面。问题出在哪里?它可以是百里香或春季请求?代码如下

@Controller
@RequestMapping("/inscription/")
public class InscriptionController {

private InscriptionService inscriptionService;

private TopicService topicService;

@Autowired
private InscriptionController(InscriptionService inscriptionService, TopicService topicService) {
    this.inscriptionService = inscriptionService;
    this.topicService = topicService;
}

@GetMapping("topic/{id}")
public String in2(@ModelAttribute("inscription") Inscription inscription, @PathVariable Long id, Model model) {
    Topic topic = topicService.findOne(id);
    model.addAttribute("inscription", inscription);
    return "inscription";
}

Thymeleaf - 当我按下按钮时,新消息应该转到题字/主题/id

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Create new topic</title>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
          crossorigin="anonymous">
</head>
<body>

<nav class="navbar navbar-light" style="background-color: #969bd9;">
    <span class="navbar-brand">Create new topic</span>
</nav>

<div class = "container">
    <div class = "row">
        <div class = "col-md6 col-md-offset-3">

            <form th:action="@{/inscription/topic/{id}(id = ${topic.id})}" th:object="${inscription}" method="post">
            <h5>
                <a th:href="@{/topic/{id}(id = topic.id)}"></a>
                <span th:text="${topic.title}"></span>
            </h5>

            <div class="col s10">
                <div class="row">
                    <div class="col s11">
                        Stworzony
                        <p th:text="${topic.createdAt} ? ${#calendars.format(topic.createdAt, 'HH:mm dd MMMM yyyy')}"></p>
                        <p th:utext="${#strings.replace(topic.text,T(java.lang.System).getProperty('line.separator'),'&lt;br /&gt;')}"></p>
                        <div class="divider"></div>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-success">New message</button>
                </div>
            </form>
        </div>
    </div>
</div>

</body>
</html>

标签: javaspringpostthymeleaf

解决方案


405 错误表示“方法不允许”。当您单击submit按钮时,您form将执行该POST方法,该方法在您的控制器中没有相应的映射。您应该将POST映射添加到您的控制器或将form方法更改为GET.


推荐阅读