Today we will see how we can send error message from our controller. Here we set the error message in controller which is dynamic and view this on jsp with single tag.
So lets start this step by step :-
Step 1:- Create MVC Portlet
Just Create a simple MVC Portlet and paste this content in the controller.
DynamicSessionErrorPortlet.java
package com.liferay.dynamic.portlet; | |
import com.liferay.dynamic.constants.DynamicSessionErrorPortletKeys; | |
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet; | |
import com.liferay.portal.kernel.servlet.SessionErrors; | |
import java.io.IOException; | |
import javax.portlet.Portlet; | |
import javax.portlet.PortletException; | |
import javax.portlet.RenderRequest; | |
import javax.portlet.RenderResponse; | |
import org.osgi.service.component.annotations.Component; | |
/** | |
* @author adit2 | |
*/ | |
@Component( | |
immediate = true, | |
property = { | |
"com.liferay.portlet.display-category=category.Liferay Is Easy", | |
"com.liferay.portlet.header-portlet-css=/css/main.css", | |
"com.liferay.portlet.instanceable=true", | |
"javax.portlet.display-name=DynamicSessionError", | |
"javax.portlet.init-param.template-path=/", | |
"javax.portlet.init-param.view-template=/view.jsp", | |
"javax.portlet.name=" + DynamicSessionErrorPortletKeys.DYNAMICSESSIONERROR, | |
"javax.portlet.resource-bundle=content.Language", | |
"javax.portlet.security-role-ref=power-user,user" | |
}, | |
service = Portlet.class | |
) | |
public class DynamicSessionErrorPortlet extends MVCPortlet { | |
@Override | |
public void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws IOException, PortletException { | |
try { | |
throw new ArithmeticException("This is Arithmatic Exception"); | |
//throw new NullPointerException("This is Null pointer Exception"); | |
} catch (Exception e) { | |
System.out.println("Inside exception "+e.getMessage()); | |
SessionErrors.add(renderRequest, "dynamic-error-msg",e.getMessage()); | |
} | |
super.doView(renderRequest, renderResponse); | |
} | |
} |
Step 2:- Change your view.jsp
Now open your view.jsp and paste this:-
view.jsp
<%@page import="com.liferay.portal.kernel.portlet.LiferayPortletRequest"%> | |
<%@page import="com.liferay.portal.kernel.servlet.SessionErrors"%> | |
<%@ include file="/init.jsp" %> | |
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %> | |
<liferay-ui:error key="dynamic-error-msg"> | |
<liferay-ui:message key="dynamic.error.msg" arguments="<%= SessionErrors.get(liferayPortletRequest, "dynamic-error-msg") %>"/> | |
</liferay-ui:error> | |
<h3>This Portlet Show Dynamic Errors</h3> | |
Step 3:- Provide entry in Language.properties
Now open Language.properties file and add the entry
dynamic.error.msg={0}
Step 4:- Deploy and Check Output
Now deploy your portlet and add to page.
Now comment Line 40 and uncomment line 41 and redeploy
Related Post:-
No comments:
Post a Comment