Help with JSP:GetProperty Tag

Howdy all,I have a <jsp:getProperty name='input' property='code01'/>tag and would like to be able to put a variable in the property field so that I can increment from code01 to code02 to code 03 by concatenating stringsIs this possible?
[295 byte] By [wmacey] at [2007-9-26 1:19:48]
# 1
Everything I try gets a " Value must be quoted " errorI create a variable like this:<% String new = "code02";%> and tried this:<jsp:getProperty name="input" property=new/>
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
HELP!!!
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
ANYBODY?
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
The <jsp:getProperty ...> tag is used to access the properties of a Java Bean that is declared in a <jsp:useBean ...> tag, not to access the values of a variable from a scriptlet.
hungyee at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Create a variable like this:

<%

String colName = "code02";

%>

use something like this,

<jsp:getProperty name="input" property='<%= colName>' />

Hope this helps u. If u hv any problems write me at kesavansrini@yahoo.co.in

Kesavan.

kes_javaman at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

> Create a variable like this:

> <%

> String colName = "code02";

> %>

>

> use something like this,

>

> <jsp:getProperty name="input" property='<%= colName %>'

> />

>

> Hope this helps u. If u hv any problems write me at

> kesavansrini@yahoo.co.in

>

> Kesavan.

kes_javaman at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

Thanks for your input on the forum, but I tried with no sucess to put it into action:

<% String colname="code02"; %>

<jsp:getProperty name='Input' property='<%=colname%>' />

Produced this:

Internal Servlet Error:

org.apache.jasper.JasperException: Cannot find any information on property '' in a bean of type 'com.infomed.InputBean'

at org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibrary.java:616)

at org.apache.jasper.compiler.GetPropertyGenerator.generate(GetPropertyGenerator.java:101)

at org.apache.jasper.compiler.JspParseEventListener$GeneratorWrapper.generate(JspParseEventListener.java:771)

at org.apache.jasper.compiler.JspParseEventListener.generateAll(JspParseEventListener.java:220)

at org.apache.jasper.compiler.JspParseEventListener.endPageProcessing(JspParseEventListener.java:175)

at org.apache.jasper.compiler.Compiler.compile(Compiler.java:210)

at org.apache.jasper.servlet.JspServlet.doLoadJSP(JspServlet.java:612)

at org.apache.jasper.servlet.JasperLoader12.loadJSP(JasperLoader12.java:146)

at org.apache.jasper.servlet.JspServlet.loadJSP(JspServlet.java:542)

at org.apache.jasper.servlet.JspServlet$JspServletWrapper.loadIfNecessary(JspServlet.java:258)

at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:268)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:429)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:500)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at org.apache.tomcat.core.ServletWrapper.doService(ServletWrapper.java:405)

at org.apache.tomcat.core.Handler.service(Handler.java:287)

at org.apache.tomcat.core.ServletWrapper.service(ServletWrapper.java:372)

at org.apache.tomcat.core.ContextManager.internalService(ContextManager.java:797)

at org.apache.tomcat.core.ContextManager.service(ContextManager.java:743)

at org.apache.tomcat.service.http.HttpConnectionHandler.processConnection(HttpConnectionHandler.java:213)

at org.apache.tomcat.service.TcpWorkerThread.runIt(PoolTcpEndpoint.java:416)

at org.apache.tomcat.util.ThreadPool$ControlRunnable.run(ThreadPool.java:501)

at java.lang.Thread.run(Thread.java:484)

Any Help you can give would be greatly appreciated, what I need to do is cycle through code01, code02....code15

and use the string to do it, but use a counter integer to create the string, ie..'code0' + i and then use the sting to access

on of the member properties in the been...(Code01, Code02) ertc.

wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
The <jsp:getProperty> tag must be used associate with javabean. Do you have a java bean which has a field called "Input"?
echojiang at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
absolutely,If I simply do a <jsp:getProperty name='Input property='code02'/>everything works great.when I try to replace the property='code02' with a variable, so that I can run through a loop, I run into the problems
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10
Does anybody have any clues?
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Anybody?
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
Have Not found a workaround yet....any ideas?
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
ANYONE?
wmacey at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

there is a way to do this?

I want to do a similar thing, but what I want is sending a parameter to a jsp and this jsp take that parameter like this:

