Java 在 JSP 中定义类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2226218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:56:53  来源:igfitidea点击:

Defining a class in a JSP

javajspclass

提问by Vivin Paliath

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.

请不要打我的脸!我知道这与好的设计背道而驰,但我只是在写一个测试页面来演示一些东西。我们的 webapp 模块(正确地)无法直接访问我们的域类。我不想在 JSP 之外创建一个完整的类,因为该页面仅用于演示目的,出于同样的原因,我不想编写很多无关的代码。我试图在 JSP 中以通常的方式定义一个类,但这不起作用(抛出了很多编译时错误)。这是一项快速而肮脏的一次性交易(完成后我将摆脱它)。我只想知道这是否可能。如果没有,那么我会走很长的路。

<%

 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

采纳答案by Hyman Leow

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.

我不明白为什么这不可能。JSP 只是编写 Servlet 的另一种方式,因此您应该能够在 Servlet 中将类创建为静态(或就此而言,非静态)内部类,就像创建任何其他类一样,使用 <%! %> 约定。

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>

回答by axtavt

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 publickeyword:

仅供参考:问题中的代码片段声明了一个嵌套类(即在方法体内声明的类)。没有public关键字是合法的:

<%      
    class Person { 
        ...
    }
%>