一个大的包含文件或几个较小的文件?

时间:2022-07-18 21:37:28

I am writing some jsp and I am wondering if it would be better to have one large include file that I would include with every page or several smaller ones that I would include only on certain pages as needed.

Any given page will only need to call a few methods at most ( < 5). If I use one file, it would be 2500+ lines of code.

My main concern would be about performance with a secondary concern on maintenance.

We are using the jsp include directive <%@ include file="FileToInclude" %>

Edit: I will be doing the same thing for asp pages as well.

我正在写一些jsp,我想知道如果有一个大的包含文件,我会包含在每个页面或几个较小的文件,我将只在某些页面上根据需要包含更好。任何给定的页面最多只需调用几个方法(<5)。如果我使用一个文件,它将是2500多行代码。我主要担心的是性能问题以及维护方面的次要问题。我们使用的是jsp include指令<%@ include file =“FileToInclude”%>编辑:我也会为asp页面做同样的事情。

5 个解决方案

#1


Anything that will be included in every JSP, feel free to add to a single included file. For stuff that will be included on only a small minority of JSPs, create separate included files for those.

任何将包含在每个JSP中的东西,随意添加到单个包含的文件中。对于仅包含在少数JSP中的内容,请为这些内容创建单独的包含文件。

Enormous JSPs are a bear to maintain.

巨大的JSP是一个需要维护的熊。

Even better, however, is moving as much code as possible out of the JSPs and into your domain objects or into your model or in your servlets. Anything that is pure Java usually belongs not in the JSP but in your JAR files:

但是,更好的方法是将尽可能多的代码移出JSP,进入域对象或模型或servlet。纯Java的任何东西通常都不属于JSP,而是属于您的JAR文件:

  1. IDEs do a better job of navigating and refactoring pure Java code.
  2. IDE在导航和重构纯Java代码方面做得更好。

  3. When JSPs are mostly HTML and JSP tags (with as little Java code as possible), they become easier to maintain and understand.
  4. 当JSP主要是HTML和JSP标记(尽可能少的Java代码)时,它们变得更容易维护和理解。

  5. It's easier to understand pure Java as there are no HTML tags mixed in.
  6. 由于没有混合HTML标签,因此更容易理解纯Java。

  7. It makes the JSPs easier to understand as a huge block of Java code is replaced with a very short call to Java code.
  8. 它使JSP更易于理解,因为大量的Java代码被非常短的Java代码调用所取代。

  9. It makes maintenance of your JSPs easier without making maintenance of your code base more difficult.
  10. 它使您的JSP维护更容易,而无需更难以维护代码库。

#2


You should use JSPs only for view purposes. Business logic should not be placed JSP files, but in plain java classes. Please read up on Model-View-Controller design patterns. See this wikipedia article

您应该仅将JSP用于视图目的。业务逻辑不应该放在JSP文件中,而应放在普通的java类中。请阅读模型 - 视图 - 控制器设计模式。请参阅此*文章

#3


This is Java right? Small include files for sure.

这是Java吗?小包含文件肯定。

Not to mention it will make refactoring, testing, debugging easier as well.

更不用说它将使重构,测试和调试更容易。

#4


Having done this is classic ASP - I would say a balance is in order

这样做是经典的ASP - 我会说平衡是有序的

I would look carefully over the functions / methods / calls you need in all pages and build one large include for those then the smaller less used methods make individual includes - this sort of balances it for performance and ease of building

我会仔细查看所有页面中所需的函数/方法/调用,并为那些构建一个大的包含然后使用较少的较少使用的方法进行单独包含 - 这样可以平衡它的性能和易于构建

#5


In ASP I have one include file that is used on every page...but that one file has about 5 or more includes in that. This way I can break the functions into logical groups (which makes it easier to find things, and safer when updating one file only) but only need remember one line when creating new pages that need the functions.

在ASP中,我有一个在每个页面上使用的包含文件......但是那个文件中包含大约5个或更多包含文件。这样我就可以将函数分解为逻辑组(这样可以更容易地查找内容,并且仅在更新一个文件时更安全),但在创建需要函数的新页面时只需要记住一行。

#1


Anything that will be included in every JSP, feel free to add to a single included file. For stuff that will be included on only a small minority of JSPs, create separate included files for those.

任何将包含在每个JSP中的东西,随意添加到单个包含的文件中。对于仅包含在少数JSP中的内容,请为这些内容创建单独的包含文件。

Enormous JSPs are a bear to maintain.

巨大的JSP是一个需要维护的熊。

Even better, however, is moving as much code as possible out of the JSPs and into your domain objects or into your model or in your servlets. Anything that is pure Java usually belongs not in the JSP but in your JAR files:

但是,更好的方法是将尽可能多的代码移出JSP,进入域对象或模型或servlet。纯Java的任何东西通常都不属于JSP,而是属于您的JAR文件:

  1. IDEs do a better job of navigating and refactoring pure Java code.
  2. IDE在导航和重构纯Java代码方面做得更好。

  3. When JSPs are mostly HTML and JSP tags (with as little Java code as possible), they become easier to maintain and understand.
  4. 当JSP主要是HTML和JSP标记(尽可能少的Java代码)时,它们变得更容易维护和理解。

  5. It's easier to understand pure Java as there are no HTML tags mixed in.
  6. 由于没有混合HTML标签,因此更容易理解纯Java。

  7. It makes the JSPs easier to understand as a huge block of Java code is replaced with a very short call to Java code.
  8. 它使JSP更易于理解,因为大量的Java代码被非常短的Java代码调用所取代。

  9. It makes maintenance of your JSPs easier without making maintenance of your code base more difficult.
  10. 它使您的JSP维护更容易,而无需更难以维护代码库。

#2


You should use JSPs only for view purposes. Business logic should not be placed JSP files, but in plain java classes. Please read up on Model-View-Controller design patterns. See this wikipedia article

您应该仅将JSP用于视图目的。业务逻辑不应该放在JSP文件中,而应放在普通的java类中。请阅读模型 - 视图 - 控制器设计模式。请参阅此*文章

#3


This is Java right? Small include files for sure.

这是Java吗?小包含文件肯定。

Not to mention it will make refactoring, testing, debugging easier as well.

更不用说它将使重构,测试和调试更容易。

#4


Having done this is classic ASP - I would say a balance is in order

这样做是经典的ASP - 我会说平衡是有序的

I would look carefully over the functions / methods / calls you need in all pages and build one large include for those then the smaller less used methods make individual includes - this sort of balances it for performance and ease of building

我会仔细查看所有页面中所需的函数/方法/调用,并为那些构建一个大的包含然后使用较少的较少使用的方法进行单独包含 - 这样可以平衡它的性能和易于构建

#5


In ASP I have one include file that is used on every page...but that one file has about 5 or more includes in that. This way I can break the functions into logical groups (which makes it easier to find things, and safer when updating one file only) but only need remember one line when creating new pages that need the functions.

在ASP中,我有一个在每个页面上使用的包含文件......但是那个文件中包含大约5个或更多包含文件。这样我就可以将函数分解为逻辑组(这样可以更容易地查找内容,并且仅在更新一个文件时更安全),但在创建需要函数的新页面时只需要记住一行。