Friday, August 31, 2007

"code of complexity"

Time is a circle.Things that appear not working as of now may work tomorrow and vice versa.

These were the words from a guy ,I met, some time back.

According to him Lucky becomes Unlucky
Like becomes dislike
Black becomes White(No pun intended)

To Conclude this :-

"Stop living in prejudices,'Freeze the Moment'"

Thursday, August 30, 2007

Writing a Simple Struts App..

Step 1: - Copy the struts-blank.war , unzip it in the development directory.
After unzipping you will get following structure
Appname
+WEB-INF
+META-INF
+pages
index.jsp

Step 2: Now we have to define a Action class ,ActionForm class and few jsp pages for the display

ActionForm class:-
package com.StrutsDemo.Example;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class SubmitForm extends ActionForm {
/* Last Name */
private String lastName = "Hansen"; // default value
private String sex;
private String age;

public String getLastName() {
return (this.lastName);
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public void setSex(String sex){
this.sex=sex;
}

public void setAge(String age){
this.age=age;
}

public String getAge(){
return this.age;
}

public String getSex(){
return this.sex;
}

/* Address */

private String address = null;
public String getAddress() {
return (this.address);
}
public void setAddress(String address) {
this.address = address;
}


public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {

// Check for mandatory data
ActionErrors errors = new ActionErrors();
if (lastName == null || lastName.equals("")) {
errors.add("Last Name", new ActionMessage("error.lastName"));
}
if (address == null || address.equals("")) {
errors.add("Address", new ActionMessage("error.address"));
}
if (sex == null || sex.equals("")) {
errors.add("Sex", new ActionMessage("error.sex"));
}
if (age == null || age.equals("")) {
errors.add("Age", new ActionMessage("error.age"));
}
return errors;
}
}


Action Class:-

package com.StrutsDemo.Example;
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 final class SubmitAction extends Action {

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

SubmitForm f = (SubmitForm) form; // get the form bean
// and take the last name value
String lastName = f.getLastName();
String address = f.getAddress();
String sex = f.getSex();
String age = f.getAge();
// Translate the name to upper case
// and save it in the request object
request.setAttribute("lastName", lastName.toUpperCase());
request.setAttribute("address", address.toUpperCase());
request.setAttribute("age", age.toUpperCase());
request.setAttribute("sex", sex.toUpperCase());
// Forward control to the specified success target
return mapping.findForward("success");
}
}

Jsp page:-











configure struts-config.xml:-













configure web.xml:-













steps for validation:-

Move the application.properties from WEB-INF/src/java/resources/ to WEB-INF/classes/resources/

for checking the validation errors write the following entries in the application.properties

submit.lastName=Enter the Last Name
submit.address=Enter the address
submit.sex=Enter the sex

run the application :
http://localhost:8080/AppName/submit (in this example submit is the action name)

this example was tested with Tomcat 5.09 on Windows 2000