Foros de discusión

Page refresh issue with re-submitting data

Mike Matovic, modificado hace 10 años.

Page refresh issue with re-submitting data

Junior Member Mensajes: 38 Fecha de incorporación: 6/12/13 Mensajes recientes
I have a portlet with a number of different JSPs. One of these JSPs has a form that calls up an action to create a record in one of my tables. Here is the JSP example:

<%@ include file="/init.jsp" %>
<%
String currTab = ParamUtil.getString(request, "currTab", "Home" );
%>

<portlet:actionurl name="registerUser" var="registerUserURL" />

<portlet:renderurl var="backURL">
	<portlet:param name="currTab" value="<%= currTab %>" />
</portlet:renderurl>

<a href="<%= backURL.toString() %>">Back to Listing</a>
<div class="row">
	<div class="span6 offset3">
		<form action="<%= registerUserURL.toString() %>" method="post">
			<fieldset class="CLFieldset">
				<legend>Register Form</legend>
				<label>First Name</label>
				<b>&lt;%= themeDisplay.getUser().getFirstName() %&gt;</b>
				<label>Last Name</label>
				<b>&lt;%= themeDisplay.getUser().getLastName() %&gt;</b>
				<label>Email Address</label>
				<input type="text" name="emailAddress" placeholder="someone@email.com">
				<input type="hidden" name="currTab" value="<%= currTab %>">
				<button type="submit" class="btn">Submit</button>
			</fieldset>
		</form>
	</div>
</div>


And here is the action in the portlet class:

public void registerUser( ActionRequest request, ActionResponse response ) throws Exception {
		System.out.println("registerUser: started");
		ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
		String currTab = ParamUtil.getString(request, "currTab");
		Date dateCreated = new Date();
		Date dateModified = dateCreated;//we don't need two separate dates here
		
		//create a user to validate against
		CLUsers clUser = new CLUsersImpl();
		
		//populate the user object
		clUser.setCompanyId(themeDisplay.getCompanyId());
		clUser.setGroupId(themeDisplay.getScopeGroupId());
		clUser.setFirstName(themeDisplay.getUser().getFirstName());
		clUser.setLastName(themeDisplay.getUser().getLastName());
		clUser.setModerator(false);//someone with higher privileges must set this
		clUser.setBanned(false);//wouldn't make sense to ban someone outright now would it? :)
		clUser.setDateCreated(dateCreated);
		clUser.setDateModified(dateModified);
		clUser.setLiferayUserId(themeDisplay.getUserId());
		clUser.setEmail(ParamUtil.getString(request, "emailAddress"));
		
		System.out.println("companyId: " + Long.toString(clUser.getCompanyId()));
		System.out.println("groupId: " + clUser.getGroupId());
		System.out.println("firstName: " + clUser.getFirstName());
		System.out.println("lastName: " + clUser.getLastName());
		System.out.println("moderator: " + Boolean.toString(clUser.getModerator()));
		System.out.println("banned: " + Boolean.toString(clUser.getBanned()));
		System.out.println("dateCreated: " + clUser.getDateCreated().toString());
		System.out.println("dateModified: " + clUser.getDateModified().toString());
		System.out.println("Liferay ID: " + clUser.getLiferayUserId());
		System.out.println("email: " + clUser.getEmail());
		
		
		//validate our user object
		ArrayList<string> errors  = new ArrayList<string>();
		
		if ( CamelslistValidator.validateUser(clUser, errors) ) {
			CLUsersLocalServiceUtil.addUser(clUser, themeDisplay.getUserId());
			SessionMessages.add(request, "userSaved");
		} else {
			for ( String error : errors ) {
				SessionErrors.add(request, error);
				_log.error(error);
				System.err.println(error);
			}
		}
		
		//head to some page here
		response.setRenderParameter("currTab", currTab);
		response.setRenderParameter("jspPage", mainJSP);
	}</string></string>


Once the person is landed back at the mainJSP page, if they refresh the page, they create another record emoticon

My real answer is probably to user struts or some other framework to manage this stuff but I need a fix for the current product. What can I do?
thumbnail
Pankaj Kathiriya, modificado hace 10 años.

RE: Page refresh issue with re-submitting data

Liferay Master Mensajes: 722 Fecha de incorporación: 5/08/10 Mensajes recientes
Use response.sendRedirect at the end of action method
Mike Matovic, modificado hace 10 años.

RE: Page refresh issue with re-submitting data

Junior Member Mensajes: 38 Fecha de incorporación: 6/12/13 Mensajes recientes
Pankaj Kathiriya:
Use response.sendRedirect at the end of action method


I tried a redirect but when I attempted to build a URL using the PortletURLFactoryUtil, I found out that I could not return a portlet display Id. For exmaple, consider the following code:

System.out.println( themeDisplay.getPortletDisplay().getId() );


That code returns nothing. How can I build a URL without that working properly?
thumbnail
Pankaj Kathiriya, modificado hace 10 años.

RE: Page refresh issue with re-submitting data

Liferay Master Mensajes: 722 Fecha de incorporación: 5/08/10 Mensajes recientes
Hello Mike,
Apart from what I said , using response.sendRedirect(jsppath). There is also one other handy option available in liferay-portlet.xml.

You can use element action-url-redirect to avoid re-submission of data. Following is information about it.

Element : action-url-redirect
Set the action-url-redirect value to true if an action URL for this
portlet should cause an auto redirect. This helps prevent double
submits. The default value is false.



Hope this would help you.
Regards,
tulsi m, modificado hace 10 años.

RE: Page refresh issue with re-submitting data

New Member Mensajes: 13 Fecha de incorporación: 10/10/13 Mensajes recientes
Hi,'

you can try by configuring <action-url-redirect > true </action-url-redirect > in liferay-portlet.xml


Regards,
Tulsi
Mike Matovic, modificado hace 10 años.

RE: Page refresh issue with re-submitting data

Junior Member Mensajes: 38 Fecha de incorporación: 6/12/13 Mensajes recientes
If I set <action-url-redirect>true</action-url-redirect> then I cannot seem to maximize the portlet. Aside from something like:
response.setWindowState( WindowState.MAXIMIZED );


how do I maximize my portlet upon the redirect?
thumbnail
Kalai arasan, modificado hace 9 años.

RE: Page refresh issue with re-submitting data

Regular Member Mensajes: 127 Fecha de incorporación: 2/01/13 Mensajes recientes
Hi!
I Set the action-url-redirect value to true liferay-portlet.xml for prevent submission while refreshing the page. Thats work fine, but while retrieve data from request.getAttribute(""); method, its displaying null value. If I removed the action-url-redirect value to true liferay-portlet.xml that displaying value correctly, but it's not working properly while using the action-url-redirect value to true liferay-portlet.xml. I want to use that both, can you please help me on this.
ghita Daoudi, modificado hace 8 años.

RE: Page refresh issue with re-submitting data

New Member Mensajes: 2 Fecha de incorporación: 17/08/11 Mensajes recientes
Hi,

To prevent resubmitting data on refresh page you must do this :
- Set action-url-redirect value to true in liferay-portlet.xml
- Send data from action with method : actionResponse.setRenderParameter("key", "value");
- Retrieve data in jsp with : String value = ParamUtil.getBoolean(request, "key");

Regards,
Ghita