Struts HTML JavaScript Tag ( < html:javascript >)

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: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

&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 Javascript Tag &lt;/title&gt;
    &lt; head&gt;
    &lt; body bgcolor=&quot;#DDDDDD&quot;&gt;
        &lt; h1 &gt; Struts html:javascript tag &lt;/h1&gt;

        &lt; html:form action=&quot;/login&quot; onsubmit=&quot;return validateLoginForm(this);&quot; &gt;

         Enter name: &lt; html:text property=&quot;name&quot;/&gt;&lt; br&gt;
        Enter Password :&lt; html:password property=&quot;password&quot;/&gt;&lt; br&gt;
         &lt; html:javascript formName=&quot;LoginForm&quot; /&gt;
        &lt; html:submit/&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 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 = &quot;success&quot;;

    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.

&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;LoginForm&quot; type=&quot;com.myapp.struts.LoginForm&quot;/&gt;
    &lt;/form-beans&gt;

&lt; action-mappings&gt;
        &lt; action input=&quot;/loginpage.jsp&quot; name=&quot;LoginForm&quot; path=&quot;/login&quot; scope=&quot;request&quot; type=&quot;com.myapp.struts.loginaction&quot; validate=&quot;true&quot;&gt;
        &lt; forward name=&quot;success&quot; path=&quot;/loginsuccess.jsp&quot;/&gt;
		&lt;/action&gt;
&lt;/action-mappings&gt;

 &lt; plug-in className=&quot;org.apache.struts.validator.ValidatorPlugIn&quot;&gt;
        &lt; set-property
            property=&quot;pathnames&quot;
            value=&quot;/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml&quot;/&gt;
    &lt; /plug-in&gt;

	&lt;/struts-config&gt;

5.Update the validation.xml which contains the Validations required for a given FormName.

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

&lt; !DOCTYPE form-validation PUBLIC
          &quot;-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN&quot;
          &quot;http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd&quot;&gt;

&lt; form-validation&gt;
&lt; formset&gt;
 &lt; form name=&quot;LoginForm&quot;&gt;
            &lt; field property=&quot;name&quot; depends=&quot;required&quot;&gt;
                    &lt; arg key=&quot;LoginForm.name&quot;/&gt;
            &lt; /field&gt;
            &lt; field property=&quot;password&quot; depends=&quot;required&quot;&gt;
                    &lt; arg key=&quot;LoginForm.password&quot;/&gt;

            &lt; /field&gt;
        &lt; /form&gt;
&lt; /formset&gt;
&lt; /form-validation&gt;

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

&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;Login Successfull Page&lt; /title&gt;
    &lt; /head&gt;
    &lt; body bgcolor=&quot;DDDDDD&quot;&gt;
        &lt; h1&gt; Login Success&lt; /h1&gt;
        &lt; h3&gt;
            Welcome &lt; bean:write name=&quot;LoginForm&quot; property=&quot;name&quot;/&gt;
        &lt; /h3&gt;
    &lt; /body&gt;
&lt; /html&gt;

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:

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.

Comments

  1. I blog also and I am posting something related to this excellent blog post,
    “Struts HTML JavaScript Tag ( )”. Do you really care in the event that I personallywork
    with a number of your suggestions? Thanks for your time ,
    Vaughn

Speak Your Mind

*