Struts 2 Include Tag helpful for directly inserting another JSP or HTML page to the current page. This tutorial shows how to use the include tag.
1. Action Class
[code lang=”java”]
package javabeat.net.struts2;
public class Struts2HelloWorldAction {
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String execute(){
return "success";
}
}
[/code]
2. Include Tag Example
IncludeTag.jsp
[code lang=”xml”]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:include value="Output.jsp">
<s:param name="param1" value="value1"/>
<s:param name="param2" value="value2"/>
</s:include>
</body>
</html>
[/code]
Output.jsp
[code lang=”xml”]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Param 1 : ${param1}
Param 2 : ${param2}
</body>
</html>
[/code]
3. Struts.xml
[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="tags" extends="struts-default">
<action name="includetag" class="javabeat.net.struts2.Struts2HelloWorldAction"
method="execute">
<result name="success">/IncludeTag.jsp</result>
</action>
</package>
</struts>
[/code]
Leave a Reply