留言板

SessionErrors always shows default Error Message

thumbnail
Ricardo Jorge Vandelay,修改在14 年前。

SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
I'm trying to use the SessionErrors class to handle validation messages in my configuration form.

However, when I add a custom error message my key is never shown and instead, the "you-have-entered-invalid-data" appears.

Here is how I'm adding:

SessionErrors.add(actionRequest, "insertTitleError");


and showing in the JSP:

<liferay-ui:error key="insertTitleError" message="error-please-insert-title" />


I have looked at the code in the error taglib and the "you-have-entered-invalid-data" message is shown when the key is null. Why is my session message disappearing?


	<c:when test="<%= key == null %>">
		<c:if test="<%= !SessionErrors.isEmpty(portletRequest) %>">
			<span class="portlet-msg-error">
			<liferay-ui:message key="you-have-entered-invalid-data" />
			</span>

			&lt;%= rowBreak %&gt;
		</c:if>
	</c:when>


Best Regards.
thumbnail
Ricardo J. Vandelay,修改在14 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
bump
thumbnail
Ricardo J. Vandelay,修改在14 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
bump

I spent a whole afternoon debugging this problem and I cannot figure out why this happens. Blergh!
thumbnail
Amos Fong,修改在14 年前。

RE: SessionErrors always shows default Error Message

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
Your code looks correct to me. Is SessionErrors always empty?

Does the user go directly to the jsp with that error tag after you set a SessionError?
thumbnail
Ricardo J. Vandelay,修改在14 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
When it enters end.jsp in the tag's HTML key and message are null and SessionErrors is empty. I've put some breakpoints in the clear methods in the SessionErrors class and they are only called after end.jsp is executed.

I'm using SessionErrors for my portlet's configuration area. Here is the full code for configuration.jsp and the configuration class.

init.jsp

&lt;%@ page contentType="text/html; charset=UTF-8" %&gt;

&lt;%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %&gt;
&lt;%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %&gt;

&lt;%@ page import="com.liferay.portal.kernel.util.ParamUtil" %&gt;
&lt;%@ page import="com.liferay.portlet.PortletPreferencesFactoryUtil" %&gt;
&lt;%@ page import="javax.portlet.PortletPreferences" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.Validator" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.HtmlUtil" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.util.PrefsParamUtil" %&gt;
&lt;%@ page import="com.liferay.portal.PortalException" %&gt;


&lt;%@ page import="com.liferay.portal.kernel.servlet.SessionErrors" %&gt;
&lt;%@ page import="com.liferay.portal.kernel.servlet.SessionMessages" %&gt; 

<portlet:defineobjects />
<liferay-theme:defineobjects />

&lt;%

PortletPreferences preferences = renderRequest.getPreferences();
String portletResource = ParamUtil.getString(request, "portletResource");

if (Validator.isNotNull(portletResource)) {
	preferences = PortletPreferencesFactoryUtil.getPortletSetup(request, portletResource);
}

%&gt;



configuration.jsp

&lt;%@ include file="/jsp/init.jsp" %&gt;
&lt;%
String name = PrefsParamUtil.getString(preferences, request, "name", "");

%&gt;

<liferay-ui:error key="baseErrorKey" message="error-message" />
<liferay-ui:success key="baseMessageKey" message="success-message" />

<form action="<liferay-portlet:actionURL portletConfiguration=" true">" method="post" id="<portlet:namespace />fm" name="<portlet:namespace />fm"&gt;
	<div>
		<label for="<portlet:namespace />-name"><liferay-ui:message key="insert-your-name" /></label>
    	<input name="<portlet:namespace />name" id="<portlet:namespace />-name" type="text" value="<%= HtmlUtil.escape(name) %>">
    </div>
    <input type="submit" value="<liferay-ui:message key=" save-information">" /&gt;
</form>



<liferay-ui:error> doesn't work
<liferay-ui:success> works well

