Reputation: 3470
I've been working with a JSP+Java+Html, and I've encountered a problem with out.print() function, in a for cycle.
My function getGeneAvailableTaxonomies()
returns a list of integer numbers (of type List<Integer>
), and I want to print these numbers in an interface.
Here's my code:
for(Integer i : ApplicationExtender.getApplicationExtender(application).getGeneAvailableTaxonomies())
{
out.print(String.format("<option value=\"%1$d\">%2$s</option>", i, TaxonId.getOrganismFromId(i)));
}
The doce %1$d
should stand for the i integer value, while %2$s
should stand for the other parameter, the taxonomy id value as String.
But, unfortunately, this is what appears:
While I would like to see something like:
There's surely an error on my out.print() function call... but what's wrong?
Many thanks
Upvotes: 0
Views: 6859
Reputation: 2616
try this as much i have understood :
String option = "<option value=\""+d+"\">"+s+"</option>";
out.print(option);
using String.format
out.print(String.format("<option value=\"%d\">%s</option>", i, TaxonId.getOrganismFromId(i)));
Updated
as you have mentioned in comments that TaxonID.getOrganismFromId(i)
is returning int
so the there is only one change in your original code %2$s
to %2$d
thats it...
out.print(String.format("<option value=\"%1$d\">%2$d</option>", i, TaxonId.getOrganismFromId(i)));
Upvotes: 0
Reputation: 9424
You dont need the "$" in your format String. As you may know, using scriptlets is not a good way to do Java Web Development. I think that using JSTL is far better, as you won't mix Java code with markup in your JSP's.
Edit: The printf method is not present in out object as I said earlier, since it is a JspWriter and JspWriter not inherit from PrintWriter (that have printf). Sorry. So, try this (it worked for me).
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<select>
<%
String[] strings = new String[]{ "aaa", "bbb", "ccc", "ddd" };
for ( int i = 0; i < strings.length; i++ ) {
out.print( String.format( "<option value='%d'>%s</option>", i, strings[i] ) );
}
%>
</select>
</body>
</html>
If you want to use a PrintWriter as in Servlets, so this will work:
<%@page import="java.io.PrintWriter"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<select>
<%
PrintWriter writer = new PrintWriter( out );
String[] strings = new String[]{ "aaa", "bbb", "ccc", "ddd" };
for ( int i = 0; i < strings.length; i++ ) {
writer.printf( "<option value='%d'>%s</option>", i, strings[i] );
}
%>
</select>
</body>
</html>
Upvotes: 1
Reputation: 28865
Your print
/format
code seems OK when I run it on http://ideone.com/u8fDT. You probably just need to have the JSP recompiled (should probably happen automatically but may sometimes require server restart).
Also, mixing HTML and Java code like this is a pretty painful way to work. JSTL or a templating system like FreeMarker would make your life easier.
Upvotes: 0