when the ActionForm class will be instantiate in struts

Hi all, I am new to this group. I've one doubt in struts.If any one know please help me. when the ActionForm class will be instantiate in struts ?ThanksSrilue
[215 byte] By [srilu.elchuria] at [2007-11-26 13:14:14]
# 1

hi friend,

this is harsha

as far as i know when we press the submit button on the form all it will take care of Action Servlet.

The ActionServlet looks up the logical resource name in the struts-config.xml file, where that name is mapped to a corresponding Action class that you provide. The Action class execute() method accesses the desired business logic, and then returns an ActionForward instance back to the ActionServlet to tell it what to do next

harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Basically on demand. When you ask for a specific action form it creates it at that timeHow long it stays in memory depends on your struts-config. Your action mapping determines whether you keep the form in session or not.
evnafetsa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Thanks for reply.means ActionForm will be instantiated when ever it called.please explain this again.ThanksSrilue
srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

when ever it goes to struts-config.xml file

there it will check in action-mappings

in action-mappings u keep the action tag

in this action tag we have an attribute 'name' is there through this name attribute it will map to the form-bean tag from that it will be instantiated and what ever the values u have given in the form it will be setted in this ActionForm class .

from this we can get the values in execute() method .

this is what happens

like ActionForm we can have DynaActionForm also

harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

But In my application , I set default values to my empname in reset method of ActionForm. At the first time just I click onto the emp.jsp page the value I have set in the reset method diplayed in the empName textbox.

Here I am not submitting any form.

without submitting any form , ActionForm returns empName's value.

Means ActionForm instatiate and returns the value.

Plese clarify this.

Thanks

Srilue

srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Hi,

Let me give a brief overview of wat exactly happens in terms control.

Whenever a u request a response from a view the transfer goes to the ActionServlet it instantiates RequestProcessor Object which checks the ActionMappings inside structs-config.xml and depending upon the mapping the control transfers to ActionForm.reset() method by calling RequestProcessor.processPopulate() method.The reset() method clears the individual attributes in the ActionForm class to ensure that the ActionForm class is properly initialized before it is populated with the user's form data. The validate() method is overridden by the developer. This method will contain all of the validation logic used in validating the data entered by the end user by which action errors are beibg created and then the control transfers to Action.execute().

Hope this might give u a better picture of what really happens

RahulSharnaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
hi no at first when load ur emp.jsp page it wont take the default values that u specified in reset method bcoz at first ActionForm class cannot be instantiated .i tried the same thing but i didnt get the answer like u get.try once again.ok
harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8
Thank u Rahul.After startung my application , just i had clicked on jsp file then automatically it displays the value which I had previously set in reset method in the text field. Here I am not submitting any form.Thanks Srilue
srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9
Hi Harsha,But I got the value into the text field which I had set in the reset ()method. ThanksSrilue
srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

Hi srilu,

i did one example but i didnt get the default values at first when we load the opening jsp page.

or

do one thing

place system.out.println() in ActionForm class

and in reset()

so that u can know which one it instantiates first.

check it once by using system.out.println() in reset method and then check the output in the server

so that i think its very clear to u.

try it once

harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11
Hi Harsha, I have already done this.I wrote System.out.println() in reset() method of ActionForm class. When I click on jsp page first time , reset() method has been called and the statement printed , in the System.out.println() ;Thanks Srilue
srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12
HI srilu,I think it wont come like that , i also tried the same type of program,but, i didnt get like that.okwill u plz send me the code for emp.jsp and the actionform class i will tryok
harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13
Hi Srilue,did u get that one or otherwise plz send me the code as i asked earlieri will try what ever i tried already when we upload the jsp page we didnt the default values
harshaa at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

[nobr]Hi harsha,

here is my emp.jspcode

<%@ page language="java"%>

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

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

<html>

<head>

<title>JSP for empform form</title>

</head>

<body>

<html:form action="/emp">

edept : <html:text property="edept"/><html:errors property="edept"/><br/>

eid : <html:text property="eid"/><html:errors property="eid"/><br/>

ename : <html:text property="ename"/><html:errors property="ename"/><br/>

<html:submit/><html:cancel/>

</html:form>

</body>

</html>

in struts-config.xml code for this empbean is

<form-beans >

<form-bean name="empform" type="Emp.EmpForm" />

</form-beans>

<action-mappings >

validate="false" />

<action

attribute="empform"

input="emp.jsp"

name="empform"

path="/emp"

scope="request"

type="Emp.EmpAction"

validate="false" />

</action-mappings>

and code for EmpForm.java is

package Emp;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

public class EmpForm extends ActionForm {

private Integer edept;

private String ename;

private Integer eid;

public void reset(ActionMapping mapping, HttpServletRequest request) {

System.out.println("in emp reset");

}

public Integer getEdept() {

return edept;

}

public void setEdept(Integer edept) {

this.edept = edept;

}

public String getEname() {

return ename;

}

public void setEname(String ename) {

this.ename = ename;

}

public Integer getEid() {

return eid;

}

public void setEid(Integer eid) {

this.eid = eid;

}

}

and the code for EmpAction.java is

package Emp;

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 EmpAction extends Action {

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) {

EmpForm empform = (EmpForm) form;

throw new UnsupportedOperationException(

"Generated method 'execute(...)' not implemented.");

}

}

this is my total application.

Here after starting the tomcat server when we click onto the emp.jsp,

automatically the statement will be displayed , i had written in the System.out.println() in reset() method.

Thanks

Srilue[/nobr]

srilu.elchuria at 2007-7-7 17:33:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

Hi Srilue,

U r absolutely correct and sorry abt the explanation i have given so far

when i executed ur program reset method is called first.

After i asked my friends abt this problem they also first confused after that we realized that

At first constructor of ActionForm and reset method will be called as it set default values for the fields it will be called first .

sorry and thankyou for posting this type of question .

i learned some thing from ur question.

thank you.

harshaa at 2007-7-21 15:47:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 16
Thank u harsha.I got the answer from one of my friends.ThanksSrilue
srilu.elchuria at 2007-7-21 15:47:18 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...