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;
} }