2016-08-25 79 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。