class

package com.jump.base.configuration;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.PortletPreferences;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import com.liferay.portal.PortalException;
import com.liferay.portal.kernel.portlet.BaseConfigurationAction;
import com.liferay.portal.kernel.servlet.SessionErrors;
import com.liferay.portal.kernel.servlet.SessionMessages;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portlet.PortletPreferencesFactoryUtil;

public class Configuration extends BaseConfigurationAction {
    public void processAction(PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
        String portletResource = ParamUtil.getString(actionRequest, "portletResource");
        PortletPreferences preferences = PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);
        
        String name = ParamUtil.getString(actionRequest, "name");
        preferences.setValue("name", name);
        preferences.store();
        
        SessionErrors.add(actionRequest, "baseErrorKey");
        SessionMessages.add(actionRequest, "baseMessageKey");
    }

    public String render(PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
        return "/jsp/backend/configuration.jsp";
    }
}


Thank you.
thumbnail
Amos Fong,修改在14 年前。

RE: SessionErrors always shows default Error Message

Liferay Legend 帖子: 2047 加入日期: 08-10-7 最近的帖子
Strange...but SessionErrors can't be empty because this:

	<c:when test="<%= key == null %>">
		<c:if test="<%= !SessionErrors.isEmpty(portletRequest) %>">
			<div class="portlet-msg-error">
				<liferay-ui:message key="you-have-entered-invalid-data" />
			</div></c:if></c:when>


What version of Liferay are you using? Is this in a plugin?
thumbnail
Ricardo J. Vandelay,修改在14 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
I'm developing a Portlet using Liferay 5.2.3 with the Plugins SDK. I've also compiled and deployed the Portal and Ext (without any modifications).

You are correct but in the beginning of that JSP if I do a

System.out.println(SessionErrors.isEmpty(portletRequest));


Its returns true and if I print

SessionErrors.print(portletRequest);


it's empty.

I've also tried this portlet with a standard Liferay bundle (downloaded from the Website) and it's the same problem.

Thank you.
thumbnail
Sandeep Nair,修改在14 年前。

RE: SessionErrors always shows default Error Message

Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
Did you find a solution?

Regards,
Sandeep
thumbnail
Ricardo J. Vandelay,修改在14 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 17 加入日期: 10-2-24 最近的帖子
No I didn't. Still have the same issue.
thumbnail
Sandeep Nair,修改在14 年前。

RE: SessionErrors always shows default Error Message

Liferay Legend 帖子: 1744 加入日期: 08-11-6 最近的帖子
It only creates problem for configuration.jsp. For normal jsps it works fine.

Regards,
Sandeep
Babu Janarthanan,修改在14 年前。

RE: SessionErrors always shows default Error Message

Regular Member 帖子: 128 加入日期: 08-7-31 最近的帖子
Hi,
you can use the below mentioned code snippet.

SessionErrors.add(httpRequest, "baseErrorKey");

<c:if test="<%= SessionErrors.contains(request, "baseErrorKey") %>">

hope it works for you.
thumbnail
Loïc Dumont,修改在13 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 43 加入日期: 10-4-27 最近的帖子
I'm having the same problem (Liferay 5.2.3, plugin-sdk). liferay-ui:error is not working in a configuration jsp but works in other jsps...

Is it a bug?
thumbnail
Denis Signoretto,修改在13 年前。

RE: SessionErrors always shows default Error Message

Expert 帖子: 375 加入日期: 09-4-21 最近的帖子
http://issues.liferay.com/browse/LPS-12402

Did you find any workaround?
Arne Timmerman,修改在13 年前。

RE: SessionErrors always shows default Error Message

New Member 发布: 1 加入日期: 11-3-29 最近的帖子
This issue is still not resolved. If, for example, a required field in a configuration.jsp is not filled in and the error is added to the ActionRequest it is gone when the configuration.jsp is re-loaded (after submitting). This is a really annoying bug and affects all Liferay portlets. Did anyone found a solution?
thumbnail
Prakash Khanchandani,修改在13 年前。

