Wednesday, December 1, 2010

A basic struts form example.



A basic struts form example.
Developing a struts example involves the following steps.

1) A form bean that extends org.apache.struts.action.ActionForm.

In this application it is NewStrutsActionForm.java

2) A action class that extends org.apache.struts.action.Action

In this application it is NewStrutsAction.java

3) A form based on struts html tags.

In this application it is RegisterForm.jsp

4) mappings in the struts-config.xml file

In this application these are :-

Declaration of the form bean

<form-beans>

<form-bean name="NewStrutsActionForm" type="hypatia.NewStrutsActionForm"/>

</form-beans>

action mappings

<action-mappings>

<action name="NewStrutsActionForm" path="/actions/Register" scope="session" type="hypatia.NewStrutsAction" validate="false">

<forward name="MINOR" path="/minor.jsp"/>

<forward name="ADULT" path="/adult.jsp"/>

</action>

</action-mappings>

This application merely checks your age and sends you to an adult page, or a minor page accordingly as you are below 21 or over.

<span style="font-weight:bold;">

struts-config.xml</span>

<?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="NewStrutsActionForm" type="hypatia.NewStrutsActionForm"/>

</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>

<forward name="welcome" path="/Welcome.do"/>

<forward name="NewStrutsActionForm" path="/Register.do"/>

</global-forwards>

<action-mappings>

<action name="NewStrutsActionForm" path="/actions/Register" scope="session" type="hypatia.NewStrutsAction" validate="false">

<forward name="MINOR" path="/minor.jsp"/>

<forward name="ADULT" path="/adult.jsp"/>

</action>

<action path="/Welcome" forward="/welcomeStruts.jsp"/>

</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<plug-in className="org.apache.struts.tiles.TilesPlugin" >

<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />

<set-property property="moduleAware" value="true" />

</plug-in>

<!-- ========================= Validator plugin ================================= -->

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







NewStrutsAction.java
/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package hypatia;

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.ActionMapping;

import org.apache.struts.action.ActionForward;

/**

*

* @author Champak

*/

public class NewStrutsAction extends org.apache.struts.action.Action {

/* forward name="success" path="" */



/**

* This is the action called from the Struts framework.

* @param mapping The ActionMapping used to select this instance.

* @param form The optional ActionForm bean for this request.

* @param request The HTTP Request we are processing.

* @param response The HTTP Response we are processing.

* @throws java.lang.Exception

* @return

*/

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

NewStrutsActionForm bean=(NewStrutsActionForm)form;

if(bean.getAge()<21)

return mapping.findForward("MINOR");

else

return mapping.findForward("ADULT");

}

}







NewStrutsActionForm.java
/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package hypatia;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

/**

*

* @author Champak

*/

public class NewStrutsActionForm extends org.apache.struts.action.ActionForm {

private String name="Missing-Name";

private int age=18;

/**

* @return

*/

public String getName() {

return name;

}

/**

* @param string

*/

public void setName(String string) {

name = string;

}

/**

* @return

*/

public int getAge() {

return age;

}

/**

* @param i

*/

public void setAge(int age) {

this.age = age;

}

/**

*

*/

public NewStrutsActionForm() {

super();

// TODO Auto-generated constructor stub

}

/**

* This is the action called from the Struts framework.

* @param mapping The ActionMapping used to select this instance.

* @param request The HTTP Request we are processing.

* @return

*/

@Override

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if (getName() == null || getName().length() < 1) {

errors.add("name", new ActionMessage("error.name.required"));

// TODO: add 'error.name.required' key to your resources

}

return errors;

}

}





RegisterForm.jsp

<%--

Document : RegisterForm

Created on : Aug 27, 2010, 4:26:48 PM

Author : Champak

--%>

<%@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">

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body

<html:form action="/actions/Register">

Name <html:text property="name"/><br>

Age <html:text property="age"/><br>

<html:submit value="Login"/>

</html:form>

<h1>Hello World!</h1>

</body>

</html>





adult.jsp

Document : adult

Created on : Aug 27, 2010, 5:33:18 PM

Author : Champak

--%>

<%@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>JSP Page</title>

</head>

<body>

<h1>You're an adult</h1>

</body>

</html>








minor.jsp
Document : minor

Created on : Aug 27, 2010, 5:33:44 PM

Author : Champak

--%>

<%@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>JSP Page</title>

</head>

<body>

<h1>You're a minor</h1>

</body>

</html>

No comments:

Post a Comment