<jsp:getProperty name="mybeanname" property='<%=request.getParameter("value")%>' />

or <% mybeanname.get + request.getParameter("value");%>

many thanks!!!

LBAEZ

LBAEZ at 2007-6-29 0:52:47 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

So far as I know, the getProperty tag doesn't support expressions, only literals ( http://java.sun.com/products/jsp/tags/12/syntaxref1213.html#8820 ).

What you can do is tell your bean what property you want returned for a given "get" call:<jsp:setProperty name="myBean" property="propVal" value="someVal"/>

<jsp:getProperty name="myBean" property="propVal" />

The getPropName method in the bean can then retrieve a value from a HashMap, or use reflection to call a different "getter" method, or whatever you need to return the desired value, based on the value passed in the "setter" method.

This isn't exactly to the Bean spec, but it will allow you to do what you want.

cafal at 2007-6-30 21:35:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
can you give me an example, I can't follow you!!many thanks....LBAEZ
LBAEZ at 2007-6-30 21:35:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 17

For a start, try:

<jsp:setProperty name='input' property='*'/>

This will unforutnately have side effects; being other variables might get setted.

If you want to know more, the keyword is "reflection". You should look up one of the Java Tutorials / Trails.

Otherwise, you can also try to download the source code of Tomcat from jakarta.apache.org and look how they had implemented the jsp:setProperty tag. I think it was under org.apache.jasper.runtime.JspRuntimeLibrary

Best of luck.

pytse at 2007-6-30 21:35:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 18

Actually I was thinking more along these lines:

--

The bean class:

--package uplate.testing;

import java.util.*;

import java.lang.reflect.*;

public class ValueClass {

private String val_name = "";

private String prop1 = "";

private String prop2 = "";

private String prop3 = "";

public ValueClass() {

}

public void setProp1(String prop){

prop1 = prop;

}

public String getProp1(){

return (prop1==null?"":prop1);

}

public void setProp2(String prop){

prop2 = prop;

}

public String getProp2(){

return (prop2==null?"":prop2);

}

public void setProp3(String prop){

prop3 = prop;

}

public String getProp3(){

return (prop3==null?"":prop3);

}

public String getPropVal(){

if (val_name == null || val_name.equals("")) return "";

try{

//change the property name into a "getter" name,

//i.e. prop2 => getProp2

String method_name = "get" + val_name.substring(0,1).toUpperCase() +

val_name.substring(1);

Class this_class = getClass();

Method _method = this_class.getMethod(method_name, null);

return (String)_method.invoke(this, null);

}

catch(Exception e){

return e.toString();

}

}

public void setPropVal(String val_name){

this.val_name = val_name;

}

}

--

The jsp page:

--<html>

<%

//Ok, here's the variable for the setProperty tag; the value of this

//String will be the property retrieved from the bean.

String prop_name = "prop2";

%>

<head><title>JSP Page</title></head>

<body>

<jsp:useBean id="mybean" scope="request" class="uplate.testing.ValueClass" />

<%-- no request coming in while testing, so manually setting properties --%>

<jsp:setProperty name="mybean" property="prop1" value="one" />

<jsp:setProperty name="mybean" property="prop2" value="two" />

<jsp:setProperty name="mybean" property="prop3" value="three" />

<%-- This is the meat; set the name of the property to be retrieved --%>

<jsp:setProperty name="mybean" property="propVal" value="<%=prop_name%>" />

Value retrieved from getProperty tag is:<BR>

<FONT COLOR=RED><jsp:getProperty name="mybean" property="propVal" /></FONT>

</body>

</html>

The last setProperty passes a dynamic value to the bean class; the bean class then uses that value to find (using reflection) the "getter" method for that value, and returns the result of that method's invocation.

There are simpler implementations, depending on how the data is stored. If all data is in a HashMap, say, by name-value pair, then the "setter" methods would call set(key, value) on the HashMap, and the "getter" methods would call get(key) on the HashMap, no reflection necessary.

Have fun!

cafal at 2007-6-30 21:35:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 19
manyyyy thankssss cafalreally I appreciate your helpp!!!!!!thanks you!!!!LBAEZ
LBAEZ at 2007-6-30 21:35:12 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...