RE: SessionErrors always shows default Error Message

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
I am using Liferay 6.0.

This is a really annoying bug and affects all Liferay portlets.


It does not affect all liferay portlets, coz I can see it working in blogs portlet, you can check out the source of com.liferay.portlet.blogs.action.ConfigurationActionImpl and html/portlets/blogs/configuration.jsp.

But somehow I can't figure out why its not working in my portlet!!emoticon

is there a possibility that we might be missing some kind of configuration in the liferay-portlet.xml?? emoticon

Currently I have handled it manually by setting error key attributes directly in request, the following is a skeleton of the jsp code:
<c:if test="${not empty customErrorKey}">
  <div class="portlet-msg-error">
    <liferay-ui:message key="my-custom-error-msg" />
  </div>
<c:if></c:if></c:if>


but this is a really crude method and hamper's code cleanliness, writing 4 lines of code and css instead of just one.

Also I have used a hook to customize the taglib/ui/error/end.jsp to not show the default error message
<liferay-ui:message key="you-have-entered-invalid-data" />


Anyone's help is much appreciated.

Thank You
olivier spieser,修改在12 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 13 加入日期: 11-6-6 最近的帖子
Hi,

I found a solution that worked for me.

1 - I overrided the file "porlet_message.jsp", I added some code to iterate over SessionErrors at the bottom of the file :

<liferay-ui:success key="<%= portletName + &quot;.doConfigure&quot; %>" message="you-have-successfully-updated-the-setup" />
<liferay-ui:success key="<%= portletName + &quot;.doEdit&quot; %>" message="you-have-successfully-updated-your-preferences" />
&lt;%
Iterator<string> itErrors = SessionErrors.iterator(renderRequest);
while (itErrors.hasNext()){
	String errorKey =  itErrors.next();
	if (StringUtils.startsWith(errorKey , portletName)
			&amp;&amp; StringUtils.endsWith(errorKey , ".doConfigure")) {
		
		String errorMessage = StringUtils.substringBetween(errorKey ,portletName+".", ".doConfigure");
		
		%&gt;
			<liferay-ui:error key="<%=errorKey%>" message="<%=errorMessage%>" />
		&lt;%
	}
}
%&gt;

<liferay-ui:error /></string>


2 -Then, in the configure action I simply add SessionErrors I want to display :

SessionErrors.add(actionRequest, portletConfig.getPortletName() + ".my-custom-error.doConfigure");


As you can see the sessionError key contains :
- the portlet Name
- The message
- The ".doConfigure" token

So, with these elements, you are sure the configure errors are only displayed in the configuration page of your portlet.
Harish Dhulipalla,修改在12 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 3 加入日期: 09-7-29 最近的帖子
In liferay 6.1, default error message not get displayed after adding a key "portletName + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE" to SessionMessages.
This gives facility to disable default error messages at portlet level on need basis.

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

Harish
thumbnail
Prakash Khanchandani,修改在12 年前。

RE: SessionErrors always shows default Error Message

Expert 帖子: 329 加入日期: 11-2-10 最近的帖子
Thanks Harish for the info.
Rob Hall,修改在11 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 47 加入日期: 11-11-30 最近的帖子
Is there any way to define this through the portlet.properties configuration?
thumbnail
Abdelrahman Mohamed El Ghanam,修改在11 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 41 加入日期: 10-9-30 最近的帖子
Does this have to be in a certain phase ? cause it doesnt work for me in the render phase .. emoticon
thumbnail
Atin Agarwal,修改在11 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 86 加入日期: 12-2-20 最近的帖子

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);


Not working in Custom portlets.
I have tried this in hooks. It Worked perfectly.
However when i have tried this in my Custom portlet's Process Action it didn't worked..I wonder Why??
Any help wud be appreciated.


