我正在使用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;}
}
我有兩個(種基本)問題:
我爲什麼要添加干將訪問公共內部類的變量?
我放在干將後,也沒直接叫他們(即
author.id
而不是author.getId()
)是那裏的編譯器遵循的命名慣例,所以我必須定義getFoo
得到foo
的價值變量?
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