A random thoughts on web development using Java

Source: Internet
Author: User

My friend often quotes me as saying: "Your Java is intrusive to my javascript ... as if she were more than I am."

I put forward a solution to the method: Is the Java object as a JS object, so that the web designer as long as the focus on the JS object, with the JS object to render the entire Web page, so I will not and his field conflict.

In short, our usual web framework is called MVC, so they put v this thing on the Java Programmer's body, poor my aesthetic ah ~ so we should continue to push v down, to the Java do not know anything, but there are very rich web designers. Can not let others to learn Java Ah, it has to write a Java object JS object, so that Web designers can easily call JS.

The general implementation process is this:

1, the two sides first discuss the needs of the project, and then determine what the next page needs to show what content, how to show regardless. After the discussion, we determined the general structure of JS object and database.

2, each write their own things ...

3, both sides write a Web page through the JS object and Java connection, debugging, completion.

Specific key code:

J2j.java code, the function is to get scope, the name is source Java object, and then write this Java object named Distname category for the Dist JS object.

Code:

/*


* J2j.java


*


* Created on October 2, 2006, 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;


} }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.