Render()'s Method different reactions - liferay

I have my render() method with no code.
And I have this action-method:
#ProcessAction(name = "viewBook")
public void viewBook(ActionRequest actionRequest,ActionResponse actionResponse) throws SystemException, PortalException {
long bookId = ParamUtil.getLong(actionRequest, "bookId");
Book book = BookLocalServiceUtil.getBook(bookId);
actionRequest.setAttribute(FinalStrings.BOOK_ENTRY, book);
actionResponse.setRenderParameter("jspPage", "/html/LibraryPortlet/view_book.jsp");
How can I rewrite this "GET" method into render() method? I mean I need to run
public void render(RenderRequest renderRequest, RenderResponse rendeResponse){
super.render(renderRequest, renderResponse)
}
in the default situation and
public void render(RenderRequest renderRequest, RenderResponse rendeResponse){
\\THIS CODE IS NOT WORKING, IT'S JUST TO SHOW WHAT I WANT!
long bookId = ParamUtil.getLong(actionRequest, "bookId");
Book book = BookLocalServiceUtil.getBook(bookId);
actionRequest.setAttribute(FinalStrings.BOOK_ENTRY, book);
actionResponse.setRenderParameter("jspPage", "/html/LibraryPortlet/view_book.jsp");
when I need to use viewBook() method. How can I parametrize(?) render() method?
Update:
For more details I attached one screenshot:
Meanwhile in my action.jsp:
<liferay-ui:icon-menu>
<portlet:actionURL name="deleteBook" var="deleteURL">
<portlet:param name="bookId"
value="${String.valueOf(book.getBookId())}" />
</portlet:actionURL>
<portlet:renderURL name="viewBook" var="viewURL">
<portlet:param name="bookId"
value="${String.valueOf(book.getBookId())}" />
</portlet:renderURL>
<portlet:renderURL var="editBookURL" name="viewEdit">
<portlet:param name="bookId" value="${String.valueOf(book.getBookId())}" />
</portlet:renderURL>
<liferay-ui:icon image="add" message="View" url="${viewURL.toString()}" />
<liferay-ui:icon image="add" message="Edit" url="${editBookURL.toString()}" />
<liferay-ui:icon-delete image="delete" message="Delete" url="${deleteURL.toString()}" />
In my view_book.jsp:
<%#page import="com.softwerke.FinalStrings"%>
<%#page import="com.softwerke.model.Book"%>
<%#page import="com.softwerke.service.BookLocalServiceUtil"%>
<%#page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%# include file="/html/init.jsp"%>
<portlet:renderURL var="backURL">
<portlet:param name="jspPage" value="/html/view.jsp"/>
</portlet:renderURL>
<liferay-ui:header backURL="${backURL}" title="Back" />
<%
Book book = (Book) request.getAttribute(FinalStrings.BOOK_ENTRY);
%>
<aui:form>
<aui:model-context bean="${book}" model="${Book.class}" />
<aui:input name="bookName" label="Book Name" disabled="true"/>
<aui:input type="textarea" name="description" label="Description" disabled="true"/>
<aui:input name="authorName" label="Author Name" disabled="true"/>
<aui:input name="price" label="Price" disabled="true"/>
</aui:form>
According to my task I can not use Action URL here. What should I do???

You can send a command param to the render, so you can split the logic into two or more render methods, depending on the command, or maybe easier, invoke a mvcPath without the need of implementing anything in render(). Include something like this in the jsp with your list of books:
<portlet:renderURL var="myBookURL">
<portlet:param name="mvcPath" value="/view_book.jsp" />
<portlet:param name="bookId" value="<%= someBookId %>" />
</portlet:renderURL>
View My Book
You just need a view_book.jsp with something like:
<%
long bookId = ParamUtil.getLong(request, "bookId");
Book book = BookLocalServiceUtil.getBook(bookId);
%>
Hope it helps.

Related

Liferay 7: hidden aui input won't set value based on parameter

I have an entity with a primary key and two other fields.
I am able to display them in a Search Container in my primary View JSP, and I want to implement an edit/update function, so I created a different JSP for that. I pass the properties of the entity I wish to edit in portlet:renderURL portlet:param tags just like this:
<portlet:renderURL var="editEntity">
<portlet:param name="jspPage" value="/update-page.jsp" />
<portlet:param name="primaryKey" value="<%= entityId %>" />
<portlet:param name="name" value="<%= entityName%>" />
<portlet:param name="description" value="<%= entityDesc%>" />
</portlet:renderURL>
In the update-page JSP if I set any input field hidden, the parameter based values disappear, so the controller cannot process the fields' values.
i.e.:
<aui:input name="primaryKey" type="hidden" value="${primaryKey}" />
<aui:input name="primaryKey" type="hidden" value="${name}" />
<aui:input name="primaryKey" type="hidden" value="${description}" />
Note: I only want to hide the primary key field, the controller servlet should be able to process it and update my entity based on the primary key, like this:
<aui:input name="primaryKey" type="text" value="${name}" />
<aui:input name="primaryKey" type="text" value="${description}" />
The funny thing is, that everything just works when I set the input fields text type, but I wouldn't want the users to enter the primary key, duh...
Any ideas how could I fix this?
It's work for me
view.jsp
<%# include file="init.jsp" %>
<portlet:actionURL name="testURL" var="testURL" />
<aui:form name="fm" method="post" action="<%= testURL.toString()%>">
<aui:input name="primaryKey" type="hidden" value="123" />
<aui:button-row>
<aui:button name="submit" type="submit" value="OK" />
</aui:button-row>
</aui:form>
TestmvcportletPortlet.java
package com.example.portlet;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
import com.liferay.portal.kernel.util.ParamUtil;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.Portlet;
import javax.portlet.ProcessAction;
import org.osgi.service.component.annotations.Component;
#Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.sample",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=Test Portlet",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
public class TestmvcportletPortlet extends MVCPortlet {
#ProcessAction(name = "testURL")
public void addBook(ActionRequest actionRequest,ActionResponse actionResponse) throws SystemException {
String a = ParamUtil.getString(actionRequest, "primaryKey");
System.out.println("Value is "+a);
}
}
have you found anything that you missed code?
I found a solution to the problem.
So, after long hours of testing I found out that I just could not get the values stored in parameters just as ${paramName} anywhere in a simple HTML tag, but I still don't know why.
What I did was request for the needed values stored within the parameters inside a JSP scriptlet just like this:
<%
String primaryKey = request.getParameter("primaryKey");
String name = request.getParameter("name");
String description = request.getParameter("description");
%>
Then I was good to go with my form:
<aui:form action="<%= updateAbbreviationURL %>" method="post">
<aui:input name="primaryKey" type="hidden" value="<%= primaryKey %>" />
<aui:input name="entityName" label="Name" type="text" value="<%= name %>" />
<aui:input name="entityDesc" label="Description" type="text" value="<%= description %>" />
<aui:button name="submit" value="submit" type="submit" />
<aui:button name="cancel" value="cancel" type="button" onClick="<%= viewURL %>" />
</aui:form>
I'd be really grateful if someone told me why my initial implementation didn't work, I mean referring to the parameter values as mentioned above, ${paramName}
Thanks in advance!

What is the best way avoid reload the jsp page?

enter image description hereHere is my jsp code
<portlet:renderURL var="rssSearchResult">
<portlet:param name="<%=CMD.ACTION%>"
value="<%=RenderKeys.FIND_CONTENT_SEARCH%>" />
<%-- <portlet:param name="categoryId" value="<%=String.valueOf(category.getCategoryId())%>"/> --%>
<div id="category-content" class="span8">
<aui:form render="<%=rssSearchResult %>">
<aui:input placeholder="Please Enter RSS Feed Title Here"
name="search" label="" id="searchTitle" type="text"
style="width:300px;" />
<aui:button type="button" name="search" id="searchTitle" value="submit" class="Find_content_search"
onclick="javascript:mynav('<%=rssSearchResult%>');addinMoveableBar();"></aui:button>
</aui:form>
*************Here is my java source code************
#RenderMapping(params=CMD.ACTION+StringPool.EQUAL+RenderKeys.FIND_CONTENT_SEARCH)
public void rssSearchResult(RenderRequest renderRequest,
RenderResponse renderResponse) {
String searchValue= ParamUtil.getString(renderRequest, "search");
DynamicQuery SearchaQuery = DynamicQueryFactoryUtil.forClass(
AssetEntry.class, PortletClassLoaderUtil.getClassLoader());
SearchaQuery.add(PropertyFactoryUtil.forName("title").eq(searchValue));
try {
List<AssetEntry> assentries=AssetEntryLocalServiceUtil.dynamicQuery(SearchaQuery);
List<AssetCategory> assetcategory=null;
for(AssetEntry assetentry:assentries){
assetcategory= AssetCategoryLocalServiceUtil.getAssetEntryAssetCategories(assetentry.getEntryId(), -1, -1);
}
renderRequest.setAttribute("AssetCategory", assetcategory);
renderRequest.setAttribute("AssetEntries", assentries);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
*************main.js***************
function mynav(navurl){
ajaxindicatorstart("Processing Please Wait");
loadPage(navurl);
history.pushState({}, '', navurl);
}
I have a search text box with submit button if i search title of category it has to be display without reloading the page....

Liferay: How to place values (or not lose values) in ParamUtil in case of exception?

I am new to developing on Liferay and am currently using Liferay 6.2 on Eclipse Kepler IDE.
I am currently making a small webpage based on the following view & add/update example in the development guide
In the example the method paramUtil.getLong(long) is used to pass the id of the row to be updated to the add/update page.
My issue is that when an exception occurs (on the back end) this data is lost and the page changes to an add entry page.
How do I retain the data sot that ParamUtil can retrieve it when an exception is thrown on the back end?
I have included snippets of the example front end code below.
VIEW.JSP:
<liferay-ui:search-container-row
className="com.nosester.portlet.eventlisting.model.Location"
keyProperty="locationId"
modelVar="location" escapedModel="<%= true %>"
>
<liferay-ui:search-container-column-text
name="name"
value="<%= location.getName() %>"
/>
<liferay-ui:search-container-column-text
name="description"
value="<%= location.getDescription() %>"
/>
<liferay-ui:search-container-column-text
name="streetAddress"
value="<%= location.getStreetAddress() %>"
/>
<liferay-ui:search-container-column-text
name="city"
value="<%= location.getCity() %>"
/>
<liferay-ui:search-container-column-text
name="stateOrProvince"
value="<%= location.getStateOrProvince() %>"
/>
<liferay-ui:search-container-column-text
name="country"
value="<%= location.getCountry() %>"
/>
<liferay-ui:search-container-column-jsp
align="right"
path="/html/locationlisting/location_actions.jsp"
/>
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
ACTIONS.JSP
<%
ResultRow row = (ResultRow) request
.getAttribute(WebKeys.SEARCH_CONTAINER_RESULT_ROW);
Location location = (Location) row.getObject();
long groupId = location.getGroupId();
String name = Location.class.getName();
long locationId = location.getLocationId();
String redirect = PortalUtil.getCurrentURL(renderRequest);
%>
<liferay-ui:icon-menu>
<portlet:renderURL var="editURL">
<portlet:param name="mvcPath" value="/html/locationlisting/edit_location.jsp" />
<portlet:param name="locationId" value="<%= String.valueOf(locationId) %>" />
<portlet:param name="redirect" value="<%= redirect %>" />
</portlet:renderURL>
<liferay-ui:icon image="edit" url="<%= editURL.toString() %>" />
EDIT.JSP
<%
Location location = null;
long locationId = ParamUtil.getLong(request, "locationId");
if (locationId > 0) {
location = LocationLocalServiceUtil.getLocation(locationId);
}
String redirect = ParamUtil.getString(request, "redirect");
%>
<aui:model-context bean="<%= location %>" model="<%= Location.class %>" />
<portlet:renderURL var="viewLocationURL" />
<portlet:actionURL name='<%= location == null ? "addLocation" : "updateLocation" %>' var="editLocationURL" windowState="normal" />
<liferay-ui:header
backURL="<%= viewLocationURL %>"
title='<%= (location != null) ? location.getName() : "New Location" %>'
/>
<aui:form action="<%= editLocationURL %>" method="POST" name="fm">
<aui:fieldset>
<aui:input name="redirect" type="hidden" value="<%= redirect %>" />
<aui:input name="locationId" type="hidden" value='<%= location == null ? "" : location.getLocationId() %>'/>
<aui:input name="name" />
<aui:input name="description" />
<aui:input name="streetAddress" />
<aui:input name="city" />
<aui:input name="stateOrProvince" />
<aui:input name="country" />
</aui:fieldset>
<aui:button-row>
<aui:button type="submit" />
<aui:button onClick="<%= viewLocationURL %>" type="cancel" />
</aui:button-row>
UPDATE 1:
Adding back end logic as requested by Parkash Kumar:
LocationListingPortlet.java (Link to Example from Document)
public void updateLocation(ActionRequest request, ActionResponse response)
throws Exception {
_updateLocation(request);
sendRedirect(request, response);
}
private Location _updateLocation(ActionRequest request)
throws PortalException, SystemException {
long locationId = (ParamUtil.getLong(request, "locationId"));
String name = (ParamUtil.getString(request, "name"));
String description = (ParamUtil.getString(request, "description"));
String streetAddress = (ParamUtil.getString(request, "streetAddress"));
String city = (ParamUtil.getString(request, "city"));
String stateOrProvince = (ParamUtil.getString(request, "stateOrProvince"));
String country = (ParamUtil.getString(request, "country"));
ServiceContext serviceContext = ServiceContextFactory.getInstance(
Location.class.getName(), request);
Location location = null;
if (locationId <= 0) {
location = LocationLocalServiceUtil.addLocation(
serviceContext.getUserId(), serviceContext.getScopeGroupId(), name, description,
streetAddress, city, stateOrProvince, country, serviceContext);
}
else {
location = LocationLocalServiceUtil.getLocation(locationId);
location = LocationLocalServiceUtil.updateLocation(
serviceContext.getUserId(), locationId, name,
description, streetAddress, city, stateOrProvince, country,
serviceContext);
}
return location;
}
private static Log _log = LogFactoryUtil.getLog(LocationListingPortlet.class);
}
In-case exception occurs, handle it in _updateLocation and reset required parameters in catch, as following:
Location location = null;
try {
if (locationId <= 0) {
location = LocationLocalServiceUtil.addLocation(
serviceContext.getUserId(), serviceContext.getScopeGroupId(),
name, description, streetAddress, city, stateOrProvince,
country, serviceContext);
} else {
location = LocationLocalServiceUtil.getLocation(locationId);
location = LocationLocalServiceUtil.updateLocation(
serviceContext.getUserId(), locationId, name,
description, streetAddress, city, stateOrProvince, country,
serviceContext);
}
}catch(Exception ex){
_log.error(ex);
// set render parameters
request.setRenderParameter("locationId", locationId);
}

Issue with Liferay UI tab Navigation

I am using <liferay-ui:tabs> for displaying jsp pages as tabs. I am able to see my pages as tabs but If I navigate to one of my pages in the tab and on click of the button then it is navigating to some other page, that it showing in a separate page instead of showing under that tab. I need to click of button events the control should be still under the tabs. my Code is as follows,
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%><%#include file="/html/users/init.jsp"%>
<portlet: defineObjects />
<portlet:renderURL var="navigateTabURL"/>
<% String navigateTab = ParamUtil.getString(request, "tabs1","Current"); %>
<liferay-ui:tabs names="Current, Available" url="<%=navigateTabURL.toString()%>" >
<c:if test='<%= navigateTab.equalsIgnoreCase("Current")%>' >
<jsp:include page="current_members.jsp" flush="true" />
</c:if>
<c:if test='<%= navigateTab.equalsIgnoreCase("Available")%>' >
<jsp:include page="available_members.jsp" flush="true" />
</c:if>
</liferay-ui:tabs>
The pages "Current" and "Available" are showing correctly, But If I click any button in my Current page it is navigating to some other jsp I need that jsp also under that tabs only not showing as a separate page.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%><%#include file="/html/users/init.jsp"%>
<portlet: defineObjects />
<%
List<User> userList = (List) request.getAttribute("UserGroupList");
//out.println(userList.size());
%>
<%!
List<User> users = null;
int totalNoOfUsers=0;
String value = null;
%>
<%
//totalNoOfUsers = UserLocalServiceUtil.getUsersCount();
totalNoOfUsers = userList.size();
users = userList;
%>
<liferay-ui:search-container delta="5" emptyResultsMessage="no-users-were-found" rowChecker="<%= new RowChecker(renderResponse) %>" >
<liferay-ui:search-container-results results="<%= ListUtil.subList(users,searchContainer.getStart(),searchContainer.getEnd()) %>"
total="<%= totalNoOfUsers %>">
</liferay-ui:search-container-results>
<liferay-ui:search-container-row className="com.liferay.portal.model.User" keyProperty="userId" modelVar="user">
<liferay-ui:search-container-row-parameter name="userIds" value="<%= user.getUserId()%>">
</liferay-ui:search-container-row-parameter>
<liferay-ui:search-container-column-text name="UserName" value="
<%= user.getScreenName()%>" />
<liferay-ui:search-container-column-text name="First Name" value="<%= user.getFirstName() %>">
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text name="Last Name" value="<%= user.getLastName() %>">
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-jsp align="right" path="/html/users/custom_user_actions.jsp" />
</liferay-ui:search-container-row>
<liferay-ui:search-iterator />
</liferay-ui:search-container>
<liferay-ui:search-container delta="5" emptyResultsMessage="no-users-were-found" />
<portlet:actionURL name="viewEntry" var="viewEntryURL"></portlet:actionURL>
<aui:form action="<%= viewEntryURL %>" name="<portlet:namespace />fms">
<aui:button type="submit" value="Cancel"></aui:button>
</aui:form>
How do I navigate the tabs as dynamic requests. Any suggestions please!!
To retain a tab selection, "value" attribute of should be mentioned with name of tab to select.
If "value" attribute not specified then first tab will be considered as active.
For example, please refer following file in liferay portal sources.
portal-web/docroot/html/portlet/dockbar/add_panel.jsp
Also, below post in liferay forum should help.
http://www.liferay.com/en_GB/community/forums/-/message_boards/message/4809190

How to convert a portal URL to its corresponding friendly URL

I have implemented friendly-url successfully for few of my custom portlets and it is working fine.
When click-able links are generated, it correctly shows the friendly-url.
Now my requirement is that, I need to send this render-URL (say URL01) as a parameter (param02) to another URL (URL02) and this URL01 then would be displayed on another page.
This is how URL01 is generated:
<portlet:renderURL var="URL01" windowState="<%=WindowState.MAXIMIZED.toString() %>">
<portlet:param name="redirect" value="<%= currentURL %>" />
<portlet:param name="myId" value="<%= String.valueOf(myObject.getMyId()) %>" />
<portlet:param name="title" value="<%= myObject.getTitle() %>" />
<portlet:param name="name" value="<%= myObject.getName() %>" />
</portlet:renderURL>
This is how URL02 is generated
<portlet:renderURL var="URL02" windowState="<%= LiferayWindowState.POP_UP.toString() %>">
<portlet:param name="redirect" value="<%= currentURL %>" />
<portlet:param name="URL01" value="<%=URL01 %>" />
<portlet:param name="ownerId" value="<%= String.valueOf(ownerId) %>" />
<portlet:param name="groupId" value="<%= String.valueOf(scopeGroupId) %>" />
</portlet:renderURL>
This URL02 would open a pop-up and the URL01 would be displayed as below in the JSP:
URL: <%= ParamUtil.getString(request, "URL01") %>
But this shows URL01 (unfriendly-URL) as:
URL: http://localhost:8080/web/guest/mypage?p_p_id=my_WAR_myportlet&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&_my_WAR_myportlet_myId=10989&_my_WAR_myportlet_title=This+is+miine&my_WAR_myportlet_name=What+name
If this is a clickable link it generates perfectly as (friendly-URL):
Click me!
So I need a utility which can convert my unfriendly-url to friendly-URL, something like if a String of unfriendly-url is passed - it would convert that to the friendly-url shown above.
Or I have to create an implementation of my own to achieve this?
Edit:
<route>
<pattern>/{myId:\d+}/{title:.+}/{name:.+}/{p_p_state}</pattern>
<ignored-parameter name="redirect" />
<implicit-parameter name="p_p_id">my_WAR_myportlet</implicit-parameter>
<implicit-parameter name="p_p_lifecycle">0</implicit-parameter>
<implicit-parameter name="p_p_mode">view</implicit-parameter>
</route>
And yes the URL01 is written above the URL02 in the JSP.
I think you can create your own <portlet-url-class> and it will be used when you create the portlet URL with the <portlet:renderURL> tag.
In liferay-portlet.xml, you can define the <portlet-url-class> entry.
If you check the source code of PortletResponseImpl.java, the method createLiferayPortletURL() checks for the PortletURLGenerationListener for that portlet.
I think, you can create a this one and modify URL as you want to.

Resources