Liferay:如果出现异常,如何在ParamUtil中放置值(或不丢失值)?

时间:2015-12-14 08:05:38

标签: liferay liferay-6

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

我目前正在根据以下视图制作一个小网页&在development guide

中添加/更新示例

在示例中,方法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);

}

0 个答案:

没有答案