How to create a new(original) struts path? - liferay

I have tried overriding existing StrutsPortletAction before with their existing struts path with success. However, I can't seem to do the same if I were to try creating my own struts action path.
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/portal/set_viewers/</struts-action-path>
<struts-action-impl>com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction</struts-action-impl>
</struts-action>
</hook>
The eclispe IDE gives me this error "/portal/set_viewers/" is not among possible values" and when I go ahead and deploy the built war anyways, tomcat errors as: com.liferay.portal.kernal.util.InstanceFactory can not access a member of class com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction with modifiers ""
Tried with struts-action-path as /blogs/set_viewers/ failed as well.
This is the .java i'm using. Very basic actually.
package com.mine.blogs.hook;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.struts.StrutsPortletAction;
public class BlogEntryViewerStrutsPortletAction implements StrutsPortletAction {
BlogEntryViewerStrutsPortletAction(){
super();
}
#Override
public void processAction(
PortletConfig arg0, ActionRequest arg1, ActionResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("process1");
}
#Override
public void processAction(
StrutsPortletAction arg0, PortletConfig arg1, ActionRequest arg2,
ActionResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("process2");
}
#Override
public String render(
PortletConfig arg0, RenderRequest arg1, RenderResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("render1");
return null;
}
#Override
public String render(
StrutsPortletAction arg0, PortletConfig arg1, RenderRequest arg2,
RenderResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("render2");
return null;
}
#Override
public void serveResource(
PortletConfig arg0, ResourceRequest arg1, ResourceResponse arg2)
throws Exception {
// TODO Auto-generated method stub
System.out.println("serve1");
}
#Override
public void serveResource(
StrutsPortletAction arg0, PortletConfig arg1, ResourceRequest arg2,
ResourceResponse arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("serve2");
}
}
And the corresponding liferay-hook.xml
<hook>
<custom-jsp-dir>/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/blogs_entry/set_viewers/</struts-action-path>
<struts-action-impl>com.mine.blogs.hook.BlogEntryViewerStrutsPortletAction</struts-action-impl>
</struts-action>
</hook>

Please try changing the struts action URL to something other than starting with "/portal". Liferay may be reserving "/portal" for portal level action paths.
For example,
<struts-action-path>/blogs_entry/set_viewers/</struts-action-path>

The error was because of the Constructor. I've removed it and it's now deploying properly

Related

Application scoped managed bean not initializing

I have an application scoped managed bean which I am trying to inject into a session filter to filter rules based on the map provided from application scoped bean .
The Application scoped beans purpose was to load application configuration from the database into the Map which can be accessed during the scope of the application.
#Named
#ApplicationScoped
public class ApplicationConfig implements Serializable {
private Map<String,String> accessRule;
private static final long serialVersionUID = -7984677603595580195L;
#PostConstruct
public void init() throws SQLException, Exception {
System.out.println("ApplicationContainer INIT");
accessRule.put("A", "A");
}
public Map<String,String> getAccessRule() {
return accessRule;
}
public void setAccessRule(Map<String,String> accessRule) {
this.accessRule = accessRule;
}
}
I have tried #PostConstruct and also tried using the constructor too but the bean is not being called.This how the Named bean is being injected
#WebFilter(urlPatterns = { "/*" })
public class ApplicationFilter implements Filter {
private static final String FACES_RESOURCES = "javax.faces.resource";
private static final Logger log = Logger.getLogger(ApplicationFilter.class.getName());
private boolean disableFilter;
private String errorPage;
private String indexPage;
#Inject
public ApplicationConfig applicationConfig;
private List<String> ignoredResources = new ArrayList<>();
#Override
public void destroy() {
// TODO Auto-generated method stub
}
#Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
System.out.println(applicationConfig.getAccessRule());
arg2.doFilter(arg0, arg1);
}
#Override
public void init(FilterConfig arg0) throws ServletException {
}
}
I have used #Named / #Inject and still doesn't work. I want to use a application scoped bean that takes details from DB and used it in a WebFilter. Kindly help

How to get default page url in liferay?

I set default page URL in portal-ext.properties.
default.landing.page.path=/group/guest/home
Then create portlet with Controller :
public class UserPortrait extends MVCPortlet {
#Override
public void render(RenderRequest renderRequest, RenderResponse renderResponse) {
}
#Override
public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException, PortletException {
super.processAction(actionRequest, actionResponse);
}
}
Now , how to get landing page URL in processAction method.
Simply, use com.liferay.portal.kernel.util.PropsUtil.get(String propertyName) to get any property from portal-ext.properties file.

Get keyword of the search with struts Liferay

I want get the keywords of the search with the portlet of Search. I saw that in the url appear: "struts_action=/search/seach" then I looked for in strut-config.xml and find:
action path="/search/search" forward="portlet.search.search"
I am trying to do a strut but I don't know very much about struts and it doesn't work. This is the code of 'liferay-hook.xml':
<portal-properties>portal.properties</portal-properties>
<struts-action>
<struts-action-path>/search/search</struts-action-path>
<struts-action-impl>com.segmentationProject.searchAction.struts.SearchAction</struts-action-impl>
</struts-action>
portal.properties:
auth.public.paths=/search/search
SearchAction.java:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liferay.portal.kernel.struts.BaseStrutsAction;
public class SearchAction extends BaseStrutsPortletAction {
#Override
public void processAction(StrutsPortletAction originalStrutsPortletAction, PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {
System.out.println("inside the process ");
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
#Override
public String render(StrutsPortletAction originalStrutsPortletAction,PortletConfig portletConfig, RenderRequest renderRequest, RenderResponse renderResponse) throws Exception {
// TODO Auto-generated method stub
System.out.println("inside the render");
return super.render(portletConfig, renderRequest, renderResponse);
}
}
Any idea about I missing or do wrong? I only want get the keywords and then do the search normally.
Thanks!
I think that this one will do the job :
String keywords = ParamUtil.getString(actionRequest, "keywords");
ParamUtil use the portlet namespace to retrieve parameters.
I think you must use originalStrutsPortletAction instead of super.
super.processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}
just replace super by originalStrutsPortletAction
originalStrutsPortletAction .processAction(originalStrutsPortletAction, portletConfig, actionRequest, actionResponse);
}