Regards
thumbnail
Manish Kumar Jaiswal,修改在11 年前。

RE: SessionErrors always shows default Error Message

Regular Member 帖子: 153 加入日期: 08-11-25 最近的帖子
Hi Atin ,

Following didnt work for me as well in plugin portlet . Does any one have solved it ?

PortletConfig portletConfig = (PortletConfig)request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
SessionMessages.add(request, portletConfig.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);

Any help wud be appreciated.

Regards
Manish
thumbnail
Philipp Schwarz,修改在10 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 29 加入日期: 12-10-19 最近的帖子
Hey folks,

same here. I'm developing on Liferay 6.1.1 CE / emoticonTomcat 7.0.27 and followed the documentation here.

Here is my jsp:
<liferay-ui:error key="tos-required" message="error.required.terms-of-service" />
<liferay-ui:success key="form-processed" message="success.form-processed" />

<liferay-portlet:actionurl var="processFormURL" name="processForm" />
<aui:form action="<%= processFormURL.toString() %>">
	<aui:input name="termsofservice" type="checkbox" label="label.terms-of-service" checked="false" />				
	<aui:button value="button.submit" type="submit" />
</aui:form>


Here are two methods from my action class:
public void processForm(ActionRequest actionRequest, ActionResponse actionResponse) 
		throws IOException, PortletException{		
	
	if(ParamUtil.getBoolean(actionRequest, "termsofservice") == false)
		SessionErrors.add(actionRequest, "tos-required");
		
	else
		SessionMessages.add(actionRequest, "form-processed");
}


@Override
public void processAction(ActionRequest actionRequest,	ActionResponse actionResponse)
        throws IOException, PortletException {

	LiferayPortletConfig config = 
		(LiferayPortletConfig)actionRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

	String hideDefaultMessage = 
                config.getPortletName() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE;
	
	SessionMessages.add(actionRequest, hideDefaultMessage);
	
	super.processAction(actionRequest, actionResponse);
	
}


And this is what it looks like when the form is submitted without ticking the checkbox:


Strangely enough, the default success message doesn't get displayed, because when I do tick the checkbox, the result is the following:


It would be really helpful if someone from the liferay staff could shed some light onto this.

Thanks and happy coding,
Phil
Pradeep P,修改在10 年前。

RE: SessionErrors always shows default Error Message

New Member 帖子: 7 加入日期: 12-4-17 最近的帖子
Hi phil,


Try using the below code :

SessionMessages.add(request,
PortalUtil.getPortletId(actionRequest)
+ SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);
thumbnail
Harish Kumar,修改在10 年前。

RE: SessionErrors always shows default Error Message

Expert 帖子: 483 加入日期: 10-7-31 最近的帖子
Use the below code snippet to hide SessionErrors default error message


PortletConfig portletConfig = (PortletConfig)renderRequest.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
LiferayPortletConfig liferayPortletConfig = (LiferayPortletConfig) portletConfig;
SessionMessages.add(renderRequest, liferayPortletConfig.getPortletId() + SessionMessages.KEY_SUFFIX_HIDE_DEFAULT_ERROR_MESSAGE);


hope this helps!
thumbnail
Philipp Schwarz,修改在10 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 29 加入日期: 12-10-19 最近的帖子
Hey Pradeep and Harish,

big thanks for your proposals!

I will have to wait for the next release cycle before I can test this, but it's definitely worth a shot. If it works we should also make an effort to update the documentation which clearly states to use the portlet name instead of the portlet id.

Anyways, thanks again,
happy coding,
Phil
thumbnail
Philipp Schwarz,修改在9 年前。

RE: SessionErrors always shows default Error Message

Junior Member 帖子: 29 加入日期: 12-10-19 最近的帖子
Hey Harish,

I know it's been a while but I just wanted to let you know that your snippet did the job for me!

Thanks again,
Philipp