Announcement Announcement Module
Collapse
No announcement yet.
Implicitly setting an entity field on POST Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Implicitly setting an entity field on POST

    I have created a simple project:

    project --topLevelPackage com.me.sample --projectName HideDateTest
    persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
    entity --class ~.bizlogic.Note
    field string --fieldName mynote --notNull
    field date --fieldName datenoted --type java.util.Date --notNull
    controller all --package ~.web
    I want the "datenoted" field to be automatically set by the server when it receives a post request.

    I modified src/main/webapp/WEB-INF/views/notes/create.jspx to comment out the field:datetime line:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <div xmlns:c="http://java.sun.com/jsp/jstl/core" 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" xmlns:spring="http://www.springframework.org/tags" version="2.0">
    <jsp:output omit-xml-declaration="yes"/>
    <form:create id="fc_com_me_sample_bizlogic_Note" modelAttribute="note" path="/notes" render="${empty dependencies}" z="WGwma68nLsNrRuliGQO2oGmVPQA=">
    <field:input field="mynote" id="c_com_me_sample_bizlogic_Note_mynote" required="true" z="IIzti0XOuhy8deKLB3TliEgGYL0="/>
    <!-- <field:datetime dateTimePattern="${note_datenoted_date_format}" field="datenoted" id="c_com_me_sample_bizlogic_Note_datenoted" required="true" z="E8lzDhNlqAPinRW28ymkwO1gyMk="/> -->
    </form:create>
    <form:dependency dependencies="${dependencies}" id="d_com_me_sample_bizlogic_Note" render="${not empty dependencies}" z="Wchem3NfWbtZotzuXcKsoxr9hIc="/>
    </div>
    I then modified src/main/java/com/me/sample/web/NoteController_Roo_Controller.aj to automatically set the date:

    @RequestMapping(method = RequestMethod.POST)
    public String NoteController.create(@Valid Note note, BindingResult result, ModelMap modelMap) {
    System.err.println("Inside create function.");
    if (result.hasErrors()) {
    System.err.println("NoteController_Roo_Controller. aj::create() bindingresult has errors!.");
    modelMap.addAttribute("note", note);
    addDateTimeFormatPatterns(modelMap);
    return "notes/create";
    }
    note.datenoted = new Date();
    note.persist();
    return "redirect:/notes/" + note.getId();
    }
    However, it is running the BindingResult's "result.hasErrors()" and never gets to the persist code. What do I need to edit to fix this? I tried to add a hidden field, but I did it wrong:

    <field:hidden field="datenoted" id="c_com_me_sample_bizlogic_Note_datenoted" required="true" z=""/>
    SEVERE: Servlet.service() for servlet jsp threw exception
    org.xml.sax.SAXException: No tag "hidden" defined in tag library associated with uri "urn:jsptagdir:/WEB-INF/tags/form/fields"
    Also, am I suppose to be editing NoteController_Roo_Controller.aj, or is this file suppose to be managed by the roo shell? (I don't want my changes overwritten in the future.)

    Thank you so much for your time.

  • #2
    easy fix

    field date --fieldName datenoted --type java.util.Date
    I had to remove the --notNull and now it works. Silly me.

    Comment

    Working...
    X