reset password
Author Message
mbuisin
Posts: 16
Posted 10:38 Apr 26, 2011 |

If a bean has a property that is a non-primitive data type (i.e. inner class, collection class), can it still be accessed with just a <jsp:getProperty> tag? During lab 1, I realized I can only access it using expression language.

Now with the MVC architecture, can we still use expression language, since the lecture and hw3 says that scripting elements are not allowed? Or is it only the code in scriplet tags <% %> that are not allowed?

Also, I've notice in the textbook (Ch. 15), the sample code for the beans had arguments in their constructors. Shouldn't beans only have empty argument/default constructors? Or maybe at the time of the books publishing, beans were allowed to have argument-filled constructors?

abajpai
Posts: 52
Posted 10:52 Apr 26, 2011 |

1) I'm not sure about accessing non-primitive data types on a jsp. but i can't figure out why you would need to, since the JSP is used only for display.


2) i think you are right about the difference on EL and the script elements <% %>. EL should be allowed since without it, it would be difficult to access the data, but no scripting using the <% %> tags

 

3) as for the beans bit, i think beans are allowed to have arguements in their constructors, but you should have a default no arg constructor as well, though i might be wrong

Last edited by abajpai at 10:57 Apr 26, 2011.
mbuisin
Posts: 16
Posted 11:12 Apr 26, 2011 |

1) I guess what I meant was if the data was nested inside a bean. For example, in our lab 1, the TestBean bean needed to have an array list of TestEntry objects. So if you want to access the question property of one of the elements of the arraylist in the Bean (i.e. TestBean.getCurrentTestEntry().getQuestion() ), how would the <jsp:getProperty> access the nested data?

If you used a <jsp:Property name="xxxBean" property="currentTestEntry" />, that would return a TestEntry object, wouldn't it? If so how would you further access the question property of that returned object using another <jsp:Property> tag?

So my alternative was to use EL: ${xxxBean.currentTestEntry.question}.

abajpai
Posts: 52
Posted 11:18 Apr 26, 2011 |

i am not sure why you are trying to use get property to access the questions.. you can set the bean using <jsp:useBean>, access the questions with $ {Bean.curentTestEntry.question}, like u said.

Last edited by abajpai at 11:18 Apr 26, 2011.
mbuisin
Posts: 16
Posted 11:28 Apr 26, 2011 |

Lol. Exactly. Honestly, I don't ever want to use it that way with the jsp tag. I was just afraid that we won't be able to use EL in MVC when it said no script elements.

Thanks for clearing that up.

abajpai
Posts: 52
Posted 11:42 Apr 26, 2011 |

No problem. Glad I could help :)

cysun
Posts: 2935
Posted 14:00 Apr 26, 2011 |

1. From Section 5.3 of the JSP 2.1. Specification:

JSP.5.3 <jsp:getProperty>

The <jsp:getProperty> action places the value of a bean instance property, converted to a String, into the implicit out object, from which the value can be displayed as output.

It means that if a property is of a class type, you can still access it using <jsp:getProperty>; however, it will be converted to a string (via the toString() method). Also <jsp:getProperty> is only used for display, i.e. you can't get a property using <jsp:getProperty> then pass the object to something else like what you can do with EL.

2. JSP scripting elements specifically refer to: JSP Expression (i.e. <%= %>), JSP Scriptlet (i.e. <% %>), and JSP Declaration (i.e. <%! %>). See Slide 11 of the lecture notes. So yes, you can use EL in your MVC implementation.

3. The textbook states that a bean class must/should have a) a zero-argument constructor, b) no public fields, and c) getters and/or setters for "persistent values" (whatever that means).

None of these is really required.

For a), if your class doesn't have a zero-argument constructor, you won't be able to create a bean object using <jsp:useBean>, but you can still create an bean object in some other way (e.g. in a servlet).

For b), if your class has some public fields, you won't be able access them directly in JSP, but you can still access them in Java code like in servlets and other beans.

For c), if you don't have getters/setters for certain values, you won't be able to access them as bean properties, but again you can still access them in Java code through method calls or field access.

So basically the textbook takes a somewhat narrow view of bean and considers only classes that can be used with <jsp:useBean> as beans. In recent years, the term bean has been used in a more broader sense in Java web development and has become synonymous with POJO (Plain Old Java Object) and data model classes/objects.