views:

315

answers:

1

i have 2 table:

message(id, name, content, channel_number) // channel_number is foreign key
channel(number, name) // number is primary key

i use hibernate to map 2 table

java class

public class Message {

 private Integer id;
 private String name;
    private String content;
 private Channel channel;
}

public class Channel {

 private Integer number;
 private String name;
}

hibernate config

<class name="Message" table="message">
  <id name="id" column="id">
   <generator class="native" />
  </id>
  <property name="name" column="name" />
  <property name="content" column="content" />
  <many-to-one name="channel" column="channel_number" not-null="true" />
 </class>

 <class name="Channel" table="channel">
  <id name="number" />
  <property name="name" />
 </class>

in spring, i have form to create/edit message. i have a select box to choose a channel. So, i load all channels in controller & show in view

<form:form commandName="message" method="post" action="messageForm.htm">
    ...
    <form:select path="channel" items="${channelList}" itemValue="number" itemLabel="name"/>

</form:form>

when i press submit, nothing happen, it's still in jsp page & no redirect to onSubmit method (everything work well before i add this select)

A: 

Any value that you pass through the form should be a string or integer. You can't submit an channel object on form. An other reason is that you can't get object from request. In the servlet request.getParameter() returns String.

What you want assigned to the value attribute is some sort of ID that you can then use to reference the appropriate Channel object. If it's a number, it can be a primitive int, Integer, or String representation - so long as you map it to a command object property that is of type Integer or String. In other words, channel needs to be Integer or String. You should probably rename it to channelID just to be clear.

Then put that int channelID variable into your Message POJO as well. In the controller , you can create a channel object using that selected channel ID and set that channel object to created message object. For an example, in controller class:

    Message message = (Message ) command;
    Channel channel= new Channel();
    channel.setChannelID(message.getChannelID());
    message.setChannel(channel);

This worked for me when I got this same problem. This thread explains the problem in more details. http://forum.springsource.org/showthread.php?t=33825

Hope this help.

arlahiru