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: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
<%@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 Text Area example </title> < head> < body> < h1 > Struts html:textarea example </h1> < html:form action="/textarea"> < h3> Enter Any Message: </b> < html:textarea property="message"/> < br> < html:submit value="Submit"/> < html:reset value="Reset"/> </html:form> </body> </html>
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 = "success"; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { textareaform formBean=(textareaform)form; String message=formBean.getMessage(); if(message.equals("")) formBean.setMessage("< span style='color:red'> No Message has been typed</span>"); 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.
<?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> < form-beans> < form-bean name="textareaform" type="com.myapp.struts.textareaform"/> </form-beans> < action-mappings> < action name="textareaform" path="/textarea" scope="request" type="com.myapp.struts.textareaaction" validate="false"> < forward name="success" path="/textareaoutput.jsp"/> </action> </action-mappings> </struts-config>
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.
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> < html> < head> < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> < title> Text Area OutPut page</title> </head> < body> < h3> The Message is: </h3> < h3> < bean:write name="textareaform" property="message" filter="false"/> </h3> </body> </html>
6.Building and running the application
Output
Access page:http://localhost:8080/textarea/
also read: