在JSP中定义类 [英] Defining a class in a JSP

查看:375
本文介绍了在JSP中定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请不要在脸上打我!我知道这是飞行在良好的设计,但我只是写一个测试页来演示的东西。我们的webapp模块(正确)没有直接访问我们的域类。我不想在JSP之外创建一个整类,因为页面只是为了演示的目的,我不想为同样的原因写了很多无关的代码。我试图在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
    */

 }
%>

我得到的错误:

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


推荐答案

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

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>

这篇关于在JSP中定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