如何通过@GetMapping正确访问URL?

时间:2019-01-15 10:05:34

标签: java spring thymeleaf

我目前正在使用nginx反向代理在Docker部署的Spring应用程序上工作,并希望通过@GetMapping@PostMapping访问子域。

什么是正确的访问方式,例如/entity/add子域?

这是代码错误,还是我的服务器配置错误?要正确检查此问题,还需要其他什么方法吗?我会很乐意添加它。

我已经查看了官方文档,指南,其他StackOverflow帖子等,但是似乎都没有作用。

  

Controller.java:

public class EntityController {
private Repository repository;

@ModelAttribute("entity")
public Entity newEntity() {
    return new Entity();
}

// Overview.
@GetMapping("/entity")
public String index(Model model) {
    final Iterable<Entity> all = repository.findAll();

    model.addAttribute("all", all);

    return "index";
}

// New entity.
@GetMapping("/entity/add")
public String loadAddPage(Model model) {
    model.addAttribute("entity", new Entity());

    return "add";
}

@PostMapping("/entity/add")
public String submitEntity(@Valid @ModelAttribute Entity entity, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "add";
    }

    repository.save(entity);

    return "redirect:/index";
}
  

index.html:

<!DOCTYPE html>
    <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Overview</title>
<meta charset="UTF-8" />
</head>
<body>
<div class="container">
    <h2>Entities</h2>

    <form action="/entity/add" method="get">
        <input type="submit" value="New Entity"/>
    </form>

    <div class="content" th:each="entity: ${all}">
        <h2 th:text="${entity.name}">Name</h2>

        <form th:href="@{/entity/details?id={entityId}(entityId=${entity.id})}">
            <input type="submit" value="Edit"/>
        </form>
        <!--<button><a th:href="@{/entity/update?entityId={entityId}(entityId=${entity.id})}"></a></button>-->
    </div>
</div>
</body>
</html>
  

add.hmtl:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<title>New entity</title>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
</head>
<body>
<div class="container">
    <form action="/entity/add" method="post" th:object="${entity}">
        <table>
            <tr>
                <td><label for="name">Name</label></td>
                <td><input id="name" type="text" th:text="Name" th:field="*{name}" /></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>

我希望单击/entity/add中的index.html表单可以正确地将我链接到entity/add,但是它只会显示404错误。与其他经过测试的子域相同。

编辑01:标题已更新。 (访问子域->访问URL)

2 个答案:

答案 0 :(得分:0)

我理解您的问题,因为您需要使用POST方法将表单提交到此操作网址“ / entity / add”。考虑使用Thymeleaf视图模板来呈现服务器端操作。检查以下内容

  1. 将HTML操作属性更改为th:action属性。
  2. 提供“提交操作”按钮以提交表单。

检查此网址以获取更多信息:https://spring.io/guides/gs/handling-form-submission/

免责声明:这是我对您的问题的评论。由于我没有声誉,因此我将其发布为答案。

答案 1 :(得分:0)

我想你是用百里香做模板的。

在application.properties文件中添加以下内容:

MyObject

在控制器中:

spring.application.name: my-app-context-path

视图中:

    @GetMapping({"/", "/index"})
     public String defaultPage(final Model model, final Locale locale) throws MyException {
            addGameInfo(model);
            return "index";
        }
 @GetMapping({"/match/{gameId}"}){
    --------
    ------
}

您应该结帐Thymeleaf Standard URL Syntax

因此您的网址将解析为

1。http://localhost:2223/my-app-context-path/

2。http://localhost:2223/my-app-context-path/match/2944107874

这取决于您的上下文路径,如果不提供上下文路径,则可以直接访问映射。