FreeMan

Java是条不归路……

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  4 随笔 :: 1 文章 :: 2 评论 :: 0 Trackbacks

2007年10月30日 #

1:创建模板标记
需要用到的标签:<tiles:insert >
<tiles:insert>的作用类似于方法中的形参,该标记将被调用到该模板的页面使用<tiles:insert />和<tiles:put />标记指定的具体信息。
下面会有一个例子:
Template.jsp模板页:
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<html>
<body>
<table width="80%" higth="80%" bordercolor="#ffddff">
 <tr height="15%" bgcolor="#ddbbcc">
  <td>
   <tiles:insert attribute="header" />
  </td>
 </tr>
 <tr height="50%" bgcolor="#ffaaaa">
  <td>
   <tiles:insert attribute="content" />
  </td>
 </tr>
 <tr height="15%" bgcolor="#ccccff">
  <td>
   <tiles:insert attribute="footer" />
  </td>
 </tr>
</table>
</body>
</html>

关键的show.jsp

<%@page contentType="text/html;charset=gb2312" language="java"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<tiles:insert page="Template.jsp">
 <tiles:put name="header" value="A.jsp"></tiles:put>
 <tiles:put name="content" value="B.jsp"></tiles:put>
 <tiles:put name="footer" value="C.jsp"></tiles:put>
</tiles:insert>


A.jsp部分,b.jsp和c.jsp略过
<%@page contentType="text/html;charset=gb2312" language="java"%>
<%@taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
演示信息,表头部分

posted @ 2007-10-30 22:48 我的Java工作经历 阅读(2015) | 评论 (0)编辑 收藏

流程控制包括转发标记<logic:forward>

重定向标记<logic:redirect>

1:其中<logic:forward>标记可以将请求转发给全局ActionForward指定的响应页面。此标记只有一个name属性,

用来指定全局变量ActionForward的名称,该属性必须来自struts应用程序配置文件<global-forwards>元素的某个子元素<forward>的name属性

2:<logic:redirect>
使用HttpServletResponse.sendRedicect()方法实现HTTP页面重定向的功能。有以下属性

forward:映射了资源相对路径的ActionForward
href:资源完整的url
page:资源的相对路径
使用这个标记,至少要有forward href page 中的一个属性,以便明确标明响应重定向到哪个资源
<logic:redirect href="testlogicBean2.jsp" />
posted @ 2007-10-30 22:04 我的Java工作经历 阅读(262) | 评论 (0)编辑 收藏

2007年7月25日 #

JAVA书写规范

(这条文章已经被阅读了364次) 时间:2002年03月08日 18:59 来源:韩伟 原创-IT

1. 命名

1.1 Package的名字由一个小写单词组成;

一个特有的包的名字的第一部分总是全部使用小写字母,

并应该是顶级域名中的一个,现在有com, edu, gov, mil, net, org,

或者是在ISO标准3166,1981中定义的两个字母的国家标识。

这样的规则可确定某一目录分开的组件,部门,项目,或登陆名

com.sun.eng , com.apple.quicktime.v2 ,org.apache.catalina

 

1.2 Class/Interface:大写字母开头而其他字母都小写;

类的名字应是名词,混合大小写,每个词的第一个字母大写。

尽量保证你的类的名字简单并是描述性的。使用完整的单词-避免

头字语和缩写(除非缩写比长的格式更广泛使用,例如URL or HTML)

class ImageSprite , interface RasterDelegate

 

1.3 方法应是动词,混合大小写,第一个字母小写,每个内部的词

的第一个字母大写;除了变量,所有的实例、类和类的常量都

是以小写字母开头的混和大小写。内部的词以大写字母开头。

变量名字不应以下划线或美元符$开始,尽管二者都是允许的。

run() ,runFast() , getBackground()

 

1.4 变量名字应简短但有意义。变量名字的选择应该是可记忆

的---就是说给普通的人指出它的使用的目的,除非是临时的。临时变

量的一般的名字对于整形变量是i,j,k,m,和n,对于字符是c,d,和e。

int i;

char c;

float myWidth;

 

1.5 常量:声明类常量的变量和ANSI常量的名字为下划线分开的

大写字母单词(应避免ANSI常量,除非为了调试)

static final int MIN_WIDTH = 4;

static final int MAX_WIDTH = 999;

 

 

2. 文件

2.1 开头:注释(name,version,date,copyright),package,import;

/*

* Classname

*

* Version information

*

* Date

*

* Copyright notice

*/

package java.awt;

import java.awt.peer.CanvasPeer;

 

