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
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
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
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
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
[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]
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.