Spring MVC:如何重定向到有错误的页面? [英] Spring MVC: How to redirect to a page with error?

查看:161
本文介绍了Spring MVC:如何重定向到有错误的页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的Controller重定向到带有自定义错误消息的页面:

I am trying to make my Controller to redirect to a page with a custom error message:

    @RequestMapping(method=RequestMethod.POST)
    public String processSubmit(@Valid Voter voter, BindingResult result, HttpServletRequest request) {
        if (result.hasErrors()) {
            logger.info("RegisterController encountered form errors ");
            return "registerPage";
        }
        if (service.isVoterRegistered(voter.getVoterID())) {
          logger.info("VoterID exists");

          request.setAttribute("firstName", voter.getFirstName());
          request.setAttribute("lastName", voter.getLastName());
          request.setAttribute("ssn", voter.getSsn());
          return "forward:/question";

        }else {
          logger.info("RegisterController is redirecting because it voter info failed to authenticate");
          //TODO: should re-direct to register page with error

          return "redirect:registerPage";
        }
       }
}


  <!-- registerPage.jsp -->
    <div class="container">
        <h1>
            Voter Registration
        </h1>
        <div class="span-12 last">  
            <form:form modelAttribute="voter" method="post">
                <fieldset>      
                    <legend>Voter Fields</legend>
                    <p>
                        <form:label for="firstName" path="firstName" cssErrorClass="error">First Name : </form:label></br>
                        <form:input path="firstName" /><form:errors path="firstName"/>
                    </p>
                    <p> 
                        <form:label for="lastName" path="lastName" cssErrorClass="error">Last Name : </form:label> </br>
                        <form:input path="lastName" /> <form:errors path="lastName" />
                    </p>
                    <p> 
                        <form:label for="ssn" path="ssn" cssErrorClass="error">Social Security Number : </form:label> </br>
                        <form:input path="ssn" /> <form:errors path="ssn" />
                    </p>
                    <p> 
                        <input type="submit"/>
                    </p>
                </fieldset>
            </form:form>
        </div>
        <hr>    
    </div>

在重定向到register.jsp页面时,我希望页面显示错误消息,说明选民没有注册。我的问题是如何让Controller返回页面,好像表单有一个验证错误(即result.hasErrors()== true)。

On redirecting to register.jsp page, I want the page to display an error message, saying that the voter is not registered. My question is how to get Controller to return to a page as if the form had a validation error (i.e result.hasErrors() == true) .

提前致谢

推荐答案

您可以在jsp中添加以下部分 -

You can add the following section in your jsp--

<c:choose>
<c:when test="${not empty  errors}">
    <div class="error">
    <c:forEach items="${errors}" var="err">
        ${err.defaultMessage}
        <br/>
    </c:forEach>
    </div>
</c:when>
</c:choose>

这里 c 只不过是这个 -

Here c is nothing but this--

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

您还需要将错误传递到模型中并在控制器中的if块内查看方法 -

Also you need to pass the errors into the model and view like this inside the if block in your controller method--

model.addAttribute("errors",result.getFieldErrors());

错误 DIV中的课程只是我的自定义css显示为红色块 -

error class in the DIV is nothing but my custom css to display as a red block--

.error{
    color: red;
    border:2px solid red;
    padding:10px;

}

您还可以查看这个


希望我的回答对你有帮助..

You can also have a look at this
Hope my answer has helped you..

这篇关于Spring MVC:如何重定向到有错误的页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