2.2 Class/Interface:JavaDoc文档注释,体(包含程序、运行和其他注释);

变量,实例,方法等:按public,protect,private排列;尽量少用public。

参见最后example

 

 

3. 缩进

3.1 避免每行多于80 个字符,逗号后或运算符前断开,

someMethod(longExpression1, longExpression2, longExpression3,

longExpression4, longExpression5);

 

var = someMethod1(longExpression1,

someMethod2(longExpression2,

longExpression3));

 

3.2 较高级的断开比较低级的断开更好,新行应与同级

上一行的开头对齐;以下是断开算术式的两个例子。

第一个较好,因为断开出现在算术式以外,处于较高一级。

longName1 = longName2 * (longName3 + longName4 - longName5)

+ 4 * longname6; // PREFER

 

longName1 = longName2 * (longName3 + longName4

- longName5) + 4 * longname6; // AVOID

 

 

3.3 缩排的一个单位应使用4 个空格,不使用制表符而尽量使用空格。

通常语句使用8个空格缩进,就要折行,由于传统的(4个空格)

缩进使观察程序体比较难。例如:

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) { file://BAD WRAPS

doSomethingAboutIt(); file://MAKE THIS LINE EASY TO MISS

}

 

file://USE THIS INDENTATION INSTEAD

if ((condition1 && condition2)

|| (condition3 && condition4)

||!(condition5 && condition6)) {

doSomethingAboutIt();

}

 

 

4. 注释,Java程序有两类注释:文档注释和执行注释。

4.1 文档注释只有java具有,/**...*/。能够被javadoc 工具生成HTML文档,

描述类,接口,构造器,方法和字段,相当于使用指南。

/**

* Class description goes here.

*

* @version 1.82 18 Mar 1999

* @author Firstname Lastname

*/

 

4.2 执行注释是那些在使用在C++中的/*...*/和//。目的为了理解程序和运行,

包括不适合于文档注释的内容,块状或一行,使用/*...*/,注释程序可使用//,

也可放在短行后面,尽量对齐。

/*

* @(#)Blah.java 1.82 99/03/18

*

* Copyright (c) 1994-1999 Sun Microsystems, Inc.

* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

* All rights reserved.

*

* This software is the confidential and proprietary information of Sun

* Microsystems, Inc. ("Confidential Information"). You shall not

* disclose such Confidential Information and shall use it only in

* accordance with the terms of the license agreement you entered into

* with Sun.

*/

 

/* A class implementation comment can go here. */

// implementation comment

 

 

5. 声明declaration

5.1 每行一个声明,加以注释;

int level; // indentation level

int size; // size of table

 

5.2 不要在同一行放置不同的类型;

int foo, fooarray[]; file://WRONG!

 

5.3 只在块的开始处,放置声明;

第一次使用时再声明,增强代码的可移植性。

if (condition) {

int int2 = 0; // beginning of "if" block

...

}

for (int i = 0; i < maxLoops; i++) { ... }

 

5.4 方法后紧跟"(" ,"{"出现在行尾。

Sample(int i, int j) {

ivar1 = i;

ivar2 = j;

}

 

 

6. 语句

6.1 每行最多包括一个语句;

argv++; // Correct

argc--; // Correct

argv++; argc--; // AVOID!

if (condition) {

statements;

} else {

statements;

}

for (initialization; condition; update) {

statements;

}

 

6.2 比较重要的一个:

try {

statements;

}catch (ExceptionClass e) {

statements;

}

 

 

7. 空格

7.1 利用空行分隔代码段来提高可读性;

7.2 使用空行:类,接口,方法之间,在一个方法的本地变量和它的第一个语句间;

7.3 被一个圆括号跟着的关键字应被一个空格所分开,

例"while (true) { ",

但方法不分开。

7.4 参数列表中的逗号之后使用一个空格。

public void doSomethingElse(Object someParam, String twoParam)

7.5 二进制的操作符与数以空格分开,例"a = (a + b) / (c * d);" 。

 

 

8. 其它

8.1 避免使用一个对象来访问一个类的(静态)变量或方法。

而是使用一个类的名字;

classMethod(); file://OK

AClass.classMethod(); file://OK

anObject.classMethod(); file://AVOID!

 

8.2 避免分配多个变量给同样值在一个单独的语句中;

fooBar.fChar = barFoo.lchar = 'c'; // AVOID!

 

8.3 适当使用()和{}来分隔运算和程序。

 

 

9. 性能有关 (优化代码,调试,运行)

避免太多的使用 synchronized 关键字 ;

