Hello World - WebSphere Portal V5 The simplest portal: Part 2, rendering JSP

xiaoxiao2021-03-06  83

This article will learn how to develop and deploy a portlet-based simple JSP in IBM WebSphere Portal Version 5.

Introduction In the first article of the "Hello World" sequence, you have already learned how to create a portlet in Java, which can output "Hello, World". This is exemplary, not a practical method for developing portlets. So, what kind of problem is it? This article (second article) solves one of the problems in the future.

In this simplest "Hello World!" Program, the Java code contains text "Hello, World", and also contains logic that displays this text. If you can separate the portlet's logic and the part of the logic, the program will do better because this allows you to create a portlet that supports different dollars and different national languages. Today, you may not need to create a portlet for wireless tag language (Wireless Markup Language, WML), or do not need to create a German version of Portlet. But what about tomorrow?

You may also work with a graphic designer, and he frequently changes the appearance and feel. If the presentation part is done in the Java code, a developer and a editorial processor need to be modified. The way to solve this problem is to use JSP to present your portlet, which allows you to create a separate JSP file, each file supporting a metadata or a national language. Designers can fully control JSP and make changes to it, and no longer need to assist in developers.

In this article, you will see how to build a portlet in IBM® WebSphere® Portal Version 5, which calls a JSP to present it in it. Then we will introduce this JSP and some tags from the portlet tag library. Finally, create a deployment descriptor and put all these things into a package and deploy it into the portal.

Creating a directory structure First, you must create a directory structure where your portlet can be stored. The following is that you will be used in the portlet:

HelloWorld / COM / IBM / Portlets / Sample - The location of the source code is stored. HelloWorld / Web-INF - Stores the location of the deployment descriptor. HelloWorld / Web-INF / LIB - The location of the JAR file is stored. HelloWorld / JSP - Stores the location of the JSP file.

Important: All directory and environmental references are based on Windows practices. You can do some corresponding adjustments to make it suitable for UNIX systems.

Creating a Java Code Sample Directory is where you store the Java source file. Create a file called HelloWorld.java in this directory and open it with your favorite text editor. Below you need to type it (or cut and paste into) the HelloWorld.java file created:

Package com.ibm.portlets.sample;

// portlet APIT

Import org.apache.jetspeed.portlet. *;

// java stuff

Import java.io. *;

