NetBeans中的代码模板

NetBeans中的代码模板

 

在 NetBeans中,为许多类型的文件都创建了模板,如jsp、java等,当我们选择创建一个这些类型的新文件时,就会自动按照模板生成初始的代码,我们可以很容易地自己修改定制这些代码。只需在主菜单的工具->模板中打开指定的模板文件,然后进行修改即可。

但是,如果需要创建一个新的模板,例如,我们常常需要创建带有表单的页面,可以创建一个特定的表单模板,这样,可以提高我们编写代码的效率。但是,通过实践发现,下面这种方式有些不足之处:自己创建一个新的jsp文件,然后在下图中选择“添加”进行指定

在使用这种模板创建新的文件时,发现其中的时间、文档名称等特殊变量均不起作用。接下来想了一个变通的办法,在上图中先选JSP,然后选择复制,再将复制后得到的模板改成自己需要的,这样就不会发生上面的问题了。

不过,目前还没找到有关NetBeans模板的更多资料,例如模板存放的位置、可以使用哪些特殊变量等等。只好暂且先这么用着吧。

---------------------------------------------------后记---------------------------------------------------------

在官网上找了下资料,发现NetBeans的模板是采用FreeMarker写的,可以向其中插入下面的预定义的变量:

  • ${date} inserts the current date, in this format: Feb 16, 2008
  • ${encoding} inserts the default encoding, such as: UTF-8
  • ${name} inserts the name of the file.
  • ${nameAndExt} inserts the name of the file, together with its extension.
  • ${package} inserts the name of the package where the file is created.
  • ${time} inserts the current time, in this format: 7:37:58 PM
  • ${user} inserts the user name.

http://platform.netbeans.org/tutorials/60/nbm-filetemplates.html#creating-the-template-file

posted on 2012-04-11 14:54  D_D_U  阅读(1846)  评论(0编辑  收藏  举报

导航