站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题使用Java进行Web开发的随想

使用Java进行Web开发的随想

摘要: 我朋友经常引用我的一句话就是:你的Java对我的Javascript是侵入的....,仿佛她比我还OO来着
  我朋友经常引用我的一句话就是:你的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对象。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>/*
* 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( "\tvar " + 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( "\t"+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;
} }</td></tr></table>
  标签的tld也一起给出吧,虽然不是关键

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>
<?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>
</td></tr></table>
  具体调用的JSP页面

  代码

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td><%@ 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"/>
</td></tr></table>
  这样我们就得到这样的HTML代码。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td><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></td></tr></table>
  控制页面输出代码为,JS程序员就是这样来渲染WEB页面的:

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td><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> </td></tr></table>
  输出内容:

  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支持....

↑返回目录
前一篇: 新版JUnit 4.0 抢先体验
后一篇: j2EE基础概念