2016-08-25 83 views
0

我正在使用Servlet和JSP一个非常基本的web应用程序,我有以下设置:混淆的Web应用程序干将

public class DataManager { 
    //some method implementations omitted since they are not important 
    public DataManager(){} 

    public class Author{ 
     public int id; 
     public String name; 
     public String born; 
     public String died; 
    } 

    public Author getAuthor(int authorId){} 
    public ArrayList<Author> getAuthors(){} 
} 

public class AuthorsServlet extends HttpServlet { 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     ServletContext context = getServletContext(); 
     DataManager dm = (DataManager) context.getAttribute("datamanager"); 
     ArrayList<DataManager.Author> authors = dm.getAuthors(); 

     request.setAttribute("authors", authors); 
     request.getRequestDispatcher("/authors.jsp").forward(request, response); 

    } 
} 

在我的JSP文件中我有:

<c:forEach var="author" items="${authors}"> 
    <tr> 
     <td>${author.id}</td> 
     <td><a href="author.jsp">${author.name}</a></td> 
     <td>${author.born}</td> 
     <td>${author.died}</td> 
    </tr> 
</c:forEach> 

但是,我一直得到一个错误,说Property 'id' not found on type db.DataManager$Author,直到我把我的DataManager类的Author类的Author类中的吸气剂:

public class Author{ 
    public int id; 
    public String name; 
    public String born; 
    public String died; 
    public int getId(){ return id; } 
    public String getName(){ return name;} 
    public String getBorn(){return born; } 
    public String getDied(){ return died;} 
} 

我有两个(种基本)问题:

  1. 我为什么要添加干将访问公共内部类的变量?

  2. 我放在干将后,也没直接叫他们(即author.id而不是author.getId())是那里的编译器遵循的命名惯例,所以我必须定义getFoo得到foo的价值变量?

+0

JSP遵循java bean模式,请参阅[https://docs.oracle.com/javase/tutorial/javabeans/writing/properties.html](https://docs.oracle.com/javase/tutorial/) javabeans/writing/properties.html) [http://www.java-samples.com/showtutorial.php?tutorialid=552](http://www.java-samples.com/showtutorial.php?tutorialid= 552) – daotan

回答

1

运行时环境自动调用author.getId()以评估${author.id}。这就是为什么您需要定义getter(明确地说,因为Java并未隐式定义它们),即使这些字段是公共的。

有一个命名约定:如果该申请的名称是fooFieldExample,吸气剂的名称应该是getFooFieldExample(注意foo的大F)

+1

这不是编译器。这发生在运行时,使用反射。 –

+0

是的。更正 – AhmadWabbi

+0

@JBNizet这是如何使用反射?对不起,我只是普遍熟悉这个概念。 – Liumx31

0

为什么我要补充的getter访问公共内部类的变量?

因为这就是JSP规范希望它是,$author.id语句转换author.getId()其中id应该有一个公共的访问方法,我把在干将getId();

即使之后,我并没有直接调用他们(即author.id而不是author.getId())是否存在编译器遵循的命名约定,所以我必须定义getFoo以获取foo变量的值?

是的,它是命名约定。请记住,JSP是用于设计目的并由页面设计者开发的.JSps不是由java程序员设计的。所以$author.id对非程序员来说更方便。

表达式$author.id是一种表达式语言语法,用于访问JSP页面中可用的变量。表达语言$author.id被翻译为author.getId();

+0

那么这是否也适用于非JSP情况?说如果我有'ArrayList authors = dm.getAuthors();'''然后我做'authors.get(1).name'这样的工作,这将工作没有'作者'类中的获取者,在'DataManager'之外的作用域? – Liumx31

+0

@ Liumx31 no。在Java代码中访问公共字段访问公共字段。它使用吸气剂是你调用吸气剂。但是,一般来说,田野不应该公开。 –

+0

@PrasadKharkar你说的是完全错误的。 –

0

声明一个类领域的私人和定义getter和setter方法对他们来说是通常的Java最佳实践(几乎是必要的)。 它们在Java中很常见,大多数框架甚至标准都依赖于它们,例如:JSP EL langauage将author.id中的author.id转换为author.getId()。

你应该声明你的领域是私人的,并使用getters和setter。