wood burning stoves 2.0*
The moose likes JSP and the fly likes jsp:setProperty is ignored ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp:setProperty is ignored !" Watch "jsp:setProperty is ignored !" New topic
Author

jsp:setProperty is ignored !

Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
I think it's strange when using following code to set the javabean's data
<jsp:setProperty name="bean1" property="surname" param="name" />
if the parameter of "name" is empty, the whole command is just ignored ! I traced and saw that happened, but it's possible in an application that need to update some data from some non-empty value to empty value.
any comments from you guys ? how to handle this as I DO need to update the variable to empty string ? Thanks.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi Sylvia,
I read about that in the J2EE pattern book from sun. The only possibility that saves you from that feature they mentioned is to empty all member fields of the bean before using it.
<THATS WRONG>:
Dont blame Java. It seems it has to do with http. If a parameter value of a post request is empty, the whole parameter-value pair just wont get send to the server.
</THATS WRONG>
At least thats my understanding. Correct me if i am wrong.
Axel
[ March 23, 2002: Message edited by: Axel Janssen ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

This doesn't have anything to do with HTTP. All this interaction takes place on the server side.
The example:

is JSP-esque for something along the lines of

The name attribute of the setProperty tag identifies which bean you are trying to set the property on. If you omit the bean name, how is the servlet container supposed to know which bean you are trying to update?
I'm actually rather surprised that your servlet container is just ignoring the tag rather than complaining about it.
What exactly are you trying to accomplish? Perhaps checking out the definition of the setProperty tag might help.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Bear,
I'm sorry, I'm confused by your wording, in my code, I did not ignore the bean name, it's totally legal codes, why the servlet container has to complain?
I'm not sure if it's the problem of JSP compiler, as Bear remind me i can direct call
<% beanname.setValue(vaule) %> and the value is an empty string, I may try that out later and let you know the outcome.
Thx.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
I traced and saw that happened, but it's possible in an application that need to update some data from some non-empty value to empty value.

I would say that you are confusing the parameter-name with the parameter-value. The param attribute in the <jsp:setProperty> action corresponds to the name of the request parameter, the value of which (which may be empty) is to be assigned to the bean property.
-anthony
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Wait a moment.
I think I know what Sylvia means:
First of all: What I said regarding http was wrong.
Dont blame Java. It seems it has to do with http. If a parameter value of a post request is empty, the whole parameter-value pair just wont get send to the server.

Thats wrong.
But: There is a problem that jsp:setProperty does n o t overwrite the bean value if the parameter value is empty.
Try the following code if you like:
1. Bean

2. JSP

1. Now fill the name field and hit the "submit"-button.
2. Now let the field empty and hit the "submit"-button.
3. Now fill the name field with a different value than in step 1.
You see. It can course problems. Dont you think?
Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Axel,
Thanks for re-produce my problem.
I've tried the way beanname.setValue(value) is OK, means, even the value is empty, it do update the data in the bean.
so I believe more the problem is with he JSP compiler, should it be a reason for JSP behave like that ?
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Sylvia.

The bean-member-not-updated-when-incomming-request parameter problem you reported, is adressed in some detail in the J2EE Design Patterns (very good book, btw.) of Sun.
regards
Axel
(edited a lot, because I talked rubbish)
[ March 25, 2002: Message edited by: Axel Janssen ]
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
Hi Axel,
I am not sure if I can follow you.

propertyB was never set when using jsp:
<jsp:getProperty name="aBean" property="propertyA"/>
But it was set when using:
<% aBean.setPropertyA(String value); %>

Nothing will be set if you use the <jsp:getProperty> tag. However, I think if you use <jsp:setProperty>, it *will* call aBean.setPropertyA(). Can you post a full example and its output to illusrate your point?
Thanks


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Sorry guys,
I was wrong again.
Thanks Michael anyway.
<jsp:setProperty name="aBean" property="propertyA"/>
gives the same result as
aBean.setPropertyA (request.getParameter("propertyA"));
The method setPropertyA in the bean is actually called with <jsp:setProperty name="aBean" property="propertyA"/>

Heres the code:


next time I will test more. The weekend seems to have been another.
I will edit my above post to not confuse people.
Axel
Arnold Schwarzenegger of this forum: Much action, little brain.
(And I not even look like Arnold Schwarzenegger)
[ March 25, 2002: Message edited by: Axel Janssen ]
Sylvia Wang
Ranch Hand

Joined: Apr 10, 2001
Posts: 53
Look, I extract the JSP compiled codes, it does check if the value is null or empty before call the method. // staff is my bean name
if(request.getParameter("address") != null
&&!request.getParameter("address").equals(""))
{
staff.setAddress((request.getParameter ("address")));
}
Shall I call it AI (Artifcial Intellengent) of JSP ? althot I don't think it's intellengent by doing this any way
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427

Look, I extract the JSP compiled codes, it does check if the value is null or empty before call the method.

I think it might be hidden somewhere in the bean specification? (I cannot find it ) Anyway, I think the idea behind this is that many HTML fields are initialized to be empty strings and if the user submit an empty string (she does not enter a new value), she probablly does *not* want to change the content of the bean.
It is confusing. But it is good that you figured it out for all of us.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
As I allready have stated above this phenomen gets a 3-page treatment in Sun Press Core J2EE Patterns book (p. 46 ff.).
Many developers assume that a request parameter with an empty string value should, if matched to a bean property, cause that bean property to take on the value of an empty string, or null. The SPEC-COMPLIANT behavior is actually to make no changes to the matching bean property in this case, though. Furthermore, since JavaBean helper instances are typically reused across requests, such confusion can lead to data values being inconsistent and incorrect.

(they alude to an example, where in a html checkbox - group first some checkboxes are selected by user and at a later request all are deselected. Result: bean variable wont be emptied!)
In another paragraph these sun authors characterize this behavior as "less than intuitive nature" (p. 52)
Their simple design solution is to reset all state in the JavaBean between requests. They mention no other but this simple and type-intensive cluttering-code solution. :roll:
Axel
[ March 26, 2002: Message edited by: Axel Janssen ]
Ruchika
Greenhorn

Joined: May 03, 2002
Posts: 1
I have got one other way of doing it ...See if this works
String name = request.getParameter("name");
<jsp:setProperty name="bean" property="name" value="<%=name%>">
It will accept null as well as "" values of the property..
I hope this solve your query..
tks,
Ruchika
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all,
i am little surprised about this whole issue. you can find following text,
"If a request parameter has an empty or null value, the corresponding bean property is not set. Likewise, if the bean has a property that does not have a matching request parameter, the property value is not set."
at Sun Java Site
and it means that if the html input param is empty the corresponding setter method never gets called and so the bean property holds its previous value. we can override this by putting space in the html parameter as a value and then checking for the space in the corresponding setter method to set the param to null or empty if we want....
also, this issue has nothing to do with HTML request passing as you can try following,
<% S.o.p("someproperty=:"+request.getProperty("someproperty")+":"); %>
<jsp:setProperty name="beanname" property="*"/>
someproperty=:<jsp:getProperty name="beanname" property="someproperty"/>:
and if "someproperty" is empty (not even spaces) then it will produce,
someproperty=::
someproperty=:<<older value of the someproperty>>:
if someproperty has a space as value then following would be the output,
someproperty=: :
someproperty=: :
this is little wiered behavior but thats how it works!
regards
maulin
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jsp:setProperty is ignored !