Custom Viewhandler for JBoss 7.1

I want to implement a custom ViewHandler. Currently I'm only forwarding all calls to the default Viewhandler, but if I enable this ViewHandler in my faces-config.xml, the preRenderView event type (and maybe other functionality) is broken. Does anybody no what I'm doing wrong?
I'm using JBoss AS 7.1.1.
Thanks.
public class ReverseProxyViewHandler extends ViewHandler {
ViewHandler defaultHandler;
public ReverseProxyViewHandler(ViewHandler defaultHandler) {
this.defaultHandler = defaultHandler;
}
#Override
public Locale calculateLocale(FacesContext context) {
return defaultHandler.calculateLocale(context);
}
#Override
public String calculateRenderKitId(FacesContext context) {
return defaultHandler.calculateRenderKitId(context);
}
#Override
public UIViewRoot createView(FacesContext context, String viewId) {
return defaultHandler.createView(context, viewId);
}
#Override
public String getActionURL(FacesContext context, String path) {
return defaultHandler.getActionURL(context, path);
}
#Override
public String getResourceURL(FacesContext context, String path) {
return defaultHandler.getResourceURL(context, path);
}
#Override
public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException, FacesException {
defaultHandler.renderView(context, viewToRender);
}
#Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
return defaultHandler.restoreView(context, viewId);
}
#Override
public void writeState(FacesContext context) throws IOException {
defaultHandler.writeState(context);
}
}
I had the same error, but by extending the ViewHandlerWrapper instead of the the ViewHandler I got it to work.
Here is an example of my CustomViewHandler (not a complete example, but was built within our app as a proof of concept) :
import javax.faces.application.ViewExpiredException;
import javax.faces.application.ViewHandler;
import javax.faces.application.ViewHandlerWrapper;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import org.apache.log4j.Logger;
public class CustomViewHandler extends ViewHandlerWrapper {
private ViewHandler wrapped;
private static Logger LOGGER = Logger.getLogger(CustomViewHandler.class);
public CustomViewHandler(ViewHandler wrapped) {
LOGGER.info("CustomViewHandler.CustomViewHandler():wrapped View Handler:"+wrapped.getClass());
this.wrapped = wrapped;
}
#Override
public UIViewRoot restoreView(FacesContext context, String viewId) {
UIViewRoot root;
try {
LOGGER.info("restoring view : " + viewId);
root = wrapped.restoreView(context, viewId);
} catch (ViewExpiredException e) {
LOGGER.error("View Expired : " + e.getMessage() + " -> recreating");
root = wrapped.createView(context, viewId);
}
return root;
}
#Override
public ViewHandler getWrapped() {
return wrapped;
}
}
If you want a more complete and functional custom view handler, look at the OmniFaces RestorableViewHandler. Source code is available on that page.
From Java Doc
javax.faces.application.ViewHandlerWrapper
Provides a simple implementation of ViewHandler that can be subclassed by developers >wishing to provide specialized behavior to an existing ViewHandler instance. The default >implementation of all methods is to call through to the wrapped ViewHandler.
Usage: extend this class and override getWrapped to return the instance we are wrapping.

Liferay Hook calling extension method

I have new action in a liferay hook which ideally will be calling methods created in an extension. But at run time when executing the action, it throws the exception
java.lang.ClassNotFoundException
for the methods created in the extension.
Has anybody created similar action in liferay hook? if so, what was the solution for this problem if encountered?
Here is my code:
public class ExampleStrutsAction extends BaseStrutsAction {
public String execute( HttpServletRequest request, HttpServletResponse response) throws Exception {
String name = ParamUtil.get(request, "name", "World");
ThemeDisplay themeDisplay= (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
BSCDynamicDataListLocalServiceUtil.cloneDynamicDataListPageInSuborganization(the‌​meDisplay, name);
return "/portal/sample.jsp";
}
}
Try below code,
hook.xml
<hook>
<portal-properties>portal.properties</portal-properties>
<language-properties>
content/Language.properties
</language-properties>
<custom-jsp-dir>/META-INF/custom_jsps</custom-jsp-dir>
<struts-action>
<struts-action-path>/my_account/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
<struts-action>
<struts-action-path>/users_admin/edit_user</struts-action-path>
<struts-action-impl>com.test.hook.action.EditUserAction</struts-action-impl>
</struts-action>
</hook>
EditUserAction class
public class EditUserAction extends BaseStrutsPortletAction
{
#Override
public void processAction(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig,
final ActionRequest actionRequest, final ActionResponse actionResponse) throws Exception
{
// add your custom code
originalStrutsPortletAction.processAction(portletConfig, actionRequest, actionResponse);
}
#Override
public String render(final StrutsPortletAction originalStrutsPortletAction, final PortletConfig portletConfig, final RenderRequest renderRequest,
final RenderResponse renderResponse) throws Exception
{
//add your custom code
return originalStrutsPortletAction.render(portletConfig, renderRequest, renderResponse);
}
}
I have created above hook for EditUserAction in Control Panel
Hope it helps you!!

Resources