Foren

Struts ActionForms with MultiPart forms bug... [Updated]

thumbnail
Jim Klo, geändert vor 14 Jahren.

Struts ActionForms with MultiPart forms bug... [Updated]

Junior Member Beiträge: 75 Beitrittsdatum: 07.11.08 Neueste Beiträge
Wondering if anyone has any clues what might be going on - while this isn't the first time I've seen this behavior, this is the first time I don't have a workaround...


I need to add some form fields to files within the Document Library that gets stored in the DLFileEntry.getExtraSettingsProperties(). I noticed while looking at the 5.2.3 source there is a file:
/portal-web/docroot/html/portlet/document_library/edit_file_entry_form_extra_fields.jsp

within there is the following:

<%--

// Override this file to add your own custom fields for file entries.

--%>

<%--
	
		<liferay-ui:message key="author" />
	
	
		<input name="ExtraSettingsProperties(author)" style="width: <%= ModelHintsConstants.TEXT_DISPLAY_WIDTH %>px;" type="text" value="<bean:write name=" DOCUMENT_LIBRARY_FILE_ENTRY" property="extraSettingsProperties(author)">" /&gt;
	


	
		<liferay-ui:message key="type" />
	
	
		<input name="ExtraSettingsProperties(type)" style="width: <%= ModelHintsConstants.TEXT_DISPLAY_WIDTH %>px;" type="text" value="<bean:write name=" DOCUMENT_LIBRARY_FILE_ENTRY" property="extraSettingsProperties(type)">" /&gt;
	
--%&gt;


which looks great, I can just add my extra fields by using the Struts ActionForm notation...

When I look at the Action in com.liferay.portlet.documentlibrary.action.EditFileEntryAction.java of which this Form should post and part of the Struts lifecycle, the form should populate.

However the form never populates, there's obviously a form instance, but the form never gets the values populated. Now technically, but struts definition the property name should start with a lowercase letter on the <input name="extraSettingsProperties(test)" ... />, however I've tried this both ways with no success.

Are Struts forms just inherently broken in Liferay? I've gone down this rat hole before when trying to build my own portlets previously and noticed that the form beans don't work, and had to use the ParamUtils to get the form values.

Does anyone know how to resolve or fix this - it seems no one is either using ActionForms or no one has noticed this problem...

Please someone help!
thumbnail
Jim Klo, geändert vor 14 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Junior Member Beiträge: 75 Beitrittsdatum: 07.11.08 Neueste Beiträge
Update: After actually stepping through Struts source.... I've determined... It looks like Liferay is broke... or at least not interfacing with Struts 100% correctly.

It turns out that Struts Form Beans work only work for non-multipart forms within Liferay Portlets. The Document Library edit form - is a multi-part form, which would explain why this isn't working.

From what I can tell, it looks like in order to fix this, a custom MultipartRequestHandler needs to be written such that the parameters and file objects get parsed correctly. Then this handler needs to injected at the right time, which I'm not totally sure what conditions need to be met to do this (otherwise, I'd just fix it)
Mazhar Anwar, geändert vor 14 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Regular Member Beiträge: 125 Beitrittsdatum: 05.02.10 Neueste Beiträge
Hi Jim,

Liferay has taken care for multipart encrypted form by com.liferay.portal.kernel.upload.UploadPortletRequest Interface.
emoticon

You can use it as follows,

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
				actionRequest);

// for File 
File file = uploadRequest.getFile(PARAMETER_FILENAME);
// where PARAMETER_FILENAME is html input tag name on your jsp.

// for other request parameters
String parameterName = uploadRequest.getParameter(PARAMETER_NAME);
//where PARAMETER_NAME is html tag name on your jsp.



Hope this will solve your problem.

Regards,
Mazhar
thumbnail
Jim Klo, geändert vor 14 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Junior Member Beiträge: 75 Beitrittsdatum: 07.11.08 Neueste Beiträge
Sure... you're missing the point... I know I can do that (and probably will end up having to do that by rewriting the Document Library action so they work)... the problem is there are several touch points in the portal source already that assume that Struts MVC works for multipart forms - when it clearly doesn't.

What I'm proposing is the actual FIX so Struts MVC works as it's supposed to - not by using a workaround method that uses a getter utility.

Part of the beauty of Struts MVC, is that you can have a form bean POJO that get's populated during the request, so when you're in your Action classes execute, you don't have to fetch each of the parameters individually which is what you're solution proposes. (oddly this all works in liferay using the Sample Struts Portlet - assuming because this uses the Struts Bridge, however the Sample Struts Liferay Portlet doesn't even instantiate right for some reason)
thumbnail
Nagendra Kumar Busam, geändert vor 13 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Liferay Master Beiträge: 678 Beitrittsdatum: 07.07.09 Neueste Beiträge
Hi Jim,

Is there any simple way to make it work. BTW, i am using LR 5.2.3

I have a requirement for which i need to add custom attribute like status which will have publish/pending as values.

Any inputs are much welcome

Thanks & Regards,
- Nagendra Kumar
thumbnail
Jim Klo, geändert vor 13 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Junior Member Beiträge: 75 Beitrittsdatum: 07.11.08 Neueste Beiträge
Yes I got this working, rather trivially.

  • Extend EditFileEntryAction and Update the Struts Mapping to use your new class.
  • Override updateFileEntry(...) by copying from the original then change this:
    String extraSettings = PropertiesUtil.toString(
    			fileEntryForm.getExtraSettingsProperties());

    to this:
    String extraSettings = PropertiesUtil.toString(
    			fileEntryForm.getExtraSettingsProperties());
    		
    		Enumeration paramNames = uploadRequest.getParameterNames();
    		Pattern regex = Pattern.compile(".*ExtraSettingsProperties\\(([^\\)]+)\\)");
    		StringBuilder extraSettingsSB = new StringBuilder();
    		while(paramNames.hasMoreElements()) {
    			String paramName = (String)paramNames.nextElement();
    			if (paramName != null) {
    				Matcher m = regex.matcher(paramName);
    				if(m.matches()) {
    					String key = m.group(1);
    					String value = ParamUtil.getString(uploadRequest, "ExtraSettingsProperties("+key+")");
    					extraSettingsSB.append(key);
    					extraSettingsSB.append(StringPool.EQUAL);
    					extraSettingsSB.append(value);
    					extraSettingsSB.append(StringPool.NEW_LINE);
    				}
    			}
    		}
    		extraSettings = extraSettingsSB.toString();
  • Follow the pattern defined in edit_file_entry_form_extra_fields.jsp for adding your extra fields.
  • Compile and deploy.
thumbnail
Nagendra Kumar Busam, geändert vor 13 Jahren.

RE: Struts ActionForm and 5.2.3 aka edit_file_entry_form_extra_fields.jsp b

Liferay Master Beiträge: 678 Beitrittsdatum: 07.07.09 Neueste Beiträge
Thank you Jim, i will give it a try & update later about the status