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:javascript >
< html:javascript > -render JavaScript validation based on the validation rules loaded by the ValidatorPlugIn.Based on the formName the set of Validation Rules are generated which should match the formName given in the xml file(validation.xml).The dynamicJavascript and staticJavascript attributes default to true, but if dynamicJavascript is set to true and staticJavascript is set to false then only the dynamic JavaScript will be rendered.
Example Code for < html:javascript>
1.Create an Jsp page and name it as loginpage.jsp.It is the Welcome page for a user. In this example the login form is created and simple validation is performed.This page contains two fields to enter by user username and password.
loginpage.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 Javascript Tag </title> < head> < body bgcolor="#DDDDDD"> < h1 > Struts html:javascript tag </h1> < html:form action="/login" onsubmit="return validateLoginForm(this);" > Enter name: < html:text property="name"/>< br> Enter Password :< html:password property="password"/>< br> < html:javascript formName="LoginForm" /> < html:submit/> < /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 have two properties to hold in tha form bean “name” and “password” .If client validation is successfull then these values get stored in the form.
LoginFormForm.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 LoginForm extends org.apache.struts.action.ActionForm { private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public LoginForm() { super(); } }
3.Simple Action class loginaction.java which is a sub class of Action class which is used to redirect the page upon successfull validation .
loginaction.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 loginaction 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 { return mapping.findForward(SUCCESS); } } public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { 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.Additionally a validator Plug-in is added to enable the client side validation.Plug-in contains the path for both validation.xml and validation-rules.xml.
<?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="LoginForm" type="com.myapp.struts.LoginForm"/> </form-beans> < action-mappings> < action input="/loginpage.jsp" name="LoginForm" path="/login" scope="request" type="com.myapp.struts.loginaction" validate="true"> < forward name="success" path="/loginsuccess.jsp"/> </action> </action-mappings> < plug-in className="org.apache.struts.validator.ValidatorPlugIn"> < set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> < /plug-in> </struts-config>
5.Update the validation.xml which contains the Validations required for a given FormName.
< ?xml version="1.0" encoding="UTF-8" ?> < !DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd"> < form-validation> < formset> < form name="LoginForm"> < field property="name" depends="required"> < arg key="LoginForm.name"/> < /field> < field property="password" depends="required"> < arg key="LoginForm.password"/> < /field> < /form> < /formset> < /form-validation>
6.Add following lines in the Application.Resources file which contains the key values.
LoginForm.name=Enter User Name LoginForm.password=Enter Password
7.Create another simple Jsp page loginsuccess.jsp which the output page for login success if validation is successfull the controller redirects to this page.This page displays the welcome message along with the user name
< html> < head> < meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> < title>Login Successfull Page< /title> < /head> < body bgcolor="DDDDDD"> < h1> Login Success< /h1> < h3> Welcome < bean:write name="LoginForm" property="name"/> < /h3> < /body> < /html>
8.Building and running the application
Output
Access page:http://localhost:8084/javascript/
If not validated the javascript alert box with error message is displayed.
If succeccfully validated the login success message along with user name is displayed.
also read: