问题描述:
POJO
public class Book  implements Serializable {
    // Fields         private String isbn;
     private Type type;//jsp页面怎么传值到Action
     private String bookName;
     private String publisher;
     private String author;
     private String introduce;
     private Date publishDate;
   
.....
}映射文件
<hibernate-mapping package="xiangnan.yangting.bookshop.model">
    <class name="Book" table="tb_book" catalog="bluedotbookshop">
        <id name="isbn" type="string">
            <column name="ISBN" length="20" />
            <generator class="native"></generator>
        </id>
        <many-to-one name="type" class="Type" fetch="select">
            <column name="typeID" not-null="true" />
        </many-to-one>

        <property name="bookName" type="string">
            <column name="bookName" length="50" not-null="true" />
        </property>
        <property name="publisher" type="string">
            <column name="publisher" length="50" not-null="true" />
        </property>
...
    </class>
</hibernate-mapping>Action
public class AddBookAction extends ActionSupport
{
private Book book;
private BookService bookService;
        public Book getBook()
{
return book;
}
public void setBook(Book book)
{
this.book = book;
}
        public BookService getBookService()
{
return bookService;
}
public void setBookService(BookService bookService)
{
this.bookService = bookService;
}
....
}jsp页面
...
<td class="altbg1" width="21%">书籍的ISBN</td>
<td class="altbg2"><input type="text" name="book.isbn"> 
</tr><tr>
<td class="altbg1" width="21%">书籍名称</td>
<td class="altbg2"><input type="text" name="book.bookName"></td>
</tr><tr>
<td class="altbg1" width="21%">书籍类别</td>
<td class="altbg2">
<select name="?"><!--这个type(Type)怎么传值??-->
<option value=0 selected>请选择</option>
<option value=3>程序语言</option>
<option value=4>操作系统</option>
<option value=5>办公软件</option>
<option value=6>计算机硬件</option>
<option value=7>计算机网络</option>
</select>
</td>
</tr>
....

解决方案 »

  1.   

    需要写转换器,Struts1要用到BeanUtil原始方法来做,Spring MVC有自己的属性编辑器。
    Struts2增加了TypeCoverter接口或DefaultTypeConverter抽象类来实现自定义的转换器,其步骤如下:第一步:将这个类的toString方法重写,里面输出自己的id(一般实体bean都有id,id是实体的灵魂)
    第二步:实现一个自定义的Type类型转换器,
    第三步:将上述自定义的转换器注册到Web应用中。
    详细资料见http://aumy2008.javaeye.com/blog/150202
      

  2.   

    <select name="type.typeID"> 
      

  3.   

    <select name="book。type.typeID"> 
      

  4.   

    还是不能保存到数据库O_O,返回“input”对应的视图
      

  5.   

    org.hibernate.PropertyValueException: not-null property references a null or transient value: xiangnan.yangting.bookshop.model.Book.type
    主要错误信息
      

  6.   

    not-null property references a null or transient value: xiangnan.yangting.bookshop.model.Book.type 
    后台打印下Book.type 是否存在值?
      

  7.   

    哎,时间比较紧,换了种方法去实现了。thank you all the same!
      

  8.   

    我直接把typeName加到tb_book表中了,抛弃了tb_type表O_O,先把功能实现再说,呵呵!
      

  9.   

    唉!我也遇到问题了。给位同胞帮我看看啊  我在jsp页面里的实体属性的值在 action中一个都取不到啊!
    <form id="form1" name="Gprsdtu" method="post" action="Gprsdtu!addGprsdtu.action" onSubmit="return check()">
      <table width="562" border="1">
        <tr>
          <td width="101">手机卡号:</td>
          <td colspan="2"><input type="text" id="dtusIm" name="dtuSimId" /></td>
        </tr>
        <tr>
          <td>gprsid:</td>
          <td colspan="2"><input type="text" id="dtugprsId" name="dtugprsId" /></td>
        </tr>
        <tr>
          <td>所属公司:</td>
          <td colspan="2"><select id="dtucpId" name="dtucpId" style="width:160px">
         <c:forEach var="companys" items="${gcompanylist}">
          <option value="${companys.cpId }">${companys.cpName }</option>
          </c:forEach>
          </select></td>
        </tr> private Integer dtuSimId;
    private String dtuEndName;
    private Timestamp dtuStateTime;
    private String dtuAddress;
    private Boolean dtuState;
    private Timestamp dtuUpdateTime;
    public Integer getDtuSimId() {
    return this.dtuSimId;
    } public void setDtuSimId(Integer dtuSimId) {
    this.dtuSimId = dtuSimId;
    } public String getDtuEndName() {
    return this.dtuEndName;
    } public void setDtuEndName(String dtuEndName) {
    this.dtuEndName = dtuEndName;
    } public Timestamp getDtuStateTime() {
    return this.dtuStateTime;
    } public void setDtuStateTime(Timestamp dtuStateTime) {
    this.dtuStateTime = dtuStateTime;
    } public String getDtuAddress() {
    return this.dtuAddress;
    }
    这个只是一部分 但是都取不到