Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 在SpringPortletMVC中设置模型错误的正确方法_Spring Mvc_Liferay_Portlet - Fatal编程技术网

Spring mvc 在SpringPortletMVC中设置模型错误的正确方法

Spring mvc 在SpringPortletMVC中设置模型错误的正确方法,spring-mvc,liferay,portlet,Spring Mvc,Liferay,Portlet,在我的MVC控制器中,我有一个带有签名的处理程序方法: public void myAction(ActionRequest request, ActionResponse response, Model model) {...} 在这个方法中,我检查一些提交的数据是否正确。如果无效,我想设置一个错误。目前我的做法很简单: model.addAttribute("operationStatus", "error"); model.addAttribute("operationMessage",

在我的MVC控制器中,我有一个带有签名的处理程序方法:

public void myAction(ActionRequest request, ActionResponse response, Model model) {...}
在这个方法中,我检查一些提交的数据是否正确。如果无效,我想设置一个错误。目前我的做法很简单:

model.addAttribute("operationStatus", "error");
model.addAttribute("operationMessage", "a lot of things went wrong");
在JSP视图中:

<c:if test="${requestScope.operationStatus == 'error'}">
    <div class="msg-error">${requestScope.operationMessage}</div>
</c:if>

${requestScope.operationMessage}
当然,在SpringPortletMVC中必须有更好的方法来处理错误。请注意,我需要在不同的位置显示错误消息,而不仅仅是在
标记中


那么我应该如何处理错误呢?

如果您的目标只是Liferay,那么您可以使用SessionErrors类来执行以下操作:

SessionErrors.add(actionRequest, "some-error");
然后,在JSP上有:

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<liferay-ui:error key="some-error" message="Your error message goes here!" />


您也可以使用“在例外情况下执行此操作”。看看我的答案。

看起来不错。但是我必须一个接一个地列出一堆
标记,即当我有20个可能的错误时,我必须在我的视图中列出20个jsp标记?是的!恐怕这是真的,而且据我所知,它周围并没有使用Liferay标记。唯一的其他方法是编写一些代码来检查会话错误并输出错误,但老实说,上面的方法是最好的。我刚刚检查过,
SessionErrors
有一个重载,它包含3个参数-第三个是
。这是什么?看看Liferay源代码,我猜value参数是一种指定错误消息的方法。正常的行为是使用键在属性文件中进行查找,以获得适当的消息。值可能会覆盖此消息。