Show Form
project --topLevelPackage org.pragmatikroo.roodocman
persistence setup --provider HIBERNATE --database MYSQL --databaseName roodocman --userName <username> --password <password>
entity  --class ~.domain.Document
field string  --fieldName name   --notNull --sizeMax 30
field string  --fieldName description  --sizeMax 500
field string  --fieldName filename   --notNull 
//field blob  --fieldName content  --notNull //Type not supported by Roo 
field string  --fieldName contentType  --notNull 
field numbert --fieldName size -
controller all  --package ~.web
perform clean
perform eclipse
Code language: HTML, XML (xml)content field is of type blob. This field would have to be entered manually into the entity class. You can use the IDE of your choice. I use STS. Either you use STS or the native Roo shell, the project would need to be synced by Roo after the change. More on this later. package org.pragmatikroo.roodocman;
@RooJavaBean
@RooToString
@RooEntity
public class Document {
    @NotNull
    @Size(max = 30)
    private java.lang.String name;
    @NotNull
    @Size(max = 500)
    private java.lang.String description;
    private java.lang.String filename;
    @NotNull
    @Lob
    @Basic(fetch = FetchType.LAZY)
    private byte[] content;
    private java.lang.String contentType;
    private java.lang.Long size;
    
    @Transient
    @Size(max = 100)
    private String url ;
}
Code language: Java (java)@RooWebScaffold annotation. Please make sure next code is included in the DocumentController project file. @RooWebScaffold(path = "documents", formBackingObject = Document.class, update=false)
@InitBinder
protected void initBinder(HttpServletRequest request, 
           ServletRequestDataBinder binder)
       throws ServletException {
 binder.registerCustomEditor(byte[].class,    newByteArrayMultipartFileEditor());
    }
@RequestMapping(value="savedoc",  method = RequestMethod.POST)
public String createdoc(@Valid Document document,
        BindingResult result, 
        Model model,
        @RequestParam("content") MultipartFile content,
           HttpServletRequest request) {
     
     document.setContentType(content.getContentType());
     document.setFilename(content.getOriginalFilename());
     document.setSize(content.getSize());
     log.debug("Document: ");
     log.debug("Name: "+content.getOriginalFilename());
     log.debug("Description: "+document.getDescription());
     log.debug("File: " +content.getName());
     log.debug("Type: "+content.getContentType());
     log.debug("Size: "+content.getSize());
        if (result.hasErrors()) {
            model.addAttribute("document", document);
            return "documents/create";
        }
        document.persist();
        
        return "redirect:/documents?page=1&size=10" +      encodeUrlPathSegment(document.getId().toString(), request);
    }
    
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public String show(@PathVariable("id") Long id, Model model) {
     Document doc = Document.findDocument(id);
     doc.setU("/documents/showdoc/"+id);
        model.addAttribute("document", Document.findDocument(id));
        model.addAttribute("itemId", id);
        return "documents/show";
    }
    
