---
title: "项目代码编写技巧（部分）"
author: "zithan"
date: "2011-03-24T09:23:00Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2011-03-24T09:23:00Z"
id: 973541
url: "https://zithan.iteye.com/blog/973541"
---

# 项目代码编写技巧

## 关于JavaScript

&lt;!--\[if !supportLists]--&gt;1、  &lt;!--\[endif]--&gt;使用JAVASCRIPT验证表单，必须同时使用JAVASCRIPT提交表单，而不是使用type=submit的按钮提交表单。这样用户在IE设置中禁止脚本之后，数据验证被屏蔽的同时，表单也无法提交。

&lt;!--\[if !supportLists]--&gt;2、  &lt;!--\[endif]--&gt;JavaScript变量和函数的命名要求同JAVA代码。局部变量必须使用VAR定义，避免使用全局变量。如：Var i,j;

&lt;!--\[if !supportLists]--&gt;3、  &lt;!--\[endif]--&gt;在编辑页面的保存、审核、反审核等javascript函数的开始处，一般需要下面代码，以防止页面按钮重复提交问题。

&lt;!--\[if gte vml 1]&gt;&lt;v:shapetype id="\_x0000\_t202" coordsize="21600,21600" o:spt="202" path="m,l,21600r21600,l21600,xe"&gt; &lt;v:stroke joinstyle="miter" /&gt; &lt;v:path gradientshapeok="t" o:connecttype="rect" /&gt; &lt;/v:shapetype&gt;&lt;v:shape id="\_x0000\_s1027" type="#\_x0000\_t202" style='position:absolute; left:0;text-align:left;margin-left:0;margin-top:7.8pt;width:468pt;height:85.8pt; z-index:2' /&gt;&lt;!\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;

document.edit.store.disabled = true;

document.edit.Submit.disabled = true;

或者

document.edit.Submit.disabled = false;

document.edit.store.disabled = false;

&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso & !vml]--&gt; &lt;!--\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt; 

 

 

 

 

 

 

## 关于HTML

&lt;!--\[if !supportLists]--&gt;1、  &lt;!--\[endif]--&gt;要特别注意大小写，虽然在windows下没有区分，但是unix是区分的，并且该问题在windows下可能不能被检出。

&lt;!--\[if !supportLists]--&gt;2、  &lt;!--\[endif]--&gt;所有链接不允许使用根目录符号“/”，只能以本文件所在目录为开始路径寻找需要的文件，即使用“./”或者“../”两种方式来寻找文件。

&lt;!--\[if !supportLists]--&gt;3、  &lt;!--\[endif]--&gt;HTML变量的命名按照JAVA代码规范，并且表单变量的名称必须与对应的ValueObject成员变量的名称一致。

&lt;!--\[if !supportLists]--&gt;4、  &lt;!--\[endif]--&gt;使用缩进的格式，具体格式要求同JAVA代码。（避免使用tab字符，缩进长度为4个空格）

&lt;!--\[if !supportLists]--&gt;5、  &lt;!--\[endif]--&gt;使用统一的css文件，规范编排格式；所以表格不允许指定高度、字体无需指定size、font等。

&lt;!--\[if !supportLists]--&gt;6、  &lt;!--\[endif]--&gt;尽量少用Struts的&lt;html&gt;标签库，而是直接写html代码，便于使用可视化编辑工具。

&lt;!--\[if !supportLists]--&gt;7、  &lt;!--\[endif]--&gt;若非特殊情况，不允许在JSP页面直接写JAVA代码。所以如果使用scriptlet，必须写清楚使用的原因以及影响。

&lt;!--\[if !supportLists]--&gt;8、  &lt;!--\[endif]--&gt;在HTML的HEAD部分，一般都会有类似的代码，以便于IE确定文件的编码方式和减少IE缓存而导致的数据重复提交、数据不一致等问题，同时指定页面显示风格、引用常用javascript函数包。

&lt;!--\[if gte vml 1]&gt;&lt;v:shape id="\_x0000\_s1026" type="#\_x0000\_t202" style='position:absolute;left:0;text-align:left;margin-left:0;margin-top:7.8pt; width:468pt;height:124.55pt;z-index:1' /&gt;&lt;!\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;meta http-equiv="Expires" content="0"&gt;

&lt;meta http-equiv="Pragma" content="no-cache"&gt;

&lt;meta http-equiv="Cache-Control" content="no-cache"&gt;

&lt;link rel="stylesheet" href="../CSS/style.css" type="text/css"&gt;

&lt;script language="Javascript" src="../Script/util.js"&gt;&lt;/script&gt;

&lt;script language="Javascript" src="../Script/utilfloat.js"&gt;&lt;/script&gt;

&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso & !vml]--&gt; &lt;!--\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt; 

 

 

 

&lt;!--\[if !supportLists]--&gt;**9、** &lt;!--\[endif]--&gt;如果使用DreamWeaver编辑页面，一定要先设置编码为**UTF-8。**

 

## 关于Struts

&lt;!--\[if !supportLists]--&gt;1、  &lt;!--\[endif]--&gt;统一使用关键字action的原因是可以方便的实现用户权限判定。

 

## 页面session超时验证和系统登陆用户验证

