< html:javascript /> html tag

< html:javascript /> html tag

Previous Home Next

 

The <html:javascript/> Tag is used to insert javaScript validation method based on the the ValidatorPlugIn.The set of validation rules that should be generated is based on the formName attribute passed in,which should match the name attribute of the form element in the xml file.

 
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 giver. If dynamicJavascript is set to false and staticJavascript is set to true then only the static JavaScript will be giver. The <html:javascript/> has no no body.

This supports following attributes:-

  • cdata :- If  "true" and XHTML attribute of the <html:html/> has been enabled then the generated javascript will be placed within a CDATA element to prevent XML parsing. The default is "true" but this optional. 
  • dynamicJavascript :-This is indicates if dynamic should be enabled. Default value to true.
  • formName :- This represent the identifier that is used to retrieve the appropriate validator rules. If "dynamicJavascript" is set to true and formName is missing then it is not recognized by the ValidatorPlugIn and thrown a JspException.
  • htmlComment :- if true ,then defined enclose the javascript with HTML comments.Default value is true.
  • method :- This is used to allow a user of the tag to override the default method name.if it is not indicated,The default method name is 'validate' .
  • page :-This attribute represent the current page number of multipart form.This attributes is valid .
  • scriptLanguage The <script> element will not contain a language attribute when this is set to false. The default is true but this property is ignored in XHTML mode. 
  • src :-The src attribute's value when defining the html script element.
  • staticJavascript :- Indicates whether or not to render the static JavaScript. The Default value is true.
  • bundle :- The servlet context attributes key is used for the MessageResources instance.If it not specified then defaults to the application resources configured for our action servlet. 


Directory Structure of JavascriptTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Struts html:javascript tag example</title>
</head>
<body>
<h3>Struts html:javascript tag example</h3>
<html:form action="/javascriptsAction" onsubmit="return validateJavascriptsForm(this);">
<html:javascript formName="javascriptsForm" />
<bean:message key="label.name"/>:
<html:text property="name" name="javascriptsForm"/><br/>
<bean:message key="label.password"/>:
<html:password property="password" name="javascriptsForm"/><br/>
<br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name> 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
    <form-bean name="javascriptsForm" type="org.r4r.struts.JavascriptForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/javascripts" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
		<action path="/javascriptsAction" type="org.r4r.struts.JavascriptAction" 
		name="javascriptsForm" input="/index.jsp" validate="true" scope="request">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
  <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>

JavascriptForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class JavascriptForm extends 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;
	}
}

JavascriptAction.java

package org.r4r.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class JavascriptAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) throws Exception{
		@SuppressWarnings("unused")
		JavascriptForm javascriptsForm=(JavascriptForm)form;
		return mapping.findForward("success");
	}

}

ApplicationResources.properties

#label message
label.name = User Name 
label.password = Password
label.submit = Submit
label.reset = Reset
errors.name= Name 
errors.password= Password Length
errors.minlength=Password Length Greater than Six
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>Struts html:javascript tag example</title>
</head>
<body>
<h3>Struts html:javascript tag example</h3>
<h4>Welcome , <bean:write name="javascriptsForm" property="name" /></h4>
</body>
</html>

Output




Previous Home Next