In the method processAction(ActionRequest request, ActionResponse response), I insert a record into database and get the ID and then
I want to redirect to the view page of this record. So I need to create a RenderURL with a parameter value for that ID.
ActionResponse doesn't provide method to create a renderURL. Some codes in Liferay do similar things like:
create renderURL before accessing the actionURL
pass the renderURL as a parameter in the actionURL
However, at that time, I don't know the value of ID.
Other codes also use new PortletURLImpl() directly. My portlet cannot see that class.
Other codes also use new PortletURLImpl() directly. My portlet cannot see that class.
Because this class is in portal-impl.jar and also it is not recommended to use classes from this jar. Starting from Liferay 6.1, you won't be able to build your portlet from plugins-sdk if you classes point to portal-impl.jar.
Now to answer your question:
Any jsp is rendered by the render method or doView method (if using liferay's MVCPortlet) and this method would be called as part of the normal life-cycle of portlets.
Here are the steps you would need to take:
set a render parameter (using response.setRenderParameter() method) in your `processAction' method at the last which would be available in your render method, as follows:
actionResponse.setRenderParameter("myID", 1201);
Just for info: After using setRenderParameter you cannot use sendRedirect method
fetch this "myID" in your render method as you fetch any other request parameter:
//assuming your ID is a long
long myUserName = ParamUtil.getLong(renderRequest, "myID");
or
String strMyID = renderRequest.getParameter("myID");
long myID = Long.parseLong(strMyID);
After this, just use
include(renderPage, renderRequest, renderResponse);
were renderPage is nothing but a string containing the path to your jsp within docroot like /html/yourportlet/view.jsp
Just as an afterthought:
If you are using a Liferay IDE, then you can try creating a simple portlet project with MVCPortlet and then look at the generated portlet.xml's <init-param>
So basically you need to pass information from action-phase to render-phase, the development guide is a good place for explaining this in detail.
That's it.
Hope this helps.
Let me know if you have any confusion regarding this.
In action phase do the following:
ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute (WebKeys.THEME_DISPLAY);
PortletURL url = PortletURLFactoryUtil.create(request, this.getPortletName(), themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
For example, if you want to redirect to the login page and back, you can do the following:
response.sendRedirect("/c/portal/login?redirect=" + HttpUtil.encodeURL(url.toString()));
Definitely you can add or copy the parameters as required.
Instead of creating the renderURL you can include the view page include(viewTemplate,actionRequest,actionResponse). Or if you want to sent any parameter any want's to get it in doView then use actionResponse.setParameter(name,value) method
I create a RenderURL with a place holder as parameter value, like this:
<portlet:renderURL var="redirect">
<portlet:param name="ID" value="__ID__" />
</portlet:renderURL>`
In processAction:
String redirect = redirectParam.replace("__ID__", "123213");
actionResponse.sendRedirect(redirect) ;
Related
I would like to know the different kind of redirections in the portets and how to realize them. How to go from one view (jsp file) to another in the same portlet, how to go from one page to another from a portlet. It is for the purpose of making a form so I would like to do this in a processAction.
Your answer is not clear.
However, in Liferay, you can route your application using urls.
For example, you can create a portlet action url on your own.
You can try something like this:
HttpServletRequest request = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(actionRequest));
PortletURL oPortletURL = PortletURLFactoryUtil.create(request, <portletName> , <plid>, <portletPhase>);
oPortletURL.setParameter("myParameter1", "parameter1");
String portletURL = oPortletURL.toString();
Where:
<portletName> is the javax.portlet.name of your portlet
<plid> is the ID of a page where is located an instance of your <portletName> portlet (try to check Layout and LayoutFriendlyUrl tables)
<portletPhase> is the phase you need, for example PortletRequest.ACTION_PHASE
Finally, you can use setParameter() method to pass parameters in query string.
On the jsp for a single portlet: If you use Liferay's MVCPortlet, you can just add the mvcPath parameter with the name of the JSP, to make sure your portlet renders with this JSP. Nothing else.
Pages have a URL. If you want to redirect to another page, you'll have to configure the page's URL somewhere. However, that's rarely done unless you're implementing a Navigation portlet. Your question sounds more like you have a multi-page portlet.
Now I have one website. I want to provide user one link with one parameter. Then he can access the website and query something according the parameter. I want to the link is "http://10.182.20.86:8080/webproject/main.xhtml/scenarioid=1234" etc.
Now I use rest to pass the parameter to Java. But I can't get instance from FacesContext. It always returns null, when I use FacesContext.getCurrentInstance() in Java. Do you know how to implement the function by link with parameter in JSF? Thanks.
I have solved it by create one class and it extends HttpServlet. I get the parameter by request and get bean attribute by HttpServletRequest getSession. Now it works. Thanks, Balus.
I am working on a project using Liferay 6.2 on JBoss ES 6.2. I need to be able to create a action URL inside an action method. The action method is looking up some data, building a JSONArray, and then setting an attribute equal to the resulting JSON string. Part of that JSON data needs an action url to another action within the same portlet.
The problem I am running into is that the generated URL seems to force any parameters I set include two underscores to the parameter name.
For instance:
PortletURL actionUrl = PortletURLFactoryUtil.create(actionRequest, portletId, plid, PortletRequest.ACTION_PHASE);
actionUrl.setPortletMode(LiferayPortletMode.VIEW);
actionUrl.setWindowState(WindowState.NORMAL);
actionUrl.setParameter("guid", guid);
actionUrl.setParameter("javax.portlet.action", "myAction");
Ends up generating something like:
http://localhost:8000/group/mySite/myPortlet?p_auth=fsdweD2&p_p_id=p_p_lifecycle=1&p_p_state=normal&p_p_mode=view&__guid=1234567890&__javax.portlet.action=myAction
Notice the __guid and __javax.portlet.action. As a result, the portlet ends up running the doView() instead of myAction().
I have also tried to create a friendly url to solve the issue, but then I run into the issue of how to generate the friendly url with the proper site context AND the required p_auth value.
URL generated doesn't have portlet ID set properly as I see p_p_id is empty in URL. Please check if you are passing correct portlet ID.
Our project uses a Web Application developed using JSF, primefaces, xhtml. Currently user can login and navigate through pages sequentially by clicking on 'action' links, like:-
index.xhtml --> login.xhtml --> classes.xhtml --> students.xhtml --> student_info.xhtml
i.e. first login --> shows the list of classes --> user selects a class --> shows the list of students in that class --> user selects a student --> shows the student info.
Each of the pages has its own 'backing bean' classes. They are instantiated as and when the user clicks through the pages.
Also, user can navigate back via certain links on each page-- say, from 'student_info' page, he/she can go back to the 'students' page.
Now requirement is: user can directly go to an inner page, say, student_info page by typing an 'url' with additional parameters, ?user=alice,?passwd=xyz, ?class=5, ?studentRollNo=15.
Also, the user should still be able to navigate back to other pages (i.e. once the page is opened, their should be no behavior difference whether the user navigated normally to student_info page or, whether he directly provided url with parameters).
My questions are:-
How to read url parameters in JSF?
Which page (or backing bean) should handle the parameters? Should it be done centrally, or, in each page (backing bean) ?
In case each page handles its relevant parameters only -- is there way to redirect remaining parameters to the next page ?
What are the best practices used in such implementations?
Note:
Actual web application is much more complex, tried to provide a simpler picture which pinpoints my problem.
new to JSF, Web App etc. Don't know if there are some JSF terminologies to describe above issues.
you can pass it by url request, and each BackingBean handle it
ex:
mypage.xhtml?myparam=test
and inject the HttpServletRequest in your BackingBean (if you are using CDI)
#Inject
HttpServletRequest request;
and get the param
#PostConstruct
public void init() {
String myparam = request.getParameter("myParam");
}
for redirect to other page you can use
public String redirect() {
return "otherPage.xhtml?faces-redirect=true&otherParam=test";
}
I am trying to make my app "Bookmarkable", and i am using view parameters to achieve it.
And i think i still do not get the right way to do it right in JSF, even after reading this, and many others.
My problem is that the get parameters get lost after any non-ajax postback, i mean, the parameter value is still set in the bean and the app works correctly, but it gets removed from the URL making the URL invalid.
For instance, having an URL like http://company.com/users?id=4, as soon as that page executes a non-ajax postback (for uploading data, for instance) the URL becomes just http://company.com/users. The app continues to work correctly, but the link is not any more "Bookmarkable".
Is there any way to prevent the non-ajax postbacks removing the viewParams from the URL?
My use case is to be able to bookmark a page to EDIT an object, and there i need to be able to upload data (if not i would not use non-ajax postbacks). I know i would not need any postback if i would want to bookmark the page to only VIEW the data of the object, but that is not my case.
I could also do a redirect to the same page with the same params, and let the app to recreate the view scoped bean, but then i really do not see any benefit over request scoped beans...
Any suggestion is very appreciated.
This behaviour is "by design". The <h:form> generates a HTML <form> element with an action URL without any view parameters. The synchronous POST request just submits to exactly that URL which thus get reflected as-is in browser's address bar. If you intend to keep the view parameters in the URL, while using ajax is not an option, then you basically need to create a custom ViewHandler which has the getActionURL() overridden to include the view parameters. This method is used by <h:form> to generate the action URL.
public String getActionURL(FacesContext context, String viewId) {
String originalActionURL = super.getActionURL(context, viewId);
String newActionURL = includeViewParamsIfNecessary(context, originalActionURL);
return newActionURL;
}
Or, as you're based on the comments already using OmniFaces, you could also use its <o:form> component which basically extends the <h:form> with the includeViewParams attribute which works much like the same as in <h:link> and <h:button>.
<o:form includeViewParams="true">
...
</o:form>
This way all <f:viewParam> values will end up in the form action URL.
See also:
Handling view parameters in JSF after post