Public class helloworld extends portletadapter {

Public Void Service (PortletRequest Request, PortletResponse Response)

Throws portletException, IOEXCEPTION {

// incrude the view jsp

GetPortletConfig (). getContext (). include ("/jsp/view.jsp",

REQUEST, RESPONSE;

}

}

After the code creates a source file, you can compile the Java code. You can use the following scripts to compile this portlet in a batch file. First define some environment variables, such as defining the environment variables of the Java installation location (java_home). Compiling the JDK provided by WebSphere Application Server is usually a good idea because this is the environment you want to run.

You also need to set the PATH environment variable to include the installation location of the Java compiler. Also set the libpath variable to point to the directory where the WebSphere JAR file is located.

Next, design a variable called CP for our compile class path. The CP variable can be built on a row, but in order to illustrate the different JAR files required, we disassemble the branch display. Then call the Java compiler to compile the code. The code assumes that you are in the HelloWorld directory. Adjust the directory path to the path to your installation.

Set java_home = c: / WebSphere / AppserVer / Java

SET PATH =% java_home% / bin

SET LIBPATH = C: / WebSphere / AppServer / LIB

SET CP =.

SET CP =% CP%;% libpath% / j2ee.jar

SET CP =% CP%;% libpath% / Dynacache.jar

SET CP =% CP%; c: /websphere/portalser/shared/app/portlet-api.jar

Javac-ClassPath% CP% COM / IBM / Portlets / Sample / HelloWorld.java

If this is not compiled because some reason, you must correct the error before proceeding. Some common mistakes are:

Enter an error - class name, path, and variable name input error. File Name Error - File Name must match the class name. In our example, the file name is HelloWorld.java, and the class name is HelloWorld. If you have changed a name, another name must also change. Editor Error - Determines the editor to store files as text. An editor like a writing panel does not store the file to be stored as text.

Creating a JAR file After completing the compilation of the Java source file, you need to create a JAR file in a web-inf / lib directory. It is assumed that you are located in the HelloWorld directory. These statements can be included as part of the batch file you created above. This depends on the PATH set to point to the JAR program.

Set java_home = c: / WebSphere / AppserVer / Java

SET PATH =% java_home% / bin

Jar -cv0f ./web-inf/lib/helloworldfromjsp.jar COM / IBM / Portlets / Sample / *. Class

After creating a JSP file, you can concentrate on creating a JSP file after the JSP file is compiled. The JSP directory is where the JSP source file is stored. Create a file called View.jsp in this JSP directory and open it with your favorite text editor. Here is some JSP code, you need to type them (or cut and paste into the view.jsp file:

Hello World from the JSP!

It's that simple. Now you can pack them and run! After that, you can look back at this JSP and see what you can add to support different national languages ​​and different meta languages. Create a deployment descriptor now, you need to create two XML files:

HelloWorld / Web-INF / Web.xml - Web Deployment Descriptor. HelloWorld / Web-INF / portlet.xml - portlet deployment descriptor.

Web.xml - Web Deployment Descriptor Web Deployment Descriptor is required for WebSphere Portal. Now the portlet extends servlet, so you need to use the web deployment descriptor to declare the Servlet (portlet) class.

Application 2.3 // en "

"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

HelloWorldFromjspportlet

HelloWorldFromJsp

com.ibm.portsets.sample.HelloWorld

HelloWorldFromJsp

/ helloworldfromjsp / *

Portlet.xml - Portlet Deployment Descriptor Portlet Deployment Descriptor defines portlets to the portal. Each portlet is mapped to a servlet defined in the web deployment descriptor through the HREF property of the portlet element. These references are represented by bold. Portlets must define a unique ID, each of which can reference the ID. Each specific portlet uses the given ID of the HREF attribute of the concrete-portlet element to reference the portlet. Take these references in blue.

1.1 // en "" portlet_1.1.dtd ">

Minor-version = "0"> HelloWorldFromJSP0Portlet

Minor-version = "0">

HelloWorldFromJsp

0

no

concrete helloworldfromjsp

author

tcat@us.ibm.com

HelloWorldFromJsp

en

Hello World from JSP </ Title></p> <p></ logage></p> <p></ concrete-portlet></p> <p></ concrete-portlet-app></p> <p></ portlet-app-def></p> <p>Many places will be wrong when you create these XML files. These errors are only discovered when you try to deploy portlets to the portal. There are some matter here to pay special attention:</p> <p>Check each XML element to make sure they have an off element. Check if the name is incorrect. For this example, check if your name is HelloWorldFromJSP. Make sure the ID and HREF properties match. Make sure the editor really saves files into text types.</p> <p>Creating a WAR file Finally, we can create a distributed WAR file. We will use standard JAR commands to build a WAR file. Run the following command in the HelloWorld directory:</p> <p>Set java_home = c: / WebSphere / AppserVer / Java</p> <p>SET PATH =% java_home% / bin</p> <p>Jar -cf HelloWorldFromJsp.War Web-INF JSP deployed WAR file</p> <p>Log in to the portal in the identity (wpsadmin) of the portal administrator. Select the Administration link in the upper right corner of the default theme. Figure 1. The administrator links to the left to select the portlets page and select Install portlet. Figure 2. Install the portlet management page Click. Find the location of the WAR file, selected, and then click. Figure 3. File Selection Dialog Click. This may take some time, please wait patiently to complete it. Verify that the portlet is HelloWorld. Click. Wait again until the installation is complete. Figure 4. Portlet Installation Verification Screen Finally, you will see this confirmation message:.</p> <p>If you encounter a class error message during the deployment process, you usually means that you have an error in an XML file. Try carefully analyze the error message presentation to your error message, you usually find the reason for the error. This information will also be recorded in the latest log files:% wps_home% / log / wps_yyyy.mm.dd-hh.mm.ss.log</p> <p>There are also some errors that are common in XML files. You should also check the following:</p> <p>The XML declaration must be on the first line of the file (no space). In the case of correct case, the XML file has the correct name.</p> <p>Add portlet to a page</p> <p>Select the Portal User Interface page on the left. Click Manage Pages. Figure 5. Management page portlet then select My Portal page. Click. Type a title for the new page. Click. I saw later. Click the Edit page icon on the side of the new page. Click. Search Hello, click. Check the Hello World Portlet and click. Figure 6. Find portlets Click to complete the page layout design. Figure 7. Editing Layout Design Click the My Portal link in the upper right corner, then select the page you created. Figure 8. Portal showing the portal we created</p> <p>I18N and multiple metadata language I18n are an international general interpolation word. This term refers to a way in some way that a application (your portlet) is designed to use this way can apply to different languages ​​and Region, no need to modify the code. This implementation method is unable to support multiple national languages ​​or multiple metadata languages. Portlets that use JSP to rendering two types of support methods can support I18n, which one can help you implement multiple metadata languages.</p> <p>The first type of support is to use multiple JSP to present your portlet. This support method allows a portlet to have different layup design, color, pictures, text, and other representations specific to the supported language, regional and meta-language.</p> <p>Your JSP directory structure is /jsp/View.jsp. The portal uses this directory as a base for searching for a most suitable JSP. The following is a search order for web browsers set to English (US) place:</p> <p>/jsp/html/en_us/view.jsp</p> <p>/jsp/html/en/view.jsp</p> <p>/jsp/html/view.jsp</p> <p>/jsp/view.jsp</p> <p>The first view.jsp found to render the portlet. This convention allows you to specify different JSPs for different national languages. For example, you can create a view.jsp in the / JSP / HTML / DE / directory. It will read "Hallo Welt" (this is what I want, "said" Hello World "in German. The central idea is to say that each language and each region can have its own JSP, which provides special layout design and Verbage. And what about different meta-language? In the search path above, you can find the specified / html /. You can give the portlet as the following path to give JSP: /jsp/html/en_us/view.jsp</p> <p>/jsp/html/en/view.jsp</p> <p>/jsp/html/de/view.jsp</p> <p>/jsp/html/view.jsp</p> <p>/jsp/wml/en_us/view.jsp</p> <p>/jsp/wml/en/view.jsp</p> <p>/jsp/wml/de/view.jsp</p> <p>/jsp/wml/view.jsp</p> <p>/jsp/view.jsp</p> <p>Having different JSPs allows your portlet users to access by WML browser, and portlets can perform search using JSP in the / WML / directory structure. So, this is the first level of I18n.</p> <p>The second level that provides help is in the form of a Java resource bindle. For your portlet, Java resource binding has such a capability that the string can be placed in an easy interpretation, and the JSP code independently independently independently. You can use the same layout design, but present the text in the correct national language. In order to do this in WebSphere Portal V5, you need to go deep into the world of JSTL (JSP Standard Tab Library, JSP Standard Tag Libraries). Previously, you are using the IBM portlet tag library with WebSphere Portal from JSP access resource binding. In V5, you can still do this, but it has already deprecated text tags, and the future vision is JSTL.</p> <p>JSTL for I18n provides a wide variety of features for JSP tags. In fact, JSTL is four different tag libraries:</p> <p>Core Tag Library - It provides an expression language and a general control process tag. For example, IF and cyclic marks. Format Tag Library - it provides I18n feature, such as retrieving messages from resource binding, Date, Time, and other formatted tags. Database Access Tag Library - allows you to access the database directly from the JSP. XML Tag Library - Various XML tags allow you to resolve and convert XML documents. I found that JSTL's database and XML section are often conflict with other tag libraries in WebSphere Portal. For example, when I enable the database tag library to enter a portlet using the DB2 data source, I have encountered a class conflict, which makes me unable to access my database. There is also when I contain the XML tag library to enter a portlet, I found that my JSP can not be compiled again.</p> <p>These small problems only appear in WebSphere Portal V5.0.2. I finally found that the JSTL XML tag library conflicts. So I suggest you reduce JSTL, allowing it to contain only formatted tag libraries and core tag libraries unless you feel that you really need JAR files have a good solution to conflicts.</p> <p>Speaking of the JAR file, you can download the JSTL library from the Standard Tag Library, or getting started from the standard tag library to find your own mirror site, download it from it. This article describes Version 1.0.</p> <p>When you decompress the downloaded tag library, you will find a few directories that have already been created. Two of them are most interested in the lib and TLD directories. You need to copy two JAR files from the lib directory to your web-inf / lib directory, these two files are jstl.jar and standard.jar. All other tag libraries don't need to be used, and they don't hinder you to perform portlets, as described above. You also need to create a web-inf / TLD directory. Please copy C.TLD, C-RT.TLD, FMT.TLD, and FMT-RT.TLD from the TLD directory to your web-INF / TLD directory. These files define the kernel (C) tag library and formatted (FMT) tag library. Now you may want to need two different TLD files. JSTL has a "Twin Libraries" concept, which is not within the discussion of this article. Briefly, it is a way to use JSTL representation, while the other allows you to use JSP.</p> <p>Yes, it is jSTL from the outside, and it is two ways to express in the backend. You may have to start being dissatisfied, complaining that I am unfair to JSTL, yes, I know, I have a little bit a little bit of jstel, I also encourage you to write an article about JSTL. You are an elegant reader, you have to interpret some resources that are better than I write, learn from JSTL. You can use Google to find it, you can also go to your online bookstore. You will find a lot of resources, but remember is JSTL 1.0!</p> <p>In order to use JSTL in your portlet to access resource bindings, you have made most preparatory work. Now, you must modify the web.xml file to declare the tag library you will use. Please note the Taglib tagged behind the servlet-mapping tag.</p> <p><? XML Version = "1.0" encoding = "UTF-8"?></p> <p><! Doctype web-app public "- // sun microsystems, inc .//dtd Web</p> <p>Application 2.3 // en "</p> <p>"http://java.sun.com/j2ee/dtds/web-app_2_3.dtd"></p> <p><web-app id = "HelloWorldFromjspWebApp"></p> <p><Display-name> HelloWorldFromjspportlet </ display-name></p> <p><servlet ID = "servlet_1"></p> <p><servlet-name> HelloWorldFromJsp </ servlet-name></p> <p><servlet-class> com.ibm.portsets.sample.HelloWorld </ servlet-class></p> <p></ servlet></p> <p><servlet-mapping id = "servletmapping_1"></p> <p><servlet-name> HelloWorldFromJsp </ servlet-name></p> <p><url-pattern> / helloworldfromjsp / * </ url-pattern></p> <p></ servlet-maping></p> <p><taglib id = "taglibrev_jstl_fmt"></p> <p><taglib-uri> http://java.sun.com/jstl/fmt </ taglib-uri> <taglib-location> /Web-inf/tld/fmt.tld </ taglib-location></p> <p></ taglib></p> <p><taglib id = "taglibrev_jstl_fmt_rt"></p> <p><taglib-uri> http://java.sun.com/JSTL/FMT_RT </ taglib-uri></p> <p><taglib-location> /web-inf/tld/fmt-rt.tld </ taglib-location></p> <p></ taglib></p> <p><taglib id = "taglibrev_jstl_core"></p> <p><taglib-uri> http://java.sun.com/jstl/core </ taglib-uri></p> <p><taglib-location> /web-inf/tld/c.tld </ taglib-location></p> <p></ taglib></p> <p><taglib id = "taglibrev_jstl_core_rt"></p> <p><taglib-uri> http://java.sun.com/jstl/core_rt </ taglib-uri></p> <p><taglib-location> /web-inf/tld/c-rt.tld </ taglib-location></p> <p></ taglib></p> <p></ web-app></p> <p>You have already defined all the tag libraries you might use. In this article, you don't need to use all, but I think it doesn't have any harm to do it. In addition, I tell you how to define all tag libraries, don't need you to think yourself in your portlet!</p> <p>Now enter the real JSP code section. In order to use the JSTL formatted tag library, you need to add the following instructions at the beginning of JSP to access the formatted tag library:</p> <p><% @ Taglib Uri = "http://java.sun.com/jstl/fmt" prefix = "fmt"%></p> <p>You can use a similar instruction to take advantage of the core library. Now we have to use the formatting library to retrieve messages from resource bindings.</p> <p><fmt: setBundle Basename = "nls.labels" /></p> <p><fmt: message key = "helloworldLabel" /></p> <p>These codes will try to get "HelloWorldLabel" from the tag feature file. If the appropriate feature file is not found, the content between the Opening and Closing text tags is present in the default, in our example is "Hello World from the JSP!". In Java, search feature files and search criteria ResourceBundles are the same:</p> <p><basename> _ <lang> _ <country> _ <variant></p> <p><basename> _ <lang> _ <country></p> <p><basename> _ <lang></p> <p><basename></p> <p>In this exercise, you have to create two feature files:</p> <p>/Web-inf/classes/nls/labels_en.properties</p> <p>/Web-inf/classes/nls/labels_de.properties</p> <p>They will have the following contents: /Web-inf/classes/nls/Labels_en.properties:</p> <p>HelloWorldLabel = Hello World!</p> <p>/Web-inf/classes/nls/labels_de.properties:</p> <p>HelloWorldLabel = Hallo Welt!</p> <p>Summarize, JSP file /jsp/View.jsp has the following content:</p> <p><% @ Taglib Uri = "http://java.sun.com/jstl/fmt" prefix = "fmt"%></p> <p><fmt: setBundle Basename = "nls.labels" /></p> <p><fmt: message key = "helloworldLabel" /></p> <p>If you set the language to English in the HTML browser, this JSP file, together with the two feature files, generate the following:</p> <p>Figure 9. English display Hello World portlet</p> <p>Then set the language to German in our HTML browser, the portlet will generate:</p> <p>Figure 10. German Show Hello World Portlet</p> <p>Extremely Beautiful! But you may have to ask, how to update the portlet that has been deployed in the portal? You can delete this portlet directly and then redeploy it. But in a real portal environment, this will lose user data and deployment information. The last section of this article will briefly introduce how to update a deployed portlet.</p> <p>Update a deployed portlet</p> <p>Log in to the portal as a portal administrator (WPSADMIN). Select the Administration link in the upper right corner of the default theme. Figure 1. The administrator links to the left to select the portlets page, and then click Install to install the portlet. Select the Manage Applications Portlet. Figure 11. Managing portlets screens in the Web Modules list box, select HelloWorldFromJsp.war entry. Click the button. Click the button. Find the location of the WAR file, select it and click. Click, this may take some time, please wait patiently to complete it. Verify that portlet is really HelloWorldFromJSP, click, and then wait for the installation to complete. Finally, you will see confirmation information.</p> <p>Conclusion This is over. This article lets you understand how to use JSP to present your portlet. This article describes how to write, compile and package Java code for a portlet. First create a deployment descriptor and package the portlet to distribute and deploy; then deploy the portlet to your portal; finally, rewritten JSP, make it international, and update the deployment portlet. Portlet development courses allow you to learn again. Good luck! have fun!</p> <p>Back to top</p> <p>Reference</p> <p>WebSphere Portal Product Document WebSphere Portal Strip JSTL Specification</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-92449.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="92449" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.032</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'nUs74m9rwPr8Zo6DrAKEnwoxpcDjJabF03u9VZNZFWj9CxQ_2FlTYiEGVxlJx_2Bw5q1aJvU6SxNsCgAvf5bpxnOrg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>