Javafx early adopters

Source: Internet
Author: User
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

 
  1. VaR name = 'job ';
  2. VaR S = "Hello {name }";
  3. 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.

  1. Class person {
  2. Attribute name: string;
  3. Attribute parent: person inverse person. Children;
  4. Attribute children: person * inverse person. parent;
  5. Function getfamilyincome (): number;
  6. Function getnumberofchildren (): number;
  7. Operation marry (spouse: person );
  8. }
  9. VaR Chris = person {
  10. Name: "Chris"
  11. Children:
  12. [Person {
  13. Name: "Dee"
  14. },
  15. Person {
  16. Name: "Candice"
  17. }]
  18. };


4.Special reflection operations
Javafx reflection is amazing. I was wondering why Java reflection is not like this.

  1. Import java. Lang. system;
  2. System. Out. println (1. Class. Name) // prints "Number"
  3. 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
  1. Import java. Io. file;
  2. Import java. Io. fileinputstream;
  3. Import java. Io. ioexception;
  4. Import java. Io. inputstreamreader;
  5. Import javax. servlet. servletexception;
  6. Import javax. servlet. http. httpservlet;
  7. Import javax. servlet. http. httpservletrequest;
  8. Import javax. servlet. http. httpservletresponse;
  9. Import javax. Script. bindings;
  10. Import javax. Script. scriptcontext;
  11. Import javax. Script. scriptengine;
  12. Import javax. Script. scriptenginemanager;
  13. Import javax. Script. simplescriptcontext;
  14. Public class fxservlet extends httpservlet {
  15. Private string realpath;
  16. Public void doget (httpservletrequest request, httpservletresponse response)
  17. Throws servletexception, ioexception {
  18. This. dowork (request, response );
  19. }
  20. Public void dopost (httpservletrequest request, httpservletresponse response)
  21. Throws servletexception, ioexception {
  22. This. dowork (request, response );
  23. }
  24. Public void dowork (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  25. System. Out. println ("begin ....");
  26. Try {
  27. Classloader loader = thread. currentthread (). getcontextclassloader ();
  28. Scriptenginemanager manager = new scriptenginemanager (loader );
  29. Scriptengine engine = manager. getenginebyextension ("FX ");
  30. Bindings bindings = engine. createbindings ();
  31. // Pass the request and response objects to the script engine.
  32. Bindings. Put ("Request: javax. servlet. http. httpservletrequest", request );
  33. Bindings. Put ("response: javax. servlet. http. httpservletresponse", response );
  34. Scriptcontext context = new simplescriptcontext ();
  35. Context. setbindings (bindings, scriptcontext. global_scope );
  36. Context. setbindings (bindings, scriptcontext. engine_scope );
  37. Engine. setcontext (context );
  38. String uri = request. getrequesturi ();
  39. String contextpath = request. getcontextpath ();
  40. // Replace the contextpath in requesturi
  41. If (contextpath! = NULL & contextpath. Trim (). Length ()> 0 ){
  42. Uri = URI. replaceall (contextpath ,"");
  43. }
  44. // 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.
  45. String filepath = This. realpath + "WEB-INF" + file. Separator + "jfx" + URI;
  46. File jfxfile = new file (filepath );
  47. If (! Jfxfile. exists ()){
  48. Response. senderror (404 );
  49. } Else {
  50. Inputstreamreader reader = new inputstreamreader (New fileinputstream (jfxfile ));
  51. Engine. eval (Reader );
  52. Reader. Close ();
  53. }
  54. } Catch (exception e ){
  55. E. printstacktrace ();
  56. }
  57. System. Out. println ("... end ");
  58. }
  59. Public void Init () throws servletexception {
  60. This. realpath = This. getservletcontext (). getrealpath ("/");
  61. }
  62. }

Web. xml

XML Code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Web-app version = "2.4"
  3. Xmlns = "http://java.sun.com/xml/ns/j2ee"
  4. Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
  5. Xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee
  6. Http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd>
  7. <Servlet>
  8. <Servlet-Name> fxservlet </servlet-Name>
  9. <Servlet-class> net. teamhot. jfxweb. fxservlet </servlet-class>
  10. </Servlet>
  11. <Servlet-mapping>
  12. <Servlet-Name> fxservlet </servlet-Name>
  13. <URL-pattern> *. FX </url-pattern>
  14. </Servlet-mapping>
  15. </Web-app>

Hello. FX

 
  1. Import java. Lang. system;
  2. Import javax. servlet. http. httpservlet;
  3. Import javax. servlet. http. httpservletrequest;
  4. Import javax. servlet. http. httpservletresponse;
  5. Import java. Io. printwriter;
  6. // Accept parameters passed in from Java
  7. VaR request: httpservletrequest = request;
  8. VaR response: httpservletresponse = response;
  9. VaR name = request. getparameter ("name ");
  10. VaR out: printwriter = response. getwriter ();
  11. VaR template = "Hello world! {If name = NULL then 'guest 'else name }";
  12. 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

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.