Programming Tutorials

IncludeAction in Struts

By: Fazal in Struts Tutorials on 2003-05-04  

The IncludeAction is a Struts action class that is used to include the content of one JSP page within another JSP page. It is typically used when you have common content that you want to reuse across multiple JSP pages, such as a header or footer.

The IncludeAction class extends the org.apache.struts.action.Action class and provides a method named execute() that is invoked when the action is called. The method includes the content of the specified JSP page using the RequestDispatcher object.

Here's an example of using the IncludeAction in Struts:

  1. Define an action in your struts-config.xml file:
<action path="/includeExample" type="org.apache.struts.action.IncludeAction">
    <forward name="success" path="/WEB-INF/jsp/include-example.jsp"/>
</action>
  1. Create a JSP page that contains the content you want to include:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Include Example</title>
</head>
<body>
    <h1>Header</h1>
    <p>This is the header content.</p>
</body>
</html>
  1. Create another JSP page that includes the first page using the IncludeAction:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Include Example</title>
</head>
<body>
    <html:form action="/includeExample">
        <html:submit value="Include"/>
    </html:form>
    <br/><br/>
    <h1>Page Content</h1>
    <p>This is the page content.</p>
    <html:include page="/include-example.jsp"/>
    <h1>Footer</h1>
    <p>This is the footer content.</p>
</body>
</html>

In this example, the IncludeAction is used to include the content of the include-example.jsp page within the main JSP page. The html:include tag is used to specify the page to include.

Note that the IncludeAction is not commonly used in modern web development, as most developers now prefer to use template engines or server-side includes to achieve the same functionality.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Struts )

Latest Articles (in Struts)