Announcement Announcement Module
Collapse
No announcement yet.
Forms Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forms

    Hi,
    I have some problems in submitting a form.
    I have two classe:
    1-Invoices:
    public class Invoice extends BaseObject implements Serializable {

    private Long id;
    private Integer invoiceNumber;
    private Customer customer;
    private Date date;
    private Set itemLines = new HashSet();
    private double totalPrice;


    /**
    * @return Returns the id.
    *
    * @hibernate.id column = "id"
    * generator-class = "native" unsaved-value = "null"
    */
    public Long getId() {
    return id;
    }
    /**
    * @param id The id to set.
    */
    public void setId(Long id) {
    this.id = id;
    }

    /**
    * @return Returns the customer.
    *
    * @hibernate.many-to-one column = "customer" class = "com._4s_.invoice.model.Customer"
    */
    public Customer getCustomer() {
    return customer;
    }

    /**
    * @param customer The customer to set.
    */
    public void setCustomer(Customer customer) {
    this.customer = customer;
    }

    /**
    * @return Returns the date.
    *
    * @hibernate.property column = "invoicedate"
    */
    public Date getDate() {
    return date;
    }
    /**
    * @param date The date to set.
    */
    public void setDate(Date date) {
    this.date = date;
    }

    /**
    * @return Returns the invoiceNumber.
    *
    * @hibernate.property column = "invoicenumber" unique = "true"
    */
    public Integer getInvoiceNumber() {
    return invoiceNumber;
    }
    /**
    * @param invoiceNumber The invoiceNumber to set.
    */
    public void setInvoiceNumber(Integer invoiceNumber) {
    this.invoiceNumber = invoiceNumber;
    }

    /**
    * @return Returns the itemLines.
    *
    * @hibernate.set table = "itemline" cascade = "save-update" lazy = "true" batch-size = "1" inverse = "true"
    * @hibernate.collection-key column = "id"
    * @hibernate.collection-one-to-many class = "com._4s_.invoice.model.ItemLine"
    */
    public Set getItemLines() {
    return itemLines;
    }
    public void addItemLine(ItemLine itemLine) {
    itemLine.setInvoice(this);
    itemLines.add(itemLine);
    }
    /**
    * @param itemLines The itemLines to set.
    */
    public void setItemLines(Set itemLines) {
    this.itemLines = itemLines;
    }

    /**
    * @return Returns the totalPrice.
    *
    * @hibernate.property column = "totalprice"
    */
    public double getTotalPrice() {
    return totalPrice;
    }
    /**
    * @param totalPrice The totalPrice to set.
    */
    public void setTotalPrice(double totalPrice) {
    this.totalPrice = totalPrice;
    }
    public void calculateTotalPrice() {
    double invoiceTotal = 0 ;
    ItemLine itemLine = null;

    Iterator i = itemLines.iterator();
    while(i.hasNext()) {
    itemLine = (ItemLine) i.next();
    invoiceTotal += itemLine.getTotalPrice();
    }

    this.totalPrice = invoiceTotal;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    public String toString() {
    // TODO Auto-generated method stub
    return null;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
    public boolean equals(Object o) {
    // TODO Auto-generated method stub
    return false;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
    public int hashCode() {
    // TODO Auto-generated method stub
    return 0;
    }
    }

    2-Customers:

    public class Customer extends BaseObject implements Serializable {

    private Long id;

    private String code;

    private String firstName;
    private String lastName;
    private String initials;

    private String address1;
    private String address2;

    private Set invoices = new HashSet();

    /**
    * @return Returns the id.
    *
    * @hibernate.id column = "id"
    * generator-class = "native" unsaved-value = "null"
    */
    public Long getId() {
    return id;
    }
    /**
    * @param id The id to set.
    */
    public void setId(Long id) {
    this.id = id;
    }


    /**
    * @return Returns the code.
    *
    * @hibernate.property column = "code" unique = "true"
    */
    public String getCode() {
    return code;
    }
    /**
    * @param code The code to set.
    */
    public void setCode(String code) {
    this.code = code;
    }

    /**
    * @return Returns the firstName.
    *
    * @hibernate.property column = "firstname" length = "50"
    */
    public String getFirstName() {
    return firstName;
    }
    /**
    * @param firstName The firstName to set.
    */
    public void setFirstName(String firstName) {
    this.firstName = firstName;
    }

    /**
    * @return Returns the initials.
    *
    * @hibernate.property column = "initials" length = "5"
    */
    public String getInitials() {
    return initials;
    }
    /**
    * @param initials The initials to set.
    */
    public void setInitials(String initials) {
    this.initials = initials;
    }

    /**
    * @return Returns the lastName.
    *
    * @hibernate.property column = "lastname" length = "50"
    */
    public String getLastName() {
    return lastName;
    }
    /**
    * @param lastName The lastName to set.
    */
    public void setLastName(String lastName) {
    this.lastName = lastName;
    }

    /**
    * @return Returns the address1.
    *
    * @hibernate.property column = "address1" length = "100"
    */
    public String getAddress1() {
    return address1;
    }
    /**
    * @param address1 The address1 to set.
    */
    public void setAddress1(String address1) {
    this.address1 = address1;
    }

    /**
    * @return Returns the address2.
    *
    * @hibernate.property column = "address2" length = "100"
    */
    public String getAddress2() {
    return address2;
    }
    /**
    * @param address2 The address2 to set.
    */
    public void setAddress2(String address2) {
    this.address2 = address2;
    }

    /**
    * @return Returns the invoices.
    *
    * @hibernate.set table = "invoice" cascade = "save-update" lazy = "true" inverse = "true"
    * @hibernate.collection-key column = "customer"
    * @hibernate.collection-one-to-many class = "com._4s_.invoice.model.Invoice"
    */
    public Set getInvoices() {
    return invoices;
    }
    /**
    * @param invoices The invoices to set.
    */
    public void setInvoices(Set invoices) {
    this.invoices = invoices;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    public String toString() {
    // TODO Auto-generated method stub
    return null;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#equals(java.lang.Object)
    */
    public boolean equals(Object o) {
    // TODO Auto-generated method stub
    return false;
    }

    /* (non-Javadoc)
    * @see java.lang.Object#hashCode()
    */
    public int hashCode() {
    // TODO Auto-generated method stub
    return 0;
    }

    }

    I created a form to add a new invoice.
    In this form I added a drop down list of the customers so that the user could select on of the existing customers while adding the invoice.

    Here is the form controller:

    public class AddInvoiceFormController extends SimpleFormController {
    private static Log log = LogFactory.getLog(AddInvoiceFormController.class);

    private InvoiceManager mgr = null;
    private CustomerManager customerManager = null;

    public void setInvoiceManager(InvoiceManager invoiceManager){
    this.mgr = invoiceManager;
    }

    public InvoiceManager getInvoiceManager(){
    return this.mgr;
    }

    public void setCustomerManager(CustomerManager customerManager){
    this.customerManager = customerManager;
    }

    public CustomerManager getCustomerManager(){
    return this.customerManager;
    }

    /**
    * *Set up a custom property editor for converting Longs
    *
    */
    public void initBinder(HttpServletRequest request,
    ServletRequestDataBinder binder){
    NumberFormat nf = NumberFormat.getNumberInstance();
    binder.registerCustomEditor(Long.class, null, new CustomNumberEditor(Long.class, nf,true));
    }

    public ModelAndView onSubmit(HttpServletRequest request,
    HttpServletResponse response,
    Object command, BindException errors)
    throws Exception{
    if (log.isDebugEnabled()){
    log.debug("entering 'onSubmit' method....");
    }
    Invoice invoice = (Invoice) command;

    mgr.saveInvoice(invoice);
    request.getSession().setAttribute("message",
    getMessageSourceAccessor().getMessage("invoice.sav ed",
    new Object[] {invoice.getInvoiceNumber().toString() +
    ' ' + invoice.getCustomer() +
    ' ' + invoice.getDate()}));

    return new ModelAndView(new RedirectView(getSuccessView()));
    }

    protected Object formBackingObject (HttpServletRequest request)
    throws ServletException{
    String id = request.getParameter("id");

    if ((id != null) && !id.equals("")) {
    return mgr.getInvoice( new Long (request.getParameter("id")));
    }else {
    return new Invoice();
    }
    }

    protected Map referenceData(HttpServletRequest request)
    throws ServletException {
    Map model = new HashMap();
    model.put("customers", customerManager.getCustomers());
    return model;
    }
    }


    Here is the jsp page of the form:

    <%@ include file="/taglibs.jsp"%>
    <html>
    <title>Add Invoice</title>

    <p>Please fill in the invoice details:</p>

    <form method="POST">

    <spring:bind path="invoice.id">
    <input type="hidden" name="id" value="${status.value}"/>
    </spring:bind>

    <table>
    <tr>
    <td>Invoice-Number:</td>
    <td>
    <spring:bind path="invoice.invoiceNumber">
    <input type="text" name="invoiceNumber" value="${status.value}"/>
    </spring:bind>
    </td>
    </tr>

    <tr>
    <td>Customer:</td>
    <td>
    <spring:bind path="invoice.customer">
    <select name="customers">
    <option value="-1">-- Select --</option>
    <c:forEach var="customer" items="${customers}">
    <option value="<c:out value="${customer.id}"/>"
    <c:if test="${customer.id == status.value}"> selected="selected"</c:if>
    >
    <c:out value="${customer.firstName}"/>
    </option>
    </c:forEach>
    </select>
    </spring:bind>
    </td>

    </tr>

    <tr>
    <td>Date:</td>
    <td>
    <spring:bind path="invoice.date">
    <input type="text" name="date" value="${status.value}"/>
    </spring:bind>
    </td>
    </tr>

    <tr>
    <td>Total Price:</td>
    <td>
    <spring:bind path="invoice.totalPrice">
    <input type="text" name="totalPrice" value="${status.value}"/>
    </spring:bind>
    </td>
    </tr>

    </table>

    <p><input type="submit" name="submit" value="Submit"/>
    </form>
    </html>


    Here is the action-servlet.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>

    <bean id="invoiceController" class="com._4s_.invoice.web.action.InvoiceControll er">
    <property name="invoiceManager">
    <ref bean="invoiceManager"/>
    </property>
    </bean>

    <bean id="addInvoiceFormController" class="com._4s_.invoice.web.action.AddInvoiceFormC ontroller">
    <property name="commandName"><value>invoice</value></property>
    <property name="commandClass">
    <value>com._4s_.invoice.model.Invoice</value>
    </property>
    <property name="formView"><value>addInvoiceForm</value></property>
    <property name="successView"><value>invoices.html</value></property>
    <property name="invoiceManager"><ref bean="invoiceManager"/></property>
    <property name="customerManager"><ref bean="customerManager"/></property>
    </bean>


    <!-- Load WEB-INF/classes/messages.properties for i18n messages using JSTL's fmt tag -->
    <bean id="messageSource"
    class="org.springframework.context.support.Resourc eBundleMessageSource">
    <property name="basename"><value>ApplicationResources</value></property>
    </bean>

    <bean id="urlMapping"
    class="org.springframework.web.servlet.handler.Sim pleUrlHandlerMapping">
    <property name="mappings">
    <props>
    <prop key="/invoices.html">invoiceController</prop>
    <prop key="/editInvoice.html">addInvoiceFormController</prop>
    </props>
    </property>
    </bean>

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver">
    <property name="requestContextAttribute"><value>rc</value></property>
    <property name="viewClass">
    <value>org.springframework.web.servlet.view.JstlVi ew</value>
    </property>
    <property name="prefix"><value>/WEB-INF/jsp/</value></property>
    <property name="suffix"><value>.jsp</value></property>
    </bean>




    </beans>


    On Submitting the form nothing occurs.
    I think that the problem is related to the list of customers but I'm not sure.
    Could anyone help me in that?


    Thanks in Advance.
    Sherihan.

  • #2
    Sherihan, posting a mass of code and config followed by what basically amounts to "why doesn't it work?" isn't going to buy you much in the way of help. I would respectfully suggest reading http://www.catb.org/~esr/faqs/smart-...ns.html#volume (please note, don't subsequently ask them for the answer either!)

    Regards,

    Comment


    • #3
      Sherihan, while I tried to build your code, I found you didnt' provide code for ItemLIne, InvoiceManager, CustomerManager etc. No way to give you some hint. :?

      Comment


      • #4
        Its hard to know what your problem is - you just say that nothing happens. A bit more information on what you expect to happen wouldn't go amiss.

        But I did spot a typo in your source:

        Code:
        <spring&#58;bind path="invoice.customer"> 
        <select name="customers">
        Customer will never be bound as the select has the wrong name. Always favour using ${status.expression} (see the javadoc for BindStatus) rather than naming the field directly to prevent this kind of error.

        Comment


        • #5
          Setting Date in Forms (Binding)

          i think i may have a problem like above

          is there a specail way of setting date in Bean Object

          if you have an Object
          Code:
          import java.util.Date
          
          public class Myclass{
           private Date myDate;
           public Date getTheDate(){ retrun myDate}
           public void setTheDate(Date input){ myDate = input; }
          }
          but on the JSP Page i have

          Code:
           <Spring:bind path="FormBean.TheDate">
                                                  <input style="width=100%" type="text" name="theDate" id="theDate" value="${status.value}"/>
          </Spring:bind>
          i don't have any validation set or anything specail. when i click on submit, it seems the request doesn't make it to my controller, but No error is pushed out to the Logger or screen.

          Suggestions anyone

          Comment

          Working...
          X