查看文章 |
IntroductionThere are a lot of messages on SDN forums regarding using third-party libraries in SAP J2EE applications. Also there is a complete section available at http://help.sap.com, many developers either find it incomplete or cannot find it at all (I bet majority even don’t try to search, but this is different topic). Below is step-by-step guide how to deploy Rhino JavaScript interpreter library to J2EE server and use it in WebDynpro application. Step 0. PreparationFirst download latest Rhino build from mozilla.org and extract <var>js.jar</var> file from Zip archive. Mozilla download page contains broken links, here is direct link to Rhino 1.6 R2 / RC2. Next, activate "Development Configurations" perpective in NetWeaver IDE (use menu path: "Window -> Open Perpective -> Other..."). We will create all necessary development components (DC for short) in Local Development / My Components branch. Activate "Development Configurations" view. Step 1. Create and build “External Library”Switch to "Development Configurations" perpective if you are not already there, drill down to "Local Development -> My Components", right-click and select "Create New DC..." command Running "Create New DC..." dialog. Now fill in all necessary fields as shown below and click finish. Create "External Library" DC. IDE will create and open automatically necessary project. In root project directory find a folder <var>libraries</var> (in my environment NetWeaver shows it in project tree, but does not create it on disk - just create it manually). Now copy <var>js.jar</var> from Rhino distribution here. Next, select <var>library</var> folder in IDE, right-click on it and call "Refresh" command. After IDE synchronized changes with file system, right-click on jar file in tree and select "Development Component -> Add To Public Part" Adding Aarchive (js.jar) to public part NetWeaver pre-create <var>ExternalLibs</var> public part for this type of projects, so simply select it in appeared dialog and click "OK". Afterwards right-click on project itself and run command "Development Component -> Build". Step complete. Step 2. Create, build, deploy “J2EE Server / Library”After we have created and built <var>External Library</var> DC we need to deploy it to SAP J2EE server somehow. So next create "J2EE Server Component / Library" DC: Create "J2EE Server Component / Library" DC Note that during project createion NetWeaver IDE may prompt you to open <var>JEE Development Perspective</var>. Just agree with it. Along this it will open <var>J2EE DC Explorer</var> view (switch to this view yourself if this doesn't happen). Now expand project structure to "DC Metadata -> DC Definition -> Used DCs". Activate contextual menu on last node and invoke "Add Used DC...". Add reference to the public part of previously created <var>Extrenal Library</var> here. Note, that you have to specify both build time and run-time dependency here. Otherwise created SDA file will not contain js.jar from external library. Add used DC (External Library) to J2EE library It is time to build the project and analyze results. First of all you have to check generated SDA file in <var>gen/default/deploy</var> folder. Verify that it contains <var>js.jar</var>. Now something interesting. Go to <var>gen/default/public</var> folder. You should find here <var>defLib</var> folder. Yes, NetWeaver IDE again created public part, this time it is <var>defLib</var>. And if you drill down to <var>gen/default/plublic/defLib/lib/java</var> then you meet <var>js.jar</var> file again. From now you may refere this (defLib) public part in the rest of SAP J2EE projects. But before we have to deploy it!. So, context menu on SAP J2EE Component / Library project, "Development Component -> Deploy". Everything should go smoothly, let us verify results and run Visual Administrator: Scroll to Libraries node JavaScript (Rhino) Library Deployed Step 3. Create WebDynpro Development ComponentNow you may refer JavaScript library from any J2EE project. Obviously, that I wiil use it from WebDynpro ;) So, two initial tasks should be known for you: we will create WebDynpro Development Component and add <var>defLib</var> public part of <var>js/lib</var> DC to it (the only difference is that here build-time dependency is enough): Create "WebDynpro" DC. Add used DC (defLib) to WebDynpro DC However, WebDynpro project will not contain JavaScript library files (as in case with J2EE library). So we have to define <var>reference</var> to aforementioned library to let class loader find necessary classes at run-time. Run contextual menu on project root in IDE and select "Properties", then switch to "WebDynpro References -> Library references" and add "mozilla.org~js~lib" (you may see the name in Visual Administrator): Add run-time library reference for WebDynpro DC Back to component itself. It is extremely trivial: single view with 2 IWDTextArea-s for JavaScript source and script output, one button and corresponding action handler. Here is the code of action handler: final Context ctx = Context.enter(); And, finally, here is running application: JavaScript Console Heck, we "invent" tool before task ;) If anyone has ideas how scripting could be applied in WebDynpro you are welcome to share your thoughts in comments!
From: https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/2361 |