Here is an example, a menu header JSP included in various pages:
<%!
class MenuItem {
String pageURL;
String text;
MenuItem(String url, String t) {
this.pageURL = url;
this.text = t;
}
}
static MenuItem[] menu = new MenuItem[] {
new MenuItem("page1.jsp", "Home"),
new MenuItem("page2.jsp", "Info"),
new MenuItem("page3.jsp", "Contact")
};
%>
<!-- html code .... -->
<tr height="40" id="menuRow">
<%
String className = "menuItem";
String uri = request.getRequestURI();
for (int i = 0; i < menu.length; i++) {
if (uri.indexOf(menu[i].url) >= 0) {
className = "menuItemCurrent";
}
%>
<td class="<%= className %>"><a href="<%=
response.encodeURL(menu[i].pageURL) %>"><%= menu[i].text %></a></td>
<td class="menuSpacer"> </td>
<% } %>
</tr>
-----Original Message-----
From: Jonathan Mast [mailto:[email protected]]
Sent: Saturday, January 24, 2009 11:00 AM
To: Tomcat Users List
Subject: Re: Inner class trouble in JSP
I can't help with the inner class issue other than to say that perhaps you
should move away from using inner classes in JSP totally. I don't
understand how you are using these inner classes, could you please explain?
On Sat, Jan 24, 2009 at 1:32 PM, Qiao Jin <[email protected]> wrote:
> **** I am using Tomcat 6.0.18 with JDK 1.5.0_17 on CentOS 5. I am getting
a
> compiling error on the following code in a jsp page that uses an inner
> class. I remember I used to be able to do this with a different container
> (implementing JSP 1.2 and JDK 1.4).
>
> <%!
>
> private class Test {
> String name = null;
>
> public Test(String n) {
> this.name = n;
> }
>
> String getName() {
> return this.name;
> }
> }
>
> static Test test = new Test("foobar");
> %>
>
> <%= test.getName() %>
>
> **** With Tomcat, I am getting the following error:
>
> org.apache.jasper.JasperException: Unable to compile class for JSP:
>
> An error occurred at line: 15 in the jsp file: /test.jsp
> No enclosing instance of type test_jsp is accessible. Must qualify the
> allocation with an enclosing instance of type test_jsp (e.g. x.new A()
> where
> x is an instance of test_jsp).
> 12: }
> 13: }
> 14:
> 15: static Test test = new Test("foobar");
> 16: %>
> 17:
> 18: <%= test.getName() %>
>
> **** Changing the code as following worked:
>
> static Test test = new test_jsp().new Test("foobar");
>
> **** My questions: I suppose this is a change from jdk 1.4 to 1.5? Doesn't
> this make the JSP page container dependent? I think that the naming
> convention of a JSP page's class is implementation specific.
>
> Having an inner class in a JSP page sometimes is pretty convenient for
> organizing some light data used in the page, such as menu text. Is there
> another alternative other than having a regular class?
>
> Any input would be appreciated. Thanks.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]