Ads 468x60px

Pages

Friday, 13 July 2012

Struts CheckBox Example

Common.properties (copy it into webapps\app1\WEB-INF\classes\com\mkyong\common\properties folder)

#error message
error.common.html.checkbox.required = Please tick the checkbox.
#label message
label.common.html.checkbox.name = CheckBox
label.common.html.checkbox.button.submit = Submit
label.common.html.checkbox.button.reset = Reset

web.xml (copy it into webapps\app1\WEB-INF folder)

<!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>Struts-CheckBox-Example</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 (copy it into webapps\app1\WEB-INF folder)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
 <form-beans>
  <form-bean
   name="htmlCheckBoxForm"
   type="com.mkyong.common.form.HtmlCheckBoxForm"/>

 </form-beans>
 <action-mappings>

     <action
   path="/CheckBoxPage"
   type="org.apache.struts.actions.ForwardAction"
   parameter="/pages/checkbox.jsp"/>

  <action
   path="/CheckBox"
   type="com.mkyong.common.action.HtmlCheckBoxAction"
   name="htmlCheckBoxForm"
   validate="true"
   input="/pages/checkbox.jsp"
   >

   <forward name="success" path="/pages/display.jsp"/>
  </action>
 </action-mappings>
 <message-resources
  parameter="com.mkyong.common.properties.Common" />
</struts-config>

HtmlCheckBoxAction.java (copy it into webapps\app1\WEB-INF\classes folder and compile it)

package com.mkyong.common.action;
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 HtmlCheckBoxAction extends Action{

 public ActionForward execute(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,HttpServletResponse response) throws Exception {

  return mapping.findForward("success");
 }

}

HtmlCheckBoxForm.java (copy it into webapps\app1\WEB-INF\classes folder and compile it)

package com.mkyong.common.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
public class HtmlCheckBoxForm extends ActionForm{

 String checkboxValue;
 public String getCheckboxValue() {
  return checkboxValue;
 }
 public void setCheckboxValue(String checkboxValue) {
  this.checkboxValue = checkboxValue;
 }
 @Override
 public ActionErrors validate(ActionMapping mapping,
   HttpServletRequest request) {

  ActionErrors errors = new ActionErrors();
      
     if( getCheckboxValue() == null || ("".equals(getCheckboxValue()))) {
        errors.add("common.checkbox.err",
          new ActionMessage("error.common.html.checkbox.required"));
     }
      
     return errors;
 }

 @Override
 public void reset(ActionMapping mapping, HttpServletRequest request) {
  // reset properties
  checkboxValue = "";
 }

}

checkbox.jsp (copy it into webapps\app1 folder)

<html>
<head>
</head>
<body>
<h1>Struts html:checkbox example</h1>
<html:form action="/CheckBox">

<html:messages id="err_name" property="common.checkbox.err">
<div style="color:red">
 <bean:write name="err_name" />
</div>
</html:messages>
<div style="padding:16px">
<bean:message key="label.common.html.checkbox.name" /> :
 <html:checkbox property="checkboxValue" />
</div>
<div style="padding:16px">
 <div style="float:left;padding-right:8px;">
  <html:submit><bean:message key="label.common.html.checkbox.button.submit" /></html:submit>
 </div>
 <html:reset><bean:message key="label.common.html.checkbox.button.reset" /></html:reset>
</div>
</html:form>

</body>
</html>



 display.jsp (copy it into webapps\app1 folder)

<html>
<head>
</head>
<body>
<h1>
 CheckBox value : <bean:write name="htmlCheckBoxForm" property="checkboxValue" />
</h1>
</body>
</html>

super keyword in java

the super keyword indicates the following :

1. The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
2. The super keyword as a standalone statement is used to call the constructor of the superclass in the base class.


Example to use the super keyword to call the constructor of the superclass in the base class:
public class class1
{
public class1(String arg)
{
super(arg);
}
..................
}

-->  The syntax super.<method_Name>() is used to give a call to a method of the superclass in the base class.
--> This kind of use of the super keyword is only necessary when we need to call a method that is overridden in this base class in order to specify that the method should be called on the superclass.


Example to use the super keyword with a method:

public class class1
{
.............................
public String fun()
{
return super.fun();
}
.............................
}


class A
{
int k = 10;
}
class Test extends A
{
public void m()
{
System.out.println(super.k);
}
}

keywords in java

Click Here to download doc

this keyword in java with example

The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts. As we can see in the program that we have declare the name of instance variable and local variables same. Now to avoid the confliction between them we use this keyword. Here, this section provides you an example with the complete code of the program for the illustration of how to what is this keyword and how to use it.
In the example, this.length and this.breadth refers to the instance variable length and breadth while length and breadth refers to the arguments passed in the method. We have made a program over this. After going through it you can better understand.

Here is the code of the program:

class Rectangle{
  int length,breadth;
  void show(int length,int breadth){
  this.length=length;
  this.breadth=breadth;
  }
  int calculate(){
  return(length*breadth);
  }
}
public class UseOfThisOperator{
  public static void main(String[] args){
  Rectangle rectangle=new Rectangle();
  rectangle.show(5,6);
  int area = rectangle.calculate();
  System.out.println("The area of a Rectangle is  :  " + area);
  }
}


Output of the program is given below:
The area of a Rectangle is : 30

Java Classpath