• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 5
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Luigi Mattino'
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic