使用Java进行Web开发的随想

来源:Oracle认证    发布时间:2012-11-12    Oracle认证视频    评论



  我自己就提出了一个解决的方法:就是把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( "/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;
} }

上一页123下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答