尽量使用 StringBuffer 对象;

尽量不要混合使用AWT 和 Swing 组件,等等。

 

 

下面是一个参考范例。

/*

* @(#)Blah.java 1.82 99/03/18

*

* Copyright (c) 1994-1999 Sun Microsystems, Inc.

* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.

* All rights reserved.

*

* This software is the confidential and proprietary information of Sun

* with Sun.....

*/

 

 

package java.blah;

 

import java.blah.blahdy.BlahBlah;

 

/**

* Class description goes here.

*

* @version 1.82 18 Mar 1999

* @author Firstname Lastname

*/

public class Blah extends SomeClass {

/* 执行注释. */

 

/** classVar1 文档注释*/

public static int classVar1;

 

/**

* classVar2 多于一行的文档注释

* 注释

*/

private static Object classVar2;

 

/** instanceVar1 文档注释 */

public Object instanceVar1;

 

/** instanceVar2 文档注释 */

protected int instanceVar2;

 

/** instanceVar3 文档注释 */

private Object[] instanceVar3;

 

/**

* ...构造器 Blah 文档注释...

*/

public Blah() {

// ...执行注释 goes here...

}

 

/**

* ...方法 doSomething 文档注释...

*/

public void doSomething() {

// ...执行注释 goes here...

}

 

/**

* ...方法 doSomethingElse文档注释..

* @param someParam参数描述

*/

public void doSomethingElse(Object someParam) {

// ...执行注释goes here...

}

}

posted @ 2007-07-25 14:49 我的Java工作经历 阅读(575) | 评论 (1)编辑 收藏

myeclipse4.0GA似乎必须要jdk1.5以上版本才可以(因为我开始安装了1.4后,连启动都有问题)。

我现在的环境是eclipse3.2+myeclipse4.0GA,总算可以打开了,不过在打开jsp文件时仍然有问题。
Unable to create this part due to an internal error. Reason for the failure: Widget is disposed

