gpt4 book ai didi

java - Thymeleaf 中的 View 和重定向有什么区别?

转载 作者:行者123 更新时间:2023-11-30 10:42:00 25 4
gpt4 key购买 nike

我知道这个问题对某些人来说可能听起来很愚蠢,但我是 Spring Boot 和 Thymeleaf 的新手。

假设我目前在 page1.html 上。用户单击某个按钮,该按钮会向我的 Controller MyController.java 生成一个 POST/GET 请求,该请求使用 @RequestMapping 将此请求映射到某个函数。

处理完请求后,我从 Controller 返回一个 View 名称,如 return "page2"

page1.html 的内容更改url 仍然保持不变http://localhost:8080/page1 而不是更改为 http://localhost:8080/page2

我知道为了转到不同的页面我必须使用 redirection 但为什么这对于 view 是不可能的?

有人能解释一下为什么会发生这种情况吗?我什么时候应该使用重定向或者什么时候应该使用 View ?

编辑 1:

这是我的 Controller

@RequestMapping (value = "edit", method = RequestMethod.POST, params="action=ADD")
{
public String saveUser(@ModelAttribute UserDto userDto) {
try {
User user = new User();
if(userDto.getId() != null) {
throw new UserExists();
}
user.setName(userDto.getName());
user.setEmail(userDto.getEmail());
System.err.println("Request Recieved");
userDao.save(user);
return "success";
} catch (Exception e) {
System.err.println("Error Saving User");
e.printStackTrace();
return "failure";
}
}

这是我的看法

<form id="manipulate-data-form"  th:object="${userobj}" method="post" th:action="@{/edit}">
<table id="manipulate" class="table table-bordered">
<tr>
<td>ID</td>
<td><input type="text" class="form-control" th:if="${userobj != null}" th:field="*{id}" ></input></td>
</tr>
<tr>
<td>NAME</td>
<td><input type="text" class="form-control" th:if="${userobj != null}" th:field="*{name}" ></input></td>
</tr>
<tr>
<td>EMAIL</td>
<td><input type="text" class="form-control" th:if="${userobj != null}" th:field="*{email}" ></input></td>
</tr>
<tr>
<td><input name="action" value="ADD" type="submit" class="btn btn-success" ></input></td>
<td>
<input name="action" value="DELETE" type="submit" class="btn btn-danger" ></input>
<input style="margin-left: 30px" name="action" value="UPDATE" type="submit" class="btn btn-primary" ></input>
</td>
</tr>
</table>
</form>

所以基本上单击按钮会调用 saveUser() Controller ,然后当我返回失败成功 View 时,URL 保持不变,但对应成功或失败的页面变化。

最佳答案

让我尝试回答您的一些问题:

The contents of the page1.html change but the url still remains the same like http://localhost:8080/page1 instead of changing to http://localhost:8080/page2

您正在访问类似 http://localhost:8080/page1 的 URL . /page1并不意味着以名称 page1.html 显示您的观点.它更多的是你的网址 @RequesteMapping注释以及应该为此请求执行哪个方法。

例如:

您正在访问网址:

http://localhost:8080/page1你的映射是:@RequestMapping("/page1")你想要返回 page1.html .所以你的观点是:return "page1";

现在您会看到 page1.html 的内容.您提供的按钮现在应该有一个链接到 http://localhost:8080/page2你的 Controller 应该有一个映射到它@RequestMapping("/page2") .在这种方法中,您可以返回您的 return "page2"您的网址将更改为 http://localhost:8080/page2 .

如果按钮的链接确实有另一个值而不是http://localhost:8080/page2但是你想显示这个特定的 URL /page2您需要重定向:return "redirect:/page2"在特定的方法@RequestMapping("/anyOtherMapping") .


Could someone explain me why does this happen and when should I use redirection or when should I use view?

我不是 100% 确定这是否正确,但我认为这只是为什么存在 View 和重定向的规则。如果您想站在特定的 URL 上并想要显示为此构建的 View ,那就去做吧。如果您不想显示相同的 URL 或想切换到另一个页面,只需使用重定向。

重定向还会再次调用正确映射到您的重定向 URL 并返回 View 的 Controller 。

一些使用重定向的例子如下:

  • 成功登录后,您可以重定向到用户主页。
  • 保存项目后(REST 方法),您可以重定向到该项目的特定 ID /item/1并返回包含该项目信息的 View 。

回答您的编辑问题:

尝试使用redirection用于更改网址。

@RequestMapping (value = "edit", method = RequestMethod.POST, params="action=ADD")
{
public String saveUser(@ModelAttribute UserDto userDto) {
try {
User user = new User();
if(userDto.getId() != null) {
throw new UserExists();
}
user.setName(userDto.getName());
user.setEmail(userDto.getEmail());
System.err.println("Request Recieved");
userDao.save(user);
return "redirect:/success";
} catch (Exception e) {
System.err.println("Error Saving User");
e.printStackTrace();
return "redirect:/failure";
}
}

@RequestMapping("/success")
public String successPage(){
return "success";
}

@RequestMapping("/failure")
public String failurePage(){
return "failure";
}

这应该更改 URL,因为您将从 /edit 进行重定向至 /failure/success页。如果需要,您还可以在重定向中提供参数。

关于java - Thymeleaf 中的 View 和重定向有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38299335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com