掲示板

Every instance of the portlet uses the same copy of the variables (not inte

12年前 に Al Razor によって更新されました。

Every instance of the portlet uses the same copy of the variables (not inte

New Member 投稿: 4 参加年月日: 11/08/07 最新の投稿
Hi,

I have a small logistics-related portlet in Liferay 6.0.6.
I got the following problem: it seems that every instance of the portlet uses the same copy of the variables. Even when I change user, I still receive the same information that the last user has selected.
Maybe you could help me to find what causes it?

Thanks for any help!
thumbnail
12年前 に David H Nebinger によって更新されました。

RE: Every instance of the portlet uses the same copy of the variables (not

Liferay Legend 投稿: 14914 参加年月日: 06/09/02 最新の投稿
You should not be using local variables to hold values between method calls, i.e. between processAction() and doView(). You have no guarantee that the value that you are currently setting in processAction() will be the same value found in doView().
12年前 に Al Razor によって更新されました。

RE: Every instance of the portlet uses the same copy of the variables (not

New Member 投稿: 4 参加年月日: 11/08/07 最新の投稿
David H Nebinger:
You should not be using local variables to hold values between method calls, i.e. between processAction() and doView(). You have no guarantee that the value that you are currently setting in processAction() will be the same value found in doView().



Thanks for the advice, David.
After I modified the logic and moved most of it into processAction method, it seems to work correctly.

Could you please give me some other advice about my refactored code:
Java file
	@Override
	protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
		{
		request.setAttribute("countryList", getCountries());

		response.setContentType("text/html");
		PortletContext portletContext = getPortletContext();
		PortletRequestDispatcher prd = portletContext.getRequestDispatcher("/view.jsp");
		prd.include(request, response);
		}

	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
		{
		// PortletSession session = request.getPortletSession();
		String departure_country = request.getParameter("departureCountry");
		String arrival_country = request.getParameter("arrivalCountry");
		String departure_port = request.getParameter("departurePort");
		String arrival_port = request.getParameter("arrivalPort");
		String submit_trigger = request.getParameter("submit_trigger");
		boolean both_ports_selected = true;

		request.setAttribute("departureCountry", departure_country);
		request.setAttribute("arrivalCountry", arrival_country);

		if ((departure_country != null) && (!"".equals(departure_country)))
			{
			request.setAttribute("departurePortList", getPorts(departure_country));
			}

		if ((arrival_country != null) && (!"".equals(arrival_country)))
			{
			request.setAttribute("arrivalPortList", getPorts(arrival_country));
			}

		if ((departure_port != null) && (!"".equals(departure_port)))
			request.setAttribute("dPort", departure_port);
		else
			both_ports_selected = false;


		if ((arrival_port != null) && (!"".equals(arrival_port)))
			request.setAttribute("aPort", arrival_port);
		else
			both_ports_selected = false;

		
		// If user pressed Submit button, check if departure and arrival ports are selected
		if ((submit_trigger != null) && both_ports_selected)
			{
			System.out.println("submit_trigger is not null!");
			if ((arrival_port != null) && (!"".equals(arrival_port)))
				{

				ArrayList<shippinglineitem> shipping_line_list = getShippingLines(departure_port, arrival_port);
				if (shipping_line_list != null)
					request.setAttribute("shippingLineList", shipping_line_list);
				}
			}

		}</shippinglineitem>


view.jsp

&lt;%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%&gt;

<portlet:defineobjects />

<script type="text/javascript" src="js/main.js"></script>


<c:if test="${fn:length(shippingLineList) > 0}">
	<c:foreach items="${shippingLineList}" var="entry">
					</c:foreach><table id="travel" class="sample" summary="Shipping lines services from eLOGMAR-M">
		<caption>Shipping lines services 5</caption>
		<thead>
			<tr>
				<th scope="col">Company</th>
				<th scope="col">Website (URL)</th>
				<th scope="col">Frequency</th>
				<th scope="col">Transit Time</th>
			</tr>
		</thead>
	
		<tbody>
			<tr>
				</tr><tr>
						<td><c:out value="${entry.company}" /></td>
						<td><c:out value="${entry.url}" /></td>
						<td><c:out value="${entry.frequency}" /></td>
						<td><c:out value="${entry.transitTime}" /></td>
					</tr>
				
			
		</tbody>
	</table>
</c:if>
<br>

<div id="wrap">
	<form name="mainForm" action="<portlet:actionURL></portlet:actionURL>" method="post">
		<div id="fields">
			<select name="departureCountry" onchange="changeSelectedOption()" id="left">
				
					
						
							<option selected value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
						
							<option value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
					
				
			</select>

			&lt;%--  --%&gt;

			<select name="arrivalCountry" onchange="changeSelectedOption()" id="right">
				
					
						
							<option selected value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
						
							<option value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
					
				
			</select> <br>

			<div class="clear"></div>

			<select name="departurePort" id="left">
				
					
						
							<option selected value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
						
							<option value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
					
				
			</select> 
			<select name="arrivalPort" id="right">
									
					
						
							<option selected value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
						
							<option value="<c:out value=&quot;${entry.value}&quot;/>">
								
							</option>
						
					
				
			</select> <br>
		</div>

		<input name="submit_trigger" type="checkbox" style="display: none;"> <input name="find_shipping_lines" type="button" value="Search" onClick="submitButtonClick()">
	</form>
</div>