使用Java進行Web開發的隨想

來源:互聯網
上載者:User

我朋友經常引用我的一句話就是:你的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;
} }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.