Liferay:如何在异常情况下在 ParamUtil 中放置值(或不丢失值)?

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

我是 Liferay 开发新手,目前正在 Eclipse Kepler IDE 上使用 Liferay 6.2。

我目前正在根据 development guide

中的以下视图和 add/update 示例制作一个小网页

在示例中,方法paramUtil.getLong(long) 用于将要更新的行的id 传递给add/update 页面。

我的问题是,当发生异常时(在后端)此数据丢失并且页面更改为添加条目页面。

如何保留数据以便在后端抛出异常时 ParamUtil 可以检索它?

我在下面包含了示例前端代码的片段。

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>

更新 1:

按照 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);

}

万一出现异常,在_updateLocation中处理,在catch中重新设置需要的参数,如下:

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);
}