ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:838回复:0

default value for form:select

楼主#
更多 发布于:2016-05-12 14:42
In my current spring project, I have a form which have this input field:                     <div class="panel panel-default">
                        <div class="panel-heading">
                            <form:label path="${entry.key}">${entry.key}</form:label>
                        </div>
                        <div class="panel-body">
                            <form:select path="${entry.key}.id" class="form-control">
                                <form:options items="${values[entry.key]}" itemValue="id" itemLabel="nome"/>
                            </form:select>
                        </div>
                    </div>
 I want add to this a default value, in case the user don't want select an option (the atribute can be null in the database). I try this: 1- <form:option value="-" label="--Please Select"/>
 2- <form:option value="" label="--Please Select"/>
 3- <form:option value="" label="--Please Select" disabled="disabled"/>
 but none of this options work (I get an error org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.spring.loja.model.categoria.persistence.model.Categoria
 when I run the application). Anyone can tell me what the right way to accomplish this? UPDATE 2 of the entity classes from my project. The code above works fine with the first one (Product), but I get the error TransientObjectException when I try insert the second one (Order). Entity class: Product @Entity
@Table(name="produto")
public class Produto {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="nome", unique=true)
    @Order(value=1)
    private String nome;

    @Column(name="preco")
    @Order(value=2)
    private Float preco;

    @OneToOne
    @JoinColumn(name="categoria")
    @Order(value=3)
    private Categoria categoria;

    @Column(name="resumo", length=140)
    @Order(value=4)
    private String resumo;

    @Column(name="descricao", length=65535)
    @Order(value=5)
    private String descricao;
}
 entity class: Order @Entity
@Table(name="pedido")
public class Pedido {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name="cliente")
    @Order(value=1)
    private Cliente cliente;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name="produto")
    @Order(value=2)
    private Produto produto;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name="cobranca")
    @Order(value=3)
    private Cobranca cobranca;
}
 UPDATE 2 I try this:                             <form:select path="${entry.key}.id" class="form-control">
                                <form:option value="" label="--Please Select"/>
                                <form:options items="${values[entry.key]}"></form:options>
                            </form:select>
 In this way, I get an error when I try insert a new value. With this:                             <form:select path="${entry.key}" class="form-control">
                                <form:option value="" label="--Please Select"/>
                                <form:options items="${values[entry.key]}"></form:options>
                            </form:select>
 I can insert a new item ONLY with when no option is selected. I try this for this entity class: @Entity
@Table(name="pagina")
public class Pagina {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name = "nome", unique=true, nullable=false, length=32)
    @Order(value=1)
    private String nome;

    @Column(name = "titulo", nullable=false, length=32)
    @Order(value=2)
    private String titulo;

    @Column(name="resumo", length=140)
    @Order(value=3)
    private String resumo;

    @Column(name = "descricao", length=65535)
    @Order(value=4)
    private String descricao;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, optional=true)
    @JoinColumn(name="pagina")
    @Order(value=5)
    private Pagina pagina;

    @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.ALL}, optional=true)
    @JoinColumn(name="produto")
    @Order(value=6)
    private Produto produto;
}
 Anyone can see what's wrong here?I am not sure the cause of the TransientObjectException, but below is a simple example for how to set default value to form:select. Hope this helps. JSP: <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>example</title>
</head>
<body>
  <form:form modelAttribute="myForm" method="post">
    <form:select path="valueOfSelectElement">
      <form:option value="default-value" label="--Please Select" />
      <form:options items="${options}" itemValue="id" itemLabel="name" />
    </form:select>
    <input type="submit" />
  </form:form>
</body>
</html>
 Controller: package org.myorg.myapp;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

  @RequestMapping(value = "/form", method = RequestMethod.GET)
  public ModelAndView showForm() {
    ModelAndView mav = new ModelAndView("form");
    List<IdAndName> options = Arrays.asList(
        new IdAndName("id1", "name1"),
        new IdAndName("id2", "name2"),
        new IdAndName("id3", "name3")
    );
    mav.addObject("options", options);

    MyForm myForm = new MyForm();
    myForm.setValueOfSelectElement("default-value");
    mav.addObject("myForm", myForm);

    return mav;
  }

  @RequestMapping(value = "/form", method = RequestMethod.POST)
  @ResponseBody
  public String processForm(MyForm myForm) {
    return myForm.getValueOfSelectElement();
  }

  static class MyForm {
    private String valueOfSelectElement;

    public String getValueOfSelectElement() {
      return valueOfSelectElement;
    }

    public void setValueOfSelectElement(String valueOfSelectElement) {
      this.valueOfSelectElement = valueOfSelectElement;
    }
  }

  static class IdAndName {
    private String id;

    private String name;

    public IdAndName(String id, String name) {
      this.id = id;
      this.name = name;
    }

    public String getId() {
      return id;
    }

    public void setId(String id) {
      this.id = id;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }
  }
}

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部