File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Do a form using Struts. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Do a form using Struts." Watch "Do a form using Struts." New topic
Author

Do a form using Struts.

Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Hi all! I need your advice in this question!

I'm doing a web applicaton and I have a MySQL database. I'm using Spring and Hibernate frameworks. They are working ok. Also I'm using Struts for view layer and I have done some JSP where I show all the information from the data base.

The question is the following:

I need to create a form because, users need to put data into the database. So... I want to do a form similar like this:




I have done this form using Struts, except down list that I have used Javascript but I think that I can't use it.

I have no problems with the fills where I use this kind of tag:
<s:textfield key="anuncio.descripcion"/>.


If I use this kind of tag I can save new data into the database.

The problem is that I don't know how can I create a downlist (or a comboboxlist, I don't mind) and save the information alongside all the fills.

I don't know if I explained me...

PS:

This was my JSP when I didn't use a down list (or combobox)



Also I have done Action class, I have modified struts.xml and it works nice, but now I need to use a down list! hehehe

Sorry for my English and... happy new year!
Raza Mohd
Ranch Hand

Joined: Jan 20, 2010
Posts: 247

hi samuel.

You can use Select strusts tag and you can prepopulate it as per your need.
you can find it here

Thanks n Regards
Raza.

Good luck!!
A small leak can sink a Gigantic ship.>
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Raza Mohd wrote:hi samuel.

You can use Select strusts tag and you can prepopulate it as per your need.
you can find it here

Thanks n Regards
Raza.


Thank you for your advice.

If I use "select" struts tag and I create something like this....



when I execute my web application and I select an option (for example.... "busco") and later I submit my form... I get this error:





I know the problem is that key="anuncio.busco_ofrezco is null but I don't know how fill this key with the option (value) that I have selected

Thank you again!


EDIT: I have been reading more about Struts tag and maybe the problem could be in submit button? I mean... maybe when I click in submit button, the formulary reset himself automaticaly. Is it possible?
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

It looks like the error is actually being thrown by Hibernate:



My suggestion would be to wrap your code in a try-catch block and get a decent stack trace so you can see where the problem occurs, then log the values you are trying to set in the database so you can make sure your code does what you think it does.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
When I try to set the values in the database, I get this error becase "anuncio.busco_ofrezco" is null.

If I use <s:textfield key="anuncio.busco_ofrezco"/> instead of select struts tag, it works nice and I have no problems about Hibernate Nullability.

I have a hibernate problem because when I did my database design, and mi java model design (AnuncioModel.java), I stablished this restriction, I mean... "busco_ofrezco" field can not be null, so if I don't put anything in this field, I get this exception, is normal.

I think that the problem is in select tag. I don't know how get the information from the list that I have declared -- list="{'Busco','Ofrezco'}" -- and save it later in anuncio.busco_ofrezco. I thought that I should use: key="anuncio.busco_ofrezco" to save it.

Also I have read this document: http://struts.apache.org/2.0.6/docs/select.html.
It says that field "key" sets the key (name, value, label) for this particular component. But I don't know how set the key, maybe is the problem...


Sorry for my English and thank you!


EDIT:

Hi buddies! I have good news

If I put this code



instead of this other




it works nice! I don't know why but now I can store the values in the data base.

Thanks for all!
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Samuel Castillo Romero wrote:
it works nice! I don't know why but now I can store the values in the data base.


The problem appears to be the use of the "name" attribute with the "key" attribute. I've seen them used separately: these examples use "name", these examples use "key", but I don't see them being used together. This makes sense because "key" associates a form field with a class variable (as well as message property) and "name" does the same thing and you have them pointing at two different values.
Samuel Castillo Romero
Greenhorn

Joined: Dec 21, 2010
Posts: 26
Joe Ess wrote:
Samuel Castillo Romero wrote:
it works nice! I don't know why but now I can store the values in the data base.


The problem appears to be the use of the "name" attribute with the "key" attribute. I've seen them used separately: these examples use "name", these examples use "key", but I don't see them being used together. This makes sense because "key" associates a form field with a class variable (as well as message property) and "name" does the same thing and you have them pointing at two different values.


Oh!! Thank you for the info!! It will be very usefull for the next time ;)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Do a form using Struts.
 
Similar Threads
problems validation.xml in struts 2
problem designing jsp using struts 2.0 tags
form visibility
How to validate using annotation
Validation Issues