Recipe3.17.Using Frames


Recipe 3.17. Using Frames

Problem

You need to reference an Action or JSP page from within one frame to another.

Solution

Use the Struts html:frame tag to create frame elements with the proper URLs for your Struts application, as in Example 3-20 (frame_test.jsp).

Example 3-20. Generating a frame set with Struts
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <frameset cols="50%,*">   <html:frame frameName="_frame1"                     page="/frame1.jsp"              marginwidth="10"             marginheight="10"               scrolling="auto"              frameborder="1"/>   <html:frame frameName="_frame2"                     page="/frame2.jsp"              marginwidth="10"             marginheight="10"               scrolling="auto"              frameborder="1"/> </frameset>

Within the individual frame's source JSP, use the target attribute on the html:link and html:form tags to refer to the other frame. The JSP in Example 3-21 (frame1.jsp) demonstrates this approach.

Example 3-21. Frame with link to other frame
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head>   <title>Struts Cookbook - Chapter 04: Frame Test</title> </head> <body bgcolor="white"> <h2>Frame 1</h2>   <html:link href="frame2.jsp?calledBy=FRAME_1" target="_frame2">     Call Frame2   </html:link>   <p>   Message received:&nbsp;   <c:out value="${FrameForm.map.message}"/> </body> </html>

Likewise, frame2.jsp shown in Example 3-22 defines a form that submits to an Action and directs the result to _frame1 using the target attribute.

Example 3-22. Frame that submits to other frame
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <html> <head>   <title>Struts Cookbook - Chapter 04: Frame Test</title> <body bgcolor="white"> <h2>Frame 2</h2>   Send a message to frame 1!   <html:form action="CallFrame1" target="_frame1">     <html:text property="message"/>     <html:submit/>   </html:form>   <c:forEach var="paramValue" items="${paramValues}">     <br />     Parameter: <c:out value="${paramValue.key}"/><br />     Values:     <c:forEach var="theValue" items="${paramValue.value}">       <c:out value="${theValue}"/><br />     </c:forEach>   </c:forEach>   </body> </html>

Discussion

There are two key concerns when using HTML frames within a Struts application. First, the URLs of the frame elements within the frameset tag should be generated using the same rules used by the Struts html:link and html:rewrite tagsi.e., enter the html:frame tag. It supports the same action, page, and href attributes as the html:link and html:rewrite tags. The html:frame tag supports additional attributes specific to the HTML frame element. Most of these attributes are pass-throughs, and the attribute value is passed through without modification to the generated frame element.

Second, with frame-to-frame interaction, you will probably need a user request, made on one frame, to affect the display of another frame displayed on the same page. The key to making this communication work is the use of the target attribute on the html:form and html:link tags.

The target attribute specifies the window or frame that receives the results of a request. The request can come from a form submission or link. In the Solution, a frameset is created that contains two frames_frame1 and _frame2positioned on the left and right, respectively. Frame 1, as shown in Example 3-21, defines a link with the page attribute set to frame2.jsp and the target set to _frame2. Parameters are passed on the query string that will be displayed by frame2.jsp:

<html:link href="frame2.jsp?calledBy=FRAME_1" target="_frame2">     Call Frame2 </html:link>

Similarly, frame2.jsp, shown in Example 3-22, defines a form where the target is specified as _frame1:

<html:form action="CallFrame1" target="_frame1">

The form-bean and action for this example are defined in the struts-config.xml file:

<form-bean name="FrameForm" type="org.apache.struts.action.DynaActionForm">   <form-property name="message" type="java.lang.String"/> </form-bean>  ... <action    path="/CallFrame1"           name="FrameForm"          scope="request"           type="org.apache.struts.actions.ForwardAction"      parameter="/frame1.jsp"/>

When you first access frame_test.jsp and click the link on the lefthand frame, you'll see the data displayed in Frame 2. Likewise, when you submit the form on the righthand frame, you will see the results displayed in Frame 1. Figure 3-5 shows the frames after this series of interaction.

Figure 3-5. frame_test.jsp


If the target attribute was omitted, the output would be displayed in the same frame from where the request was made. Think of a frame as a browser within a browser. The Solution works the way it does because _frame1 and _frame2 are both part of the same frameset.

Other specially named targets are relevant to frames that are handled in specific ways by the browser. These special targets in described in Chapter 3.

Table 3-5. Special frame target values

Frame

Description

_blank

The browser loads the received request into a new browser window. This target is useful for creating pop ups.

_self

The browser loads the received request into the current window or frame. This is the default setting when the target is not specified.

_parent

The browser loads the received request into the parent window or frameset directly containing this frame. If the current reference is a top-level frame or window, this is equivalent to _self.

_top

The browser loads the received request into the window at the top of the hierarchy. This results in the request being displayed in the entire browser window.


Frames can get complicated, particularly if you use frames within frames as well as hidden frames. Many people prefer to steer clear of frames because of this complexity. Carefully consider your requirements when using frames. You may find that you can get similar behavior without the complexity of inter-frame communication using JSP includes or Tiles.

See Also

The Struts documentation for the html tag library provides complete details on the html:frame tag as well as the use of the target attribute. You can find this at http://struts.apache.org/userGuide/struts-html.html.

A great reference for details on frames can be found in HTML and XHTML: The Definitive Guide by Chuck Musciano and Bill Kennedy (O'Reilly).

Recipe 14.1 provides the basics of setting up Tiles for your application.



    Jakarta Struts Cookbook
    Jakarta Struts Cookbook
    ISBN: 059600771X
    EAN: 2147483647
    Year: 2005
    Pages: 200

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net