系统登陆用户信息统一存放在名称为SYSUser\_Login的Session中；每个Action类的perform函数开始部分需要验证session是否超时、用户是否已登陆，单元测试代码如下：

      //begin get the session and the test login user

      HttpSession session = httpServletRequest.getSession();

      SYSSysuser usrTest = SYSSysuserProxy.findSYSSysuser(new Long(1));

      session.setAttribute("SYS\_SYSUser\_Login",usrTest);

      //end get the session and the test login user

&lt;!--\[if !supportLists]--&gt;n         &lt;!--\[endif]--&gt;提交集成时上面代码需要修改为：

       //begin get the session and the login user

       HttpSession session = httpServletRequest.getSession(false);

       if (session == null || session.getAttribute("SYS\_SYSUser\_Login") == null)

         throw new GeneralException(ExceptionType.SESSION\_TIME\_OUT\_EXCEPTION,                                    "In Action.perform()");

       //begin get the session and the login user

 

## 关于JAVA

&lt;!--\[if !supportLists]--&gt;1、  &lt;!--\[endif]--&gt;尽量保持简洁明了，长度尽量限制在25个字符以下。

&lt;!--\[if !supportLists]--&gt;2、  &lt;!--\[endif]--&gt;不要使用下划线、美元符号开头的标识符，尽管这在语法上是允许的。

&lt;!--\[if !supportLists]--&gt;3、  &lt;!--\[endif]--&gt;使用完整的单词，避免使用只取首字母的简写词和缩写词（除非缩写词比完整词汇的使用更广泛，比如URL，HTML）。

&lt;!--\[if !supportLists]--&gt;4、  &lt;!--\[endif]--&gt;采用客户该领域的词汇（例如：如果用户称他们的客户为Customer而不是Client，就要求使用Customer）。

&lt;!--\[if !supportLists]--&gt;5、  &lt;!--\[endif]--&gt;避免使用“神秘数”，而是使用常量来代替。

&lt;!--\[if !supportLists]--&gt;6、  &lt;!--\[endif]--&gt;常量使用大写字母和下划线“\_”组成的字符串。

&lt;!--\[if !supportLists]--&gt;7、  &lt;!--\[endif]--&gt;源代码一行不要超过80个字符，超过80个字符就使用折行（Warpping Line）

&lt;!--\[if !supportLists]--&gt;8、  &lt;!--\[endif]--&gt;避免在源程序中使用Tab

&lt;!--\[if !supportLists]--&gt;9、  &lt;!--\[endif]--&gt;pakage放在import之前，import必须输入完整的类名。优先列出本项目的包，然后才是标准库。

&lt;!--\[if !supportLists]--&gt;10、              &lt;!--\[endif]--&gt;空白 Blank

&lt;!--\[if !supportLists]--&gt;10.1、     &lt;!--\[endif]--&gt;在while、for、if、switch等关键字与判断条件之间加上空格。

&lt;!--\[if gte vml 1]&gt;&lt;v:shape id="\_x0000\_s1028" type="#\_x0000\_t202" style='position:absolute;left:0;text-align:left; margin-left:45pt;margin-top:4.15pt;width:369pt;height:54.6pt;z-index:3'&gt; &lt;v:textbox style='mso-next-textbox:#\_x0000\_s1028' /&gt; &lt;/v:shape&gt;&lt;!\[endif]--&gt;&lt;!--\[if !vml]--&gt;

    &lt;!--\[endif]--&gt;&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;

while (true){

    statements;

}

&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso & !vml]--&gt; &lt;!--\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt; 

 

 

 

10.2、在函数的多个参数之间加上空格。

&lt;!--\[if gte vml 1]&gt;&lt;v:shape id="\_x0000\_s1029" type="#\_x0000\_t202" style='position:absolute;left:0;text-align:left; margin-left:45pt;margin-top:4.15pt;width:369pt;height:23.4pt;z-index:4'&gt; &lt;v:textbox style='mso-next-textbox:#\_x0000\_s1029' /&gt; &lt;/v:shape&gt;&lt;!\[endif]--&gt;&lt;!--\[if !vml]--&gt;

    &lt;!--\[endif]--&gt;&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;

myMethod(arg1, arg2, arg3);

&lt;!--\[if !mso]--&gt;

&lt;!--\[endif]--&gt;&lt;!--\[if !mso & !vml]--&gt; &lt;!--\[endif]--&gt;&lt;!--\[if !vml]--&gt;

&lt;!--\[endif]--&gt; 

 

10.3、一元运算符与变量之间**不能**加空格。如：argv++;

 

&lt;!--\[if !supportLists]--&gt;11、              &lt;!--\[endif]--&gt;变量声明之后，应该马上对变量进行初始化。

&lt;!--\[if !supportLists]--&gt;12、              &lt;!--\[endif]--&gt;变量应该在块（block）的开始部分集中声明，而不是用到的时候才声明。（临时变    量、循环变量、异常变量除外）

&lt;!--\[if !supportLists]--&gt;13、              &lt;!--\[endif]--&gt;强烈建议不要用public变量！

&lt;!--\[if !supportLists]--&gt;14、              &lt;!--\[endif]--&gt;先写注释，后写代码。

&lt;!--\[if !supportLists]--&gt;15、              &lt;!--\[endif]--&gt;注释信息不仅要包括代码的功能，还应给出原因

&lt;!--\[if !supportLists]--&gt;16、              &lt;!--\[endif]--&gt;使用Javadoc标记注释，以便于生成api文档。

&lt;!--\[if !supportLists]--&gt;17、              &lt;!--\[endif]--&gt;保持注释的简洁清晰。