org.eclipse.swt.SWTException: Widget is disposed
 at org.eclipse.swt.SWT.error(SWT.java:3374)
 at org.eclipse.swt.SWT.error(SWT.java:3297)
 at org.eclipse.swt.SWT.error(SWT.java:3268)
 at org.eclipse.swt.widgets.Widget.error(Widget.java:435)
 at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:330)
 at org.eclipse.swt.widgets.Control.setLayoutData(Control.java:2386)
 at com.genuitec.eclipse.webdesigner3.design.DesignEditSystem.createDesignView(DesignEditSystem.java:72)
 at com.genuitec.eclipse.webdesigner3.WebDesigner3.createDesignView(WebDesigner3.java:73)
 at com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor.createDesignView(WebDesignerMultiPageEditor.java:392)
 at com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor.createDesignPage(WebDesignerMultiPageEditor.java:364)
 at com.genuitec.eclipse.webdesigner3.editors.WebDesignerMultiPageEditor.createPages(WebDesignerMultiPageEditor.java:286)
 at org.eclipse.ui.part.MultiPageEditorPart.createPartControl(MultiPageEditorPart.java:276)
 at org.eclipse.ui.internal.EditorReference.createPartHelper(EditorReference.java:596)
 at org.eclipse.ui.internal.EditorReference.createPart(EditorReference.java:372)
 at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:566)
 at org.eclipse.ui.internal.PartPane.setVisible(PartPane.java:290)
 at org.eclipse.ui.internal.presentations.PresentablePart.setVisible(PresentablePart.java:140)
 at org.eclipse.ui.internal.presentations.util.PresentablePartFolder.select(PresentablePartFolder.java:268)
 at org.eclipse.ui.internal.presentations.util.LeftToRightTabOrder.select(LeftToRightTabOrder.java:65)
 at org.eclipse.ui.internal.presentations.util.TabbedStackPresentation.selectPart(TabbedStackPresentation.java:394)
 at org.eclipse.ui.internal.PartStack.refreshPresentationSelection(PartStack.java:1144)
 at org.eclipse.ui.internal.PartStack.setSelection(PartStack.java:1097)
 at org.eclipse.ui.internal.PartStack.showPart(PartStack.java:1311)
 at org.eclipse.ui.internal.PartStack.add(PartStack.java:455)
 at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:102)
 at org.eclipse.ui.internal.PartStack.add(PartStack.java:441)
 at org.eclipse.ui.internal.EditorStack.add(EditorStack.java:111)
 at org.eclipse.ui.internal.EditorSashContainer.addEditor(EditorSashContainer.java:60)
 at org.eclipse.ui.internal.EditorAreaHelper.addToLayout(EditorAreaHelper.java:217)
 at org.eclipse.ui.internal.EditorAreaHelper.addEditor(EditorAreaHelper.java:207)
 at org.eclipse.ui.internal.EditorManager.createEditorTab(EditorManager.java:819)
 at org.eclipse.ui.internal.EditorManager.openEditorFromDescriptor(EditorManager.java:718)
 at org.eclipse.ui.internal.EditorManager.openEditor(EditorManager.java:679)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditorBatched(WorkbenchPage.java:2586)
 at org.eclipse.ui.internal.WorkbenchPage.busyOpenEditor(WorkbenchPage.java:2521)
 at org.eclipse.ui.internal.WorkbenchPage.access$10(WorkbenchPage.java:2513)
 at org.eclipse.ui.internal.WorkbenchPage$9.run(WorkbenchPage.java:2498)
 at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:67)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2493)
 at org.eclipse.ui.internal.WorkbenchPage.openEditor(WorkbenchPage.java:2478)
 at org.eclipse.ui.ide.IDE.openEditor(IDE.java:388)
 at org.eclipse.ui.ide.IDE.openEditor(IDE.java:350)
 at org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.openInEditor(EditorUtility.java:275)
 at org.eclipse.jdt.internal.ui.javaeditor.EditorUtility.openInEditor(EditorUtility.java:139)
 at org.eclipse.jdt.internal.ui.actions.OpenActionUtil.open(OpenActionUtil.java:49)
 at org.eclipse.jdt.ui.actions.OpenAction.run(OpenAction.java:190)
 at org.eclipse.jdt.ui.actions.OpenAction.run(OpenAction.java:174)
 at org.eclipse.jdt.ui.actions.SelectionDispatchAction.dispatchRun(SelectionDispatchAction.java:267)
 at org.eclipse.jdt.ui.actions.SelectionDispatchAction.run(SelectionDispatchAction.java:243)
 at org.eclipse.jdt.internal.ui.packageview.PackageExplorerActionGroup.handleOpen(PackageExplorerActionGroup.java:306)
 at org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart$4.open(PackageExplorerPart.java:651)
 at org.eclipse.jface.viewers.StructuredViewer$2.run(StructuredViewer.java:817)
 at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:37)
 at org.eclipse.core.runtime.Platform.run(Platform.java:843)
 at org.eclipse.ui.internal.JFaceUtil$1.run(JFaceUtil.java:44)
 at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:149)
 at org.eclipse.jface.viewers.StructuredViewer.fireOpen(StructuredViewer.java:815)
 at org.eclipse.jface.viewers.StructuredViewer.handleOpen(StructuredViewer.java:1069)
 at org.eclipse.jface.viewers.StructuredViewer$6.handleOpen(StructuredViewer.java:1168)
 at org.eclipse.jface.util.OpenStrategy.fireOpenEvent(OpenStrategy.java:249)
 at org.eclipse.jface.util.OpenStrategy.access$2(OpenStrategy.java:243)
 at org.eclipse.jface.util.OpenStrategy$1.handleEvent(OpenStrategy.java:283)
 at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:66)
 at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:928)
 at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3348)
 at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:2968)
 at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:1914)
 at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:1878)
 at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:419)
 at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
 at org.eclipse.ui.internal.ide.IDEApplication.run(IDEApplication.java:95)
 at org.eclipse.core.internal.runtime.PlatformActivator$1.run(PlatformActivator.java:78)
 at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:92)
 at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:68)
 at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:400)
 at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:177)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at org.eclipse.core.launcher.Main.invokeFramework(Main.java:336)
 at org.eclipse.core.launcher.Main.basicRun(Main.java:280)
 at org.eclipse.core.launcher.Main.run(Main.java:977)
 at org.eclipse.core.launcher.Main.main(Main.java:952)

网络上搜索得到的信息是myeclipse的版本可能和eclipse存在冲突,导致myeclipse的jsp页面设计器无法正确打开,因此,在国外的论坛上,都建议还是使用eclipse3.0来写jsp。

或者,就是不要使用myeclipse的页面设计器(反正对写代码的人而言更多是使用代码视图),因此要将jsp的默认打开方式改成代码试图:
window---->perferences----->General------->editors------>file associations
选择jsp------->选择相应的editor 为default。也就是把myeclispe jsp editor 设为default(而不是myeclispe visual jsp editor)

posted @ 2007-07-25 14:44 我的Java工作经历 阅读(740) | 评论 (0)编辑 收藏

仅列出标题