@RequestMapping(value = "/showdoc/{id}", method = RequestMethod.GET)
public String showdoc( @PathVariable("id") Long id,
        HttpServletResponse response,
        Model model) {
   Document doc = Document.findDocument(id);
             
   try {
          response.setHeader("Content-Disposition", "inline;filename=\"" +doc.getFilename()+ "\"");
          OutputStream out = response.getOutputStream();
          response.setContentType(doc.getContentType());
          IOUtils.copy( new ByteArrayInputStream(doc.getContent()),out);
            out.flush();
         
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
Code language: Java (java)DocumentControler.java file to avoid been removed by Roo when the project is synced for changes. <form:multi id="fc_org_pragmatikroo_roodocman_domain_Document" modelAttribute="document" path="/documents/savedoc" render="${empty dependencies}" z="O7jiRGyhKQOUnBT8yJ9W4XU6VrQ=">
      <field:input field="name" id="c_org_pragmatikroo_roodocman_domain_Document_name" max="30" required="true" z="K3YncHn7F+HtBX/zgCZMYM6VO7k="/>
      <field:textarea field="description" id="c_org_pragmatikroo_roodocman_domain_Document_description" required="true" z="/RLpJWAf9zVjHtRapWIFve8JB8Y="/>
      <field:input field="filename" id="c_org_pragmatikroo_roodocman_domain_Document_filename" render="false" z="u4EclOEjQnID4g34VPv9zu9+qPo="/>
      <field:file field="content" id="c_org_pragmatikroo_roodocman_domain_Document_content" required="true" z="GZHlfc+o4h7EBA/SZ8/yXMVenOw="/>
      <field:input field="contentType" id="c_org_pragmatikroo_roodocman_domain_Document_contentType" render="false" z="jtq5/DHhBTgNImjac/d4AIfpCzA="/>
      <field:input field="size" id="c_org_pragmatikroo_roodocman_domain_Document_size" render="false" validationMessageCode="field_invalid_integer" z="ONDmhU5Eg5pw1j8LgTz9sXjOm6E="/>
      <field:textarea field="url" id="c_org_pragmatikroo_roodocman_domain_Document_url" render="false" z="bQ3FUVGL01nfWselK7WDPUD65Rw="/>
</form:multi>
Code language: HTML, XML (xml)Create Form
<page:show id="ps_org_pragmatikroo_roodocman_domain_Document" object="${document}" path="/documents" update="false" z="J8X2O2T06x6jYb3WAyaNiTPGVZ0=">
      <field:display field="name" id="s_org_pragmatikroo_roodocman_domain_Document_name" object="${document}" z="VZEPJgXqYkqaHpDDPTcYr6DAjsM="/>
      <field:display field="description" id="s_org_pragmatikroo_roodocman_domain_Document_description" object="${document}" z="CeXQhSyEDeLn1Iu2hblanNbVc+A="/>
      <field:display field="filename" id="s_org_pragmatikroo_roodocman_domain_Document_filename" object="${document}" z="2HT3+zT1+1ft5kN4KhvXFWT9QnM="/>
      <field:display field="contentType" id="s_org_pragmatikroo_roodocman_domain_Document_contentType" object="${document}" z="FgCNUQ2KCygzKpUbP06Nxyjyfd8="/>
      <field:display field="size" id="s_org_pragmatikroo_roodocman_domain_Document_size" object="${document}" z="WdQ4wob2LThzMpucODZRa275TBc="/>
      <field:frame field="url" id="s_org_pragmatikroo_roodocman_domain_Document_url" object="${document}" render="true" z="user-managed"/>
</page:show>
Code language: HTML, XML (xml)Show Form
List Form
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.0.1</version>
</dependency>
Code language: HTML, XML (xml)Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…
Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…
Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…
1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…
GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…
1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…
View Comments
Thanks for sharing!
Might merge this into my project(s) and if I might find useful stuff to be added, I'll be back. :)
grts
Jochen, publish the link when possible... It will be great to see it working on other projects.
If you want to see it working now. Visit the WorldAlmanac on the Cities Menu.
Cheers jD
Jochen,
It is the Document Document showcase...
Thx
jD
Thank you! I was looking for it for a while and I think your post is the only place to get this information! Thanks for sharing!
Thanks very much fpr this great sample ! :D
Great tutorial. Thanks! One question though. Will the browser cache the image given that it doesn't change?
Web site is very useful for me but i want this application in struts
can help me?
Faroo,
I suggest you review article in this link http://viralpatel.net/tutorial-save-get-blob-object-spring-3-mvc-hibernate/. It is closer to an struts implementation. Or just
google "struts file uploads" you will tons of implementations out there.
Thx
jD
Hi JD ,
I saw u r message but i want retrive image from database and display in jsp using form,DAO,DO i am tring to display but some thing is wrong .i got the imgdata but how can i display in jsp ,i saw google they r using but they r also cant display img so please send me some source code to me plz help me?
ThX®ards
----------------
faroo
Hi Jd,
the above application display the imgage from database how can i display in my jsp like above page i am also using same fields and same data but retriving data success but how can i display
Good tutorial. When i try it , I have error with multi-tagx et file-tagx file , I don't know the content of this file .
Thanks for your help
@ jac have you been able to s0lve this issue i am having internal error because of the multi tag for form too