aspose file tools*
The moose likes JSP and the fly likes How retrieve bean's parameters from the pageContext using a string as Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How retrieve bean Watch "How retrieve bean New topic
Author

How retrieve bean's parameters from the pageContext using a string as "dotted" notation

Luigi Mattino'
Greenhorn

Joined: Dec 04, 2013
Posts: 17
Hi all,
I hope that someone like me have to face the same problem: Retrieve an attribute that is inside a bean, contained in the pageContext (Session, request) using a string key, such as those used in a JSP expression language (eg ${beanName1.beanName2.attribute1}). My intent is to use strings like that: "beanName1.beanName2.attribute1" to retrieve variables in the session or request by java code.

In my custom tag i need to retrieve value in aìpageContext:
<mytag:input id="attribute1" property="beanName1.beanName2.attribute1" />
instead of write this code
<input type="text" id="attribute1" value="${beanName1.beanName2.attribute1}" />

How could it done?
Nick Wa
Greenhorn

Joined: Nov 28, 2013
Posts: 5

Your code is trying to do a few things at once and it's a bit hard to understand but I might be able to guess it.

The <input> tag has to be in a <form> tag. Afterward, you need to use the proper attributes within the <input> tag. When you use the id attribute, that is generally associated with things such as <label> tags, which assists with the webpage interactivity. Also, the value attribute will print ${beanName1.beanName2.attribute1} to the screen as a string but not access the value it references. In order to retrieve the value from the <input> tag into your JSP, you will need to specify a name attribute. For example:

<label for="myText">Click on this to get into the text box: </label>
<input type="text" name="getThisText" id="myText" />

I'm confused why you're using ${beanName1.beanName2.attribute1} to get the value of a form field. Are you trying to pass that value into attribute1? If so, you'll need to create a constructor in your JavaBean that accepts the desired type and amount of parameters, such as:



Ideally, in your servlet you would be assigning the value using setValue(String str) but you could also do it in a JSP.

If I haven't answered your question, can you please re-phrase what you're trying to do?


"He who gets an error is a lucky man. He has a new lesson to learn from. He who has no errors is a lucky man." - Java prof
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16303
    
  21

The "$" notation for property references is part of the J2EE standard Expression Language (EL) and indicates a read-only expression that is interpreted at page render time. You can write your own custom tags that tap into this mechanism, allowing you to provide EL support for your custom tags.

Doing the same thing without the automatic evaluation provided by J2EE (which is what you would have to do if you omitted the "$" but wanted the same results) requires more work from you.

There is an Apache library called "beanutils" that does this. In fact, the EL processor used by many popular J2EE servers - including Tomcat and JBoss - uses the Apache BeanUtils to do its work.

It is actually a good idea to read the BeanUtils documentation even if you don't plan to use BeanUtils directly. You can learn a lot about how the various property reference expressions work.


Customer surveys are for companies who didn't pay proper attention to begin with.
Luigi Mattino'
Greenhorn

Joined: Dec 04, 2013
Posts: 17
Hi Nick, thank's for your answer and sorry for my quikly explaination of this issue, i'll try to clarify better:
My need is to use a custom tag, that wrap the <input> tag an have the attribute "property" which is a java.util.String.
This string stand for a "bean's path" with dot notation as in EL expression: imagine that we have some class as follow


Somewhere in my string controller i wrote ...

in the forward jsp where it'll end up i wrote my custom tag as follow

my wish is that the core extension of TagSupport print out this one

How I can retrieve attributeOne string by this "beanOne.beanTwo.attributeOne" input string, without write a finder method that iterate over request,
session, pageContext and through all loaded beans into.
Nick Wa
Greenhorn

Joined: Nov 28, 2013
Posts: 5

I understand what you're trying to do, however, you've skipped the crucial steps for using TagSupport. First, you need to create a .java file that either implements or extends TagSupport. If you implement it, there are several methods you'll have to overwrite. If you extend it, you only need to overwrite the methods you'll be using. The documentation for these methods can be found HERE. Second, in your web application, you should have a folder called WEB-INF, which contains web.xml. In this folder, you'll need to add a .tld file, which will provide information for the custom tag. Last, in your JSP, you'll call the custom tag. In order to reference it, you'll use: <%@ taglib uri="WEB-INF/myTagLib.tld" prefix="g" %>. Later in your JSP, you'll use <g: customTag />. In your .tld, you'll define the role of customTag and call the .java. "g" is just a random letter I chose, you can use whichever letter as long as it is not one already in use. For example, if you're going to use JSTL, you shouldn't use "c" as your custom tag.

If you absolutely need to use the custom tag in an <input> tag, then you're going to have to edit the .tld for <input> to include your custom tag. It may be easier to make a new tag and resources just to avoid having to combine your code with the Java and .tld of the <input>.

Also, check out BeanUtils as suggested by Tim Holloway.
Luigi Mattino'
Greenhorn

Joined: Dec 04, 2013
Posts: 17
Thank you Tim and thank you Nick,
I had already done my custom tag (class extend TagSupport and related tld), but the last step that i was missing is this:

Problem solved!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How retrieve bean's parameters from the pageContext using a string as "dotted" notation