<< Back - Alkacon logo

OpenCms 6.0 interactive documentation:

Step 4: Using a JSP template from scriptlet

OpenCms logo - Forward >>

Using JSP templates without the OpenCms taglib

In the previous steps you learned how to use JSP based templates with the OpenCms  <cms:> taglib.

If you want to create a scriptlet based JSP without using the taglib, there is another way to include template elements in your JSP. This is achieved by the JavaBean org.opencms.jsp.CmsJspActionElement . This Bean provides direct access to the functionality offered by the OpenCms taglib. It is described in detail in the com.alkacon.documentation.documentation_scriptlet module.

Here's a simple JSP that uses the template from the previous example without the taglib (check out the file example-jsp-template-action.jsp to see this in action):

<%	
// create a JSP action element
org.opencms.jsp.CmsJspActionElement cms =
new org.opencms.jsp.CmsJspActionElement(pageContext, request, response);

// load the template head
cms.include("/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto-complete.jsp", "head");

// generate some output
out.println(new java.util.Date());

// load the template foot
cms.include("/system/modules/com.alkacon.documentation.howto_template/jsptemplates/howto-complete.jsp", "foot");
%>

The include() method here needs two String parameters, the first is the path and filename to the included JSP template, the second the name of the selected element. Instead of writing the name of the JSP template file directly in your JSP like we did in this example, you can also combine the methods of the CmsJspActionElement. For example you can use the method  cms.property("template", "search") which returns the value of the "template" property if you attached this to the JSP (or a parent folder). This way you can achieve exactly the same result as using the <cms:include property="template" file="search" /> tag from the OpenCms taglib.

Whether you prefer using the taglib or the Java Bean is a matter of application (or taste). Both options execute the same code inside OpenCms. It is up to you to make your choice.

Conclusion

After working through this howto, you should be able to create your own JSP based templates in OpenCms. JSP based templates can be used for "editable" HTML pages and also for "dynamic" JSPs as outlined in our examples. Of course, not all options of the OpenCms taglib or scriptlet API available to build templates with have been explained in detail here. You should consult the reference modules for this libraries for more in-depth information.

©2003-2005 Alkacon Software (http://www.alkacon.com) - The OpenCms experts