Struts HTML Text AreaTag ( < html:textarea > )

SHARE & COMMENT :

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

&lt;%@ taglib prefix=&quot;html&quot; uri=&quot;http://struts.apache.org/tags-html&quot; %&gt;

< html:textarea >

< html:textarea > -renders a text area input field.Multiple lines input field will return a value typed in the input text area box UI to server.ActionForm bean contains the corresponding property to hold the text value.It holds in the String format.

Example Code for < html:textarea >

1.Create an Jsp page and name it as textarea.jsp.It is the Welcome page for a user.

textarea.jsp

&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@taglib prefix=&quot;html&quot; uri=&quot;http://jakarta.apache.org/struts/tags-html&quot; %&gt;
&lt;%@taglib prefix=&quot;bean&quot; uri=&quot;http://jakarta.apache.org/struts/tags-bean&quot; %&gt;


&lt; html&gt;
    &lt; head&gt;

        &lt; title&gt; HTML Text Area example &lt;/title&gt;
    &lt; head&gt;
    &lt; body&gt;
        &lt; h1 &gt; Struts html:textarea example &lt;/h1&gt;

        &lt; html:form action=&quot;/textarea&quot;&gt;

           &lt; h3&gt; Enter Any Message:
		   &lt;/b&gt; &lt; html:textarea property=&quot;message&quot;/&gt; &lt; br&gt;
		   &lt; html:submit value=&quot;Submit&quot;/&gt;
            &lt; html:reset value=&quot;Reset&quot;/&gt;
        &lt;/html:form&gt;
    &lt;/body&gt;
&lt;/html&gt;

2.Create a Form bean.Form bean is used to hold the properties of the submitted form which is a sub class of ActionForm.Here we haveone property to hold in tha form bean “message” to hold the message typed in the input textarea field.
textareaform.java

package com.myapp.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class textareaform extends org.apache.struts.action.ActionForm {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public textareaform() {
        super();
      }
}





}

3.Simple Action class textareaaction.java which is a sub class of Action class used to process the user’s request.In this class we check for null values entered in the input teaxtarea field and set their respective error message.
textareaaction.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 textareaaction extends org.apache.struts.action.Action {


    private final static String SUCCESS = &quot;success&quot;;


    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        textareaform formBean=(textareaform)form;
        String message=formBean.getMessage();
        if(message.equals(&quot;&quot;))
            formBean.setMessage(&quot;&lt; span style='color:red'&gt; No Message has been typed&lt;/span&gt;&quot;);
        return mapping.findForward(SUCCESS);
    }
}

4.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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;

&lt;!DOCTYPE struts-config PUBLIC
          &quot;-//Apache Software Foundation//DTD Struts Configuration 1.2//EN&quot;
          &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd&quot;&gt;

&lt; struts-config&gt;
    &lt; form-beans&gt;
        &lt; form-bean name=&quot;textareaform&quot; type=&quot;com.myapp.struts.textareaform&quot;/&gt;
    &lt;/form-beans&gt;

&lt; action-mappings&gt;
        &lt; action name=&quot;textareaform&quot; path=&quot;/textarea&quot; scope=&quot;request&quot; type=&quot;com.myapp.struts.textareaaction&quot; validate=&quot;false&quot;&gt;
        &lt; forward name=&quot;success&quot; path=&quot;/textareaoutput.jsp&quot;/&gt;
		&lt;/action&gt;
&lt;/action-mappings&gt;

	&lt;/struts-config&gt;

5.Create another simple Jsp page textareaoutput.jspwhich is for displaying the output Message.If input text area field is empty error message is displayed.

&lt;%@page contentType=&quot;text/html&quot; pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;%@ taglib uri=&quot;http://jakarta.apache.org/struts/tags-bean&quot; prefix=&quot;bean&quot; %&gt;

&lt; html&gt;
    &lt; head&gt;
        &lt; meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
        &lt; title&gt; Text Area OutPut page&lt;/title&gt;
    &lt;/head&gt;
    &lt; body&gt;
        &lt; h3&gt; The Message is:	&lt;/h3&gt;
		&lt; h3&gt;
		&lt; bean:write name=&quot;textareaform&quot; property=&quot;message&quot; filter=&quot;false&quot;/&gt;
		&lt;/h3&gt;
    &lt;/body&gt;
&lt;/html&gt;

6.Building and running the application
Output

Access page:http://localhost:8080/textarea/

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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better