Struts HTML Link Tag ( < html:link >)

Struts HTML Tag Library

Struts HTML tag library provides tags which are used to create input forms and HTML user interfaces. The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms.

Syntax to use Struts HTML tag library

<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>

< html:link >

< html:link > -renders an HTML < a> element URL rewriting will be applied automatically, to maintain session state in the absence of cookies. The content displayed for this hyperlink will be taken from the body of this tag.Compulsory any of the attributes (forward,href,page,action) needs to be specified to redirect from base url.We can pass Parameters to the url by using < paramId>,< paramName > for one parameter and we can pass more than one parameter by using the Map objects which contains the key values pairs which determine the parameter id and parameter value.In this example we pass only Single Parameter.

Example Code for < html:link>.

1.Create an Jsp page and name it as linkpage.jsp.It is the Welcome page for a user.This page contains simple Header and a anchor which provides a link to another page.

linkpage.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="html" uri="http://jakarta.apache.org/struts/tags-html" %>
<%@taglib prefix="bean" uri="http://jakarta.apache.org/struts/tags-bean" %>

< html>
    < head>

        < title> HTML Link example </title>
    < head>
    < body bgcolor="#DDDDDD">
        < h1 > Struts html:link example </h1>

        < html:link page="/linkoutput.jsp" paramId="id" paramName="name"/> Click < /html:link>

		  </body>
</html>

2.Simple Action class linkaction.java which is a sub class of Action class used to load the parameter values into the page link.Firstly this action class is called and in this execute method contains an setAttribute() method attributes are added to the request.
linkaction.java

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

public class linkaction extends org.apache.struts.action.Action {

    private static final String SUCCESS = "success";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        request.setAttribute("name","Java");
        return mapping.findForward(SUCCESS);
    }
}

3.Create or modify struts config file struts-config.xml with action mappings.Struts-config file contains the information about the configuration of the struts framework to the application.It contains the action mappings which helps to select Action,ActionForm and other information for specific user’s request’s.In this file no need to specify the form bean as we are not using form bean to store.

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

< struts-config>

< action-mappings>
        < action  path="/link" type="com.myapp.struts.linkaction" >
        < forward name="success" path="/linkoutput.jsp"/>
		</action>
</action-mappings>

	</struts-config>

4.Change or modify the web.xml file by making the welcome file to index.jsp which contains the jsp forward to call action method to load parameter data to the request.
index.jsp

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

< jsp:forward page="link.do"/>

5.Building and running the application
Output

Access page:http://localhost:8084/link/
The param values are added to the URl.

also read:

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*