top
Loading...
使用Java進行Web開發的隨想
我朋友經常引用我的一句話就是:你的Java對我的Javascript是侵入的....,仿佛她比我還OO來著。

我自己就提出了一個解決的方法:就是把Java對象寫成JS對象,這樣Web設計人員只要關注JS對象,用JS對象來渲染整個Web頁面,這樣我就不會和他的領域沖突了。

簡單來說,我們平常的WEB框架都是號稱MVC的,這樣它們就把V這個事情壓在了JAVA程序員的身上,可憐我的審美觀啊~所以我們應該把V繼續往下推,推給對JAVA什么都不懂,但是卻有非常豐富的WEB設計人員的身上。總不能讓別人去學JAVA呀,那就只好把JAVA對象寫成JS對象,這樣WEB設計人員就可以輕松調用JS了。

大體實現過程是這樣的:

1、雙方先討論項目的需求,然后確定下個個頁面需要顯示什么內容,怎么顯示不管。討論完后便確定了JS對象和數據庫的大體結構。

2、各自寫各自的東西...

3、雙方寫好后把WEB頁面通過JS對象和Java連接起來,調試,完工。

具體關鍵代碼:

J2J.java的代碼,功能是獲取scope范圍內,名稱為source的java對象,然后把這個java對象寫成名稱為distName類別為dist的JS對象。

代碼:

/*
* J2J.java
*
* Created on 2006年10月2日, 下午7:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package net.vlinux.tag.j2j;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.lang.reflect.*;
import java.util.*;

/**
*
* @author vlinux
*/
public class NewObject extends TagSupport {
private String dist;
private String distName;
private String scope;
private String source;

private List<Method> getGetMethods( Object aObject ) {
Method[] array = aObject.getClass().getMethods();
List<Method> list = new ArrayList<Method>();
for( int i=0;i<array.length;i++ ){
String methodName = array[i].getName();
if( methodName.matches("get.*") )
list.add(array[i]);
}
return list;
}

private String getFieldName( Method aMethod){
String methodName = aMethod.getName();
String subName = methodName.substring(3,methodName.length());
return subName.toLowerCase();
}

private Object getSourceObject(String scope, String source){
if( scope.equals("request") ){
return pageContext.getRequest().getAttribute(source);
}else if( scope.equals("session") ){
return pageContext.getSession().getAttribute(source);
}else if( scope.equals("page") ){
return pageContext.getAttribute(source);
}else{
System.out.println("xxx");
return null;
}
}

public int doStartTag(){
JspWriter out = pageContext.getOut();
Object sourceObject = getSourceObject(getScope(),getSource());
List list = getGetMethods( sourceObject );
try{
out.println( "<script>" );
out.println( "var " + getDistName() + " = new " + getDist() + "();");
for( int i=0;i<list.size();i++ ){
try{
String fieldName = getFieldName((Method)list.get(i));
String value = ((Method)list.get(i)).invoke( getSourceObject(getScope(),getSource())).toString();
out.println( ""+getDistName() + "." + fieldName + " = "" + value +""");
}catch(Exception e){
//
}
}
out.println( "</script>" );
}catch( java.io.IOException ioe){
//
}
return (EVAL_BODY_INCLUDE);
}
public int doEndTag(){
return (EVAL_PAGE);
}

public String getDist() {
return dist;
}

public void setDist(String dist) {
this.dist = dist;
}

public String getDistName() {
return distName;
}

public void setDistName(String distName) {
this.distName = distName;
}

public String getScope() {
return scope;
}

public void setScope(String scope) {
this.scope = scope;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
} }

標簽的tld也一起給出吧,雖然不是關鍵

代碼:

<?xml version="1.0" encoding="UTF-8"?> <taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>J2J</shortname> <uri>/J2J</uri> <tag> <name>newObject</name> <tagclass>net.vlinux.tag.j2j.NewObject</tagclass> <bodycontent>JSP</bodycontent> <info></info> <attribute> <name>distName</name> <required>true</required> </attribute> <attribute> <name>dist</name> <required>true</required> </attribute> <attribute> <name>scope</name> <required>true</required> </attribute> <attribute> <name>source</name> <required>true</required> </attribute> </tag> </taglib>

具體調用的JSP頁面

代碼

<%@ taglib uri="/WEB-INF/J2J.tld" prefix="jj"%>
<% 
//創建一個簡單對象
net.vlinux.test.User user = new net.vlinux.test.User();
user.setId(new Integer(1));
user.setName("vlinux");
user.setPassword("lovefs");
user.setUsername("oldmanpushcart");
//把對象放到request中去
request.setAttribute("user",user);
%>


<!--
這里要注意
dist是目標Javascript對象,這個是必須和web設計人員事先約定好的
distName 是目標Javascript對象實例的名,這個也是必須和web設計人月事先約定好
scope 告訴標簽去那個范圍尋找java對象的實例
source 源對象,也就是java對象,標簽會通過scope確定的范圍搜尋source
-->
<jj:newObject dist="User" distName="user" scope="request" source="user"/>

這樣我們就得到這樣的HTML代碼。

代碼:

<script>
var user = new User();
user.username = "oldmanpushcart"
user.name = "vlinux"
user.id = "1"
user.password = "lovefs"
user.class = "class net.vlinux.test.User"
</script>

控制頁面輸出代碼為,JS程序員就是這樣來渲染WEB頁面的:

代碼:

<script>document.writeln(user.id);</script><br>
<script>document.writeln(user.name);</script><br>
<script>document.writeln(user.username);</script><br>
<script>document.writeln(user.password);</script><br>

輸出內容:

1 vlinux
oldmanpushcart
lovefs

JavaToJS我喜歡叫她j2j.

j2j的優點在于:

1、Java程序員和JS程序員相互之間不會干擾,各自干各自的。

2、JS程序員不用依賴JAVA代碼才能進行測試,相反,它們很早就可以通過構造一些JS銀彈來對頁面情況進行測試了,開發速度一般比JAVA部分還快。很多項目都是先弄個大體框架,然后再慢慢細調。這樣效率低,而且也不方便和客戶交流--客戶喜歡看到實際的東西。如果是J2J就是一步到位。

3、方便日后的維護和替換--萬一有一天我死了(T_T),我的朋友還可以找其他的WEB程序員進行維護,如果找不到JAVA程序員,她甚至還可以找個ASP程序員把我的代碼全部重寫而不用修改頁面的任何地方--也許表單的action需要改一下。

4、天生支持AJAX

當然,任何東西都是有缺點的,j2j的缺點在于:

1、不適合用來改寫以前的代碼,因為j2j的JS對于其他頁面來說是侵入的

2、HTTP頁面上會有大量的<script></script>段的存在,顯得非常的不美觀

3、沒有IDE支持....
作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