有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用重定向时,视图中不显示java Spring MVC消息

我正在编写一个可以添加新用户的Spring应用程序。输入的唯一数据是用户名和密码。两个用户不能有相同的用户名,我会检查这一点(这是有效的)

如果用户添加成功,我将返回主页并显示消息“New user added”(新用户已添加)。如果它们没有成功添加,我想停留在当前页面并显示消息“用户名已存在”。目前,尝试失败的情况仍然有效。如果尝试成功,导航部分会工作,但不会显示消息

以下是相关代码:

UserCredentialsController。爪哇

@Controller
public class UserCredentialsController {

    //Omitted some methods above



@RequestMapping(value = "/savenewuser", method = RequestMethod.POST)
public String saveNewUser(@Valid @ModelAttribute("newuser") NewUserValidation newUserValidation, BindingResult result, Model model)
{
 if(result.hasErrors())
 {

     //Return to form with errors displayed (messages in NewUserValidation class)
     return "newuserform";
 }
    else
 {

     //Add new user to database then redirect to main page.
     User user = new User();
     user.setUsername(newUserValidation.getUsername());
     user.setPassword(newUserValidation.getPassword());
     if(userService.addUser(user))
     {
         model.addAttribute("message", "New user added");
         return "redirect:/";
     }
     else
     {
         model.addAttribute("message", "Username already exists");
         return "newuserform";
     }
 }
}

 //Omitted some methods below.

newuserform。jsp

<%@ taglib prefix='form' uri='http://www.springframework.org/tags/form'%>
<!DOCTYPE html>
<html>
    <head>
        <title>Add New User</title>
        <link rel="stylesheet" type="text/css" href="/resources/css/styles.css" />
    </head>
    <body>
        <div class="message">
            <c:if test="${message!=null}">${message}</c:if>
        </div>
        <form:form method="post" action="savenewuser" modelAttribute="newuser">
            <table class="no-border">
                <tr>
                    <td colspan="2"><h1>Add New User</h1></td>
                </tr>
                <tr class="no-border">
                    <td class="no-border">Username</td>
                    <td class="no-border"><form:input type="text" path="username" /></td>
                </tr>
                <tr class="no-border">
                    <td class="no-border">Password</td>
                    <td class="no-border"><form:input type="password" path="password" /></td>
                </tr>
                <tr>
                    <td colspan="2"><input type="submit" value="Add New User" /><br />
                    <form:errors path="username" /><br />
                    <form:errors path="password" /></td>
                </tr>
            </table>
        </form:form>
    </body>
</html>

主页。jsp

<%@ taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c' %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<!DOCTYPE html>
<html>
    <head>
        <title>Main Page</title>
        <link rel="stylesheet" type="text/css" href="/resources/css/styles.css" />
        <script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
        <script type="text/javascript" src="/resources/javascript/animation.js"></script>
    </head>
    <body>
    <div class="message"><c:if test="${message!=null}">${message}</c:if></div>
    <c:if test="${myList==null}">myList not available.</c:if><br /> 
        <table>
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Gender</th>
                <th>Random Answer</th>
            </tr>
            <c:forEach var="entry" items="${myList}">
                <tr>
                    <td>${entry.firstName}</td>
                    <td>${entry.lastName}</td>
                    <td>${entry.gender}</td>
                    <td>${entry.randomAnswer}</td>
                </tr>
            </c:forEach>
        </table>
        <a href="/addincident">Add New Person</a> &nbsp; <a href="/addnewuser">Add New User</a> &nbsp; <a href="/login">Log in</a>
    </body>
</html>

正如您在我的控制器中看到的,在内部if-else语句的else块中,如果addUser()方法返回true(即它成功),那么我将相关消息添加到模型中,然后重定向到主页面。否则,我会添加不同的消息并重新加载相同的页面

如您所见,这两个路径之间的一个关键区别是,如果addUser()返回true,那么我在return语句的字符串中添加了“redirect”。我这样做的原因是因为在主页上。jsp我正在将另一个表中的一些数据加载到页面上。简单地返回“mainpage”意味着数据不会加载,但会显示我想要显示的消息。使用我在上面控制器中显示的方法,显示数据,但不显示消息。有趣的是,当我使用重定向时,我想显示的消息在URL中显示为一个参数,但当我不使用重定向时,这不会发生


共 (1) 个答案

  1. # 1 楼答案

    如果要在重定向后显示消息,请使用^{}

    下面是一个用法示例

    @RequestMapping("/foo")
    public String foo(RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", "User added");
        return "redirect:/";
    }
    

    并在JSP中像普通模型变量一样访问它:

    <c:if test="${not empty message}">
        <c:out value="${message}" />
    </c:if>
    

    将消息添加到模型时,重定向后该消息将不可用