After Java 6 came out, its major feature was support for the scripting language. I used to give a rough look and did not feel any specific use. I recently learned about Java's special scripting language javafx, which is quite interesting.
I. The following are some related websites:
1. Sun javafx Official Website: .www.sun.com/software/javafx/script/
2. openjfx Website: openjfx.dev.java.net/
3. javafx: First steps-"Hello onjava" app www.oreillynet.com/onjava/blog/2007/05/javafx_first_steps_hello_onjav_1.html
4. The javafx Script Programming Language Reference (Chinese Version)
The Hello World Program is very simple. You can see it later.
2. I like the following features of this language:
This language integrates the features of many scripting languages, such as JavaScript, PHP, actionscript, and SQL.
1.Array Operations are distinctive.
VaR Nums = [0 .. 3];
... Is a range expression. The preceding statement is equivalent to VaR Nums = [, 2, 3];
Arrays support Query expressions similar to SQL statements.
VaR Nums = select N * n from N in [1 .. 100];
The result is an array of square numbers ranging from 1 to 100.
2.String
Strings in javafx support embedded expressions
- VaR name = 'job ';
- VaR S = "Hello {name }";
- System. Out. println (s );
When the program outputs the result, it automatically calculates the expression {} in the string and replaces it with the result. The output result of this program is: Hello Joe
This feature is similar to PhP. It is useful when used as a result output template. You do not have to create the tag syntax by yourself. Instead, you must use regular analysis to replace the syntax.
3.Class and Object
Object declaration can be performed using a syntax similar to JSON.
- Class person {
- Attribute name: string;
- Attribute parent: person inverse person. Children;
- Attribute children: person * inverse person. parent;
- Function getfamilyincome (): number;
- Function getnumberofchildren (): number;
- Operation marry (spouse: person );
- }
- VaR Chris = person {
- Name: "Chris"
- Children:
- [Person {
- Name: "Dee"
- },
- Person {
- Name: "Candice"
- }]
- };
4.Special reflection operations
Javafx reflection is amazing. I was wondering why Java reflection is not like this.
- Import java. Lang. system;
- System. Out. println (1. Class. Name) // prints "Number"
- System. Out. println ("hello". Class. Name); // prints "string"
Convenient?
3. Try on the web.
After reading the instructions, it seems that javafx aims to simplify swing interface development and programming. On the one hand, javafx does not have a good WYSIWYG ide support, and I am interested in Web programming. So I conducted a small experiment.
My idea is to use a servlet to intercept all requests to Fx, obtain the absolute path of the javafx File Based on requesturi, and then call the script engine of Java 6 in servelt to explain the execution.
Fxservlet. Java
Java code
- Import java. Io. file;
- Import java. Io. fileinputstream;
- Import java. Io. ioexception;
- Import java. Io. inputstreamreader;
- Import javax. servlet. servletexception;
- Import javax. servlet. http. httpservlet;
- Import javax. servlet. http. httpservletrequest;
- Import javax. servlet. http. httpservletresponse;
- Import javax. Script. bindings;
- Import javax. Script. scriptcontext;
- Import javax. Script. scriptengine;
- Import javax. Script. scriptenginemanager;
- Import javax. Script. simplescriptcontext;
- Public class fxservlet extends httpservlet {
- Private string realpath;
- Public void doget (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception {
- This. dowork (request, response );
- }
- Public void dopost (httpservletrequest request, httpservletresponse response)
- Throws servletexception, ioexception {
- This. dowork (request, response );
- }
- Public void dowork (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
- System. Out. println ("begin ....");
- Try {
- Classloader loader = thread. currentthread (). getcontextclassloader ();
- Scriptenginemanager manager = new scriptenginemanager (loader );
- Scriptengine engine = manager. getenginebyextension ("FX ");
- Bindings bindings = engine. createbindings ();
- // Pass the request and response objects to the script engine.
- Bindings. Put ("Request: javax. servlet. http. httpservletrequest", request );
- Bindings. Put ("response: javax. servlet. http. httpservletresponse", response );
- Scriptcontext context = new simplescriptcontext ();
- Context. setbindings (bindings, scriptcontext. global_scope );
- Context. setbindings (bindings, scriptcontext. engine_scope );
- Engine. setcontext (context );
- String uri = request. getrequesturi ();
- String contextpath = request. getcontextpath ();
- // Replace the contextpath in requesturi
- If (contextpath! = NULL & contextpath. Trim (). Length ()> 0 ){
- Uri = URI. replaceall (contextpath ,"");
- }
- // Get the absolute address of the FX file according to requesturi. We put the FX file under the jfx directory under the WEB-INF of the site.
- String filepath = This. realpath + "WEB-INF" + file. Separator + "jfx" + URI;
- File jfxfile = new file (filepath );
- If (! Jfxfile. exists ()){
- Response. senderror (404 );
- } Else {
- Inputstreamreader reader = new inputstreamreader (New fileinputstream (jfxfile ));
- Engine. eval (Reader );
- Reader. Close ();
- }
- } Catch (exception e ){
- E. printstacktrace ();
- }
- System. Out. println ("... end ");
- }
- Public void Init () throws servletexception {
- This. realpath = This. getservletcontext (). getrealpath ("/");
- }
- }
Web. xml
XML Code
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Web-app version = "2.4"
- 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
- Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
- <Servlet>
- <Servlet-Name> fxservlet </servlet-Name>
- <Servlet-class> net. teamhot. jfxweb. fxservlet </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <Servlet-Name> fxservlet </servlet-Name>
- <URL-pattern> *. FX </url-pattern>
- </Servlet-mapping>
- </Web-app>
Hello. FX
- Import java. Lang. system;
- Import javax. servlet. http. httpservlet;
- Import javax. servlet. http. httpservletrequest;
- Import javax. servlet. http. httpservletresponse;
- Import java. Io. printwriter;
- // Accept parameters passed in from Java
- VaR request: httpservletrequest = request;
- VaR response: httpservletresponse = response;
- VaR name = request. getparameter ("name ");
- VaR out: printwriter = response. getwriter ();
- VaR template = "Hello world! {If name = NULL then 'guest 'else name }";
- Out. Print (Template );
Run: http: // localhost: 8080/jfx/Hello. FX
Result: Hello world! Guest
Run http: // localhost: 8080/jfx/Hello. FX? Name = jolestar
Result: Hello world! Jolestar
Note: Tomcat runs in the jdk6 environment. If you still cannot find javax. script. scriptengine. javax. extract the script package, repackage it, and put it under Tomcat common Lib.
From: http://jolestar.javaeye.com/blog/107017