Hi Harps,

  Thanks for your reply, i written the UserConverter class as you said, but
i am unable to find out the template/simple directory and the fileName
called select.ftl

   I run the project without the  select.ftl file and try to add or update
the record then it gives an error like  Invalid field value for field
"purchase.user"..
  
   I found xwork-conversion.properties file and it contains the code like
java.util.Date=org.appfuse.webapp.action.DateConverter is there any need to
write the UserConverter in this file.

   Please suggest me and can you please explain me what is the .ftl files
and uses or suggest any tutorials on that one.


Harps wrote:
> 
> Hi Sudhakar,
> 
> This is because of how struts 2 works. You are updating the user objects
> id field so it thinks it needs to save the User object when it should not
> be. It should only be saving the Purchase object.
> 
> <s:select name="purchase.user.id" list="users" listKey="id"
> listValue="username"></s:select>
> 
> should be
> 
> <s:select name="purchase.user" list="users" listKey="id"
> listValue="username"></s:select>
> 
> ie remove the .id from the name
> 
> Then you need to write a converter for the user object. You may want to
> take a look at the existing DateConverter class. The User one will be
> something like the following
> 
>  public class UserConverter extends StrutsTypeConverter {
>        private UserManager userManager;
>       
>        public void setUserManager(UserManager userManager) {
>                this.userManager = userManager;
>        }
>       
>        public Object convertFromString(Map map, String[] value, Class aClass) 
> {
>                if (value[0] == null || value[0].trim().equals("")) {
>                        return null;
>                }
>               
>                try {
>                        return userManager.getUser(value[0]);
>                } catch (Exception e) {
>                        e.printStackTrace();
>                        throw new TypeConversionException(e.getMessage());
>                }
>       
>        }
>       
>        public String convertToString(Map map, Object o) {
>                return String.valueOf(((User) o).getId());
>        }
>  }
> 
> So now saving should work correctly, however, when displaying the record
> for editing it will not display your user. ie it will just display the
> first record in the drop down user list. 
> 
> This is a bug in select.ftl which is part of struts 2. I hacked this to
> fix it temporarily, I believe they may have fixed it in struts2 HEAD but I
> am not positive. To hack it you need to grab that file out of the
> struts2-core jar file. Stick that under your web app -> template -> simple
> directory in order to override it. Then change the part that writes the
> options out as html. I changed it to the following and it seems to work
> but I by no means am saying that this is the correct way to do it.
> 
> 
> <option value="${itemKeyStr?html}"<#rt/>
>       <#if parameters.nameValue??>
>               <#if tag.contains(parameters.nameValue.id, itemKey) == true>
>                       selected="selected"<#rt/>
>               </#if>
>               <#if tag.contains(parameters.nameValue, itemKey) == true>
>                       selected="selected"<#rt/>
>               </#if>
>       </#if>
>     >${itemValue?html}</option><#lt/>
> 
> Hope this helps,
> Harps.
> 
> 
> sudhakargupta wrote:
>> 
>> Hi Matt,
>> 
>>  In appfuse2 struts i written the many-to-one relationship code like this
>> 
>> Purchase.java
>> 
>> @ManyToOne (targetEntity=User.class)
>> @JoinColumn (name="user_id", nullable=flase)
>> private User user;
>> 
>> PurchaseAction.java: (appended code)
>> 
>> public List getUsers() {
>>    return userManager.getUsers(new User());
>> }
>> 
>> purchaseForm.jsp: (Generated by appfuse:gen)
>> 
>> <s:select name="purchase.user.id" list="users" listKey="id"
>> listValue="username"></s:select>
>> 
>>  It gives an error when add a new record 
>> 
>> object references an unsaved transient instance - save the transient
>> instance before flushing: com.ozonetel.sms.model.User; nested exception
>> is org.hibernate.TransientObjectException: object references an unsaved
>> transient instance - save the transient instance before flushing:
>> com.ozonetel.sms.model.User 
>> 
>>  It works fine when update the already existed record
>>  please suggest me how to solve this problem
>>    
>> 
>> 
> 
> 


-----
---
Sudhakar
-- 
View this message in context: 
http://www.nabble.com/Struts2-Many-to-one-New-Record-Save-problem-%28JSP-Drop-down%29-tp23442385s2369p23477491.html
Sent from the AppFuse - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@appfuse.dev.java.net
For additional commands, e-mail: users-h...@appfuse.dev.java.net

Reply via email to