Raging Goblin

17 July 2013

Spring Roo 10: Update images

Filed under: Java,Spring Roo — Raging Goblin @ 20:23
Tags: ,

In my previous blog about uploading images I left it to the reader to come up with a solution for updating an image once a LogItem is created. However, I recently noticed that I was not able to update this image as easy as I thought, due to the fact that the HiddenHttpMethodFilter provided by Spring is not capable of handling multipart forms. This will have the effect that when you click on an update button, you will end up creating a new one as the POST request will end up in this function. This is easy to remedy, but as it took me a while, I will put you in the right direction. We will continue where we left off the last time.

Step 9: Update update.jspx to send a MultipartFile

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div xmlns:field="urn:jsptagdir:/WEB-INF/tags/form/fields" xmlns:form="urn:jsptagdir:/WEB-INF/tags/form" xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <jsp:output omit-xml-declaration="yes"/>
    <form:update id="fu_raging_goblin_roo_domain_LogItem" modelAttribute="logItem" multipart="true" path="/logitems/update" versionField="Version" z="user-managed">
        <field:input field="message" id="c_raging_goblin_roo_domain_LogItem_message" z="apnKidZldiFISmA4Q9pumki/o6Q="/>
        <field:datetime dateTimePattern="${logItem_creationdate_date_format}" field="creationDate" id="c_raging_goblin_roo_domain_LogItem_creationDate" z="p5GCO6w9q76CIDoEn17xbZPqlT0="/>
		<img height="256px" src="${logItem.id}/image"/>
        <field:input field="image" id="c_raging_goblin_roo_domain_LogItem_image" type="file" z="user-managed" required="false"/>
        <field:input field="contentType" id="c_raging_goblin_roo_domain_LogItem_contentType" z="user-managed" render="false"/>
        <field:input field="path" id="c_raging_goblin_roo_domain_LogItem_path" render="false" z="user-managed"/>
    </form:update>
</div>

Note several things. First, I changed the multipart to true, just like the create.jspx page. This will result in the error I described. Therefore I changed the path item so that it points to another path. Secondly, I set the input for the image to ‘not required’ as you might want to choose not to change the image. For the rest, changes are similar to create.jspx.

Step 10: Add an update method to the controller
The update method should be moved from the LogItemController_Roo_Controller aspect to the controller, which by now should be a very familiar trick. Change it to:

@RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/html")
	public String update(@Valid LogItem logItem, BindingResult bindingResult,
			Model uiModel, HttpServletRequest httpServletRequest) {
		if (bindingResult.hasErrors()) {
			populateEditForm(uiModel, logItem);
			return "logitems/update";
		}
		uiModel.asMap().clear();

		CommonsMultipartFile image = logItem.getImage();
		if (image != null && image.getSize() != 0) {
			File file = new File(image.getOriginalFilename());
			try {
				image.transferTo(file);
				logItem.setContentType(image.getContentType());
				logItem.setPath(file.getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
				return "logitems/update";
			}
		} else {
			LogItem logItemFromDb = LogItem.findLogItem(logItem.getId());
			logItem.setContentType(logItemFromDb.getContentType());
			logItem.setPath(logItemFromDb.getPath());
		}

		logItem.merge();
		return "redirect:/logitems/"
				+ encodeUrlPathSegment(logItem.getId().toString(),
						httpServletRequest);
	}

As you see, I put the method on the path /update, which corresponds with the value in update.jspx. The rest is pretty standard in my humble opinion.

Advertisements

3 Comments »

  1. […] from list.jspx by setting render=”false” on those columns. Edit 17-05-2013: See later post on how to update an […]

    Pingback by Spring Roo 8: Uploading images 1 (store images as blob in database) | Raging Goblin — 17 July 2013 @ 20:28 | Reply

  2. Hello. Wonderfull job. All i need, the image, the emun internalization and the login.
    Now i am facing to problem. I can not update my entries. Even if i use merge(); i keep getting and exception
    org.hibernate.PersistentObjectException: detached entity passed to persist: xxx.xxxxx.domain.PersonInformations;

    And when i try to put your part 10 in place :
    CommonsMultipartFile image = logItem.getImage();
    Incompatible type CommonsMultipartFile and byte[]
    I do not have either acces to .setpath() and .getpath()

    Comment by Bakk Manu — 2 April 2016 @ 10:07 | Reply

  3. Heyy,How to create this update controller,I am getting errors

    Comment by nithin v — 29 September 2016 @ 02:55 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: