开发者

Defining a class in a JSP

Please don't punch me in the face! I know this flies in the face of good design, but I'm simply writing a test page to demonstrate something. Our webapp module (correctly) has no direct access to our domain classes. I don't want to create a whole class outside of the JSP, since the page is just for demonstration purposes, and I don't want to write a lot of extraneous code for the same reason. I was trying to define a class the usual way in the JSP, but that didn't work (threw a lot of compile-time errors). This is a quick-n-dirty, one-time deal (I'll be getting rid of it once I'm done). I'd just like to know if this is possible or not. If not, then I will go the long way.

<%

 public class Person {
    private int id;
    private int age;
    private String name;

    /*
      ... ctor and getters and setters
    */

 }
%>

And the errors I got:

convert-jsp-to-java:
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------

An error occurred at line: 57 in the generated java file
Syntax error on token "class", invalid VariableDeclarator

An error occurred at line: 73 in the generated java file
The return type is incompatible with Object.getClass()

An error occurred at line: 74 in the generated java file
Syntax error on token "class", Identifier expected

An error occurred at line: 77 in the generated java file
Syntax error on token "class", invalid VariableDeclaratorId

An error occurred at line: 78 in the generated java file
Syntax error on token "this", PrimitiveType expected

An error occurred at line: 78 in the generated java file
Syntax error on token "class", invalid Expression

An error occurred at line: 79 in the generated java file
Syntax error on 开发者_如何转开发token "class", invalid Expression


I don't see why it wouldn't be possible. A JSP is just another way of writing a Servlet, so you should be able to create classes as static (or for that matter, non-static) inner classes within the Servlet, as you would any other class, using the <%! %> convention.

I was able to do a quick, functional, proof of concept:

<%@page contentType="text/html" pageEncoding="MacRoman"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%!
private static class NdBadIdea {
  private final int foo = 42;

  public int getFoo() {
    return foo;
  }
}
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=MacRoman">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <%=new NdBadIdea().getFoo()%>
    </body>
</html>


Just for information: code snippet from the question declares a nested class (i.e. a class, declared inside a method body). It would be legal without public keyword:

<%      
    class Person { 
        ...
    }
%>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