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.
> 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.
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.
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
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.
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.
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!