• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java UseBean setting all properties not working  RSS feed

 
Rahul Chaitanya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am trying to set all the properties of a bean from JSP form. Below is my code and for some reason, it isn't working:

My JSP:

<jsp:useBean id="user" class="user.UserData" scope="session">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>


<form name="test1" action="testing" method="post">
<select name="queryType1">
<c:forEach items="${CONST.SEARCH_OPTIONS}" var="szenario">
<option value="${szenario}">${szenario}</option>
</c:forEach>
</select>

<jsp:setProperty name="user" property="queryType2" value="reached servlet"/>
<INPUT type="submit"/>

</form>

In My Servlet:
UserData input = (UserData)session.getAttribute("user");

if I do:
input.getQueryType1 is always getting null
but if I do:
input.getQueryType2 is correctly getting the value: "reached servlet".

Both the above attributes had setter and getter methods.

Is there anyway that I can capture the Select box value into the bean attribute ? Am I missing anything ?
Since: <jsp:setProperty name="user" property="*"/> should set all the values of a bean from html form.

Please, suggest (I am using Tomcat 6.0)

 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<select name="queryType1"> does not bind the property to the bean (UserData).

You would have to set the properties of the bean by reading the request object's parameter map.
 
Rahul Chaitanya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you let me know, how to set the form properties to the bean in the JSP itself ??
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. You are submitting to a servlet, so you need to gather the params there. Putting a useBean in the JSP that executes before the form is even shown to the user is senseless.

useBean is a dinosaur left over from the days of JSP1. Its use is not needed here.

If you want to bind to a bean in the servlet, grab the BeanUtils 3rd party library.
 
Rahul Chaitanya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I am currently making changes to a very old application and so had to use <use:beans> along with forms.

And so, is there a way to write a Java Script function, that submits the form to the same jsp, that sets the UseBean values and then within the same function, submit the form back to servlet ? Is this possible ?

Please suggest Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a huge clunky Rube Goldberg machine. Why over-complicate things like that?

Just use BeanUtils in the servlet.

 
Rahul Chaitanya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear, Thank you so much. BeaUtils worked.

Now, is there a way, to keep the BeanUtils object into sesstion and have the form re-populated with the values that where present in Bean object ?

For Example:

<jsp:useBean id="user" class="user.UserData" scope="session">
<jsp:setProperty name="user" property="*"/>
</jsp:useBean>


<select name="queryType1">
<c:forEach items="${LIST.OPTIONS}" var="szenario">
<option value="${szenario}">${szenario}</option>
</c:forEach>
</select>


queryType1 is part of the bean [user.UserData] that was defined in the top that already had some values assigned to it.

Is there a way, that I can use the bean values to re-populate the form ?

Can we do it with some JAVA SCRIPT function or else, is there any other way to directly set the bean values to html:form directly, instead of modifying each and every html element to get values from bean ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66158
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, UseCodeTags, not bold, for posting code.

Secondly, get rid of the useBean tag. Just remove it. It's not needed and will just create problems when it gets in the way.

Now, is there a way, to keep the BeanUtils object into sesstion and have the form re-populated with the values that where present in Bean object ?

The bean created by bean utils is an object like any other. So you can place it in the session just like any other object. Why did you think that you could not?

Is there a way, that I can use the bean values to re-populate the form ?

Of course. For text elements, just use the value attribute. For select elements, put selected on the appropriate option. For checkboxes and radio elements, set the checked attribute as appropriate.

html:form

html:form? You are using Struts?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!