Struts FAQ
What is Action Class?
An Action class in the struts application extends Struts 'org.apache.struts.action.Action" Class.
Action class acts as wrapper around the business logic and provides an inteface to the application's Model layer.
An Action works as an adapter between the contents of an incoming HTTP request and the business logic that corresponds to it.
Then the struts controller (ActionServlet) slects an appropriate Action and Request Processor creates an instance if necessary,
and finally calls execute method of Action class.
To use the Action, we need to Subclass and overwrite the execute() method. and your bussiness login in execute() method.
The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the JSP as per the value of the returned ActionForward object.
ActionForward JSP from struts_config.xml file.
—————
Difference between MVC 1 and MVC 2?
MVC1 was a first generation approach that used JSP pages and the JavaBeans component
—————
Struts Follows Which version of MVC?
MVC 2
—————
How does client side validation using validator framework work in struts ?
There are two configuration files used.
One if called validator-rules.xml and the other is validation.xml.
This is example of client side validation :Java Script message.
Step 1.
In the JSP page : empform.jsp
<html:form action="/EmpSaveAaction" method="post" onsubmit="return validateEmpForm(this);">
<html:text property="firstName" size="30" maxlength="30"/>
<html:text property="lastName" size="30" maxlength="30"/>
<html:submit>Save</html:submit>
<!-- Begin Validator Javascript Function-->
<html:javascript formName="empForm"/>
<!-- End of Validator Javascript Function-->
</html:form>
You have to add
<html:javascript formName="empForm"/> in JSP for activate client side validation .
Step 2.
Add action mapping in struts-config.xml
<action
path="/EmpSaveAaction"
type="empForm"
name="AddressForm"
scope="request"
validate="true"
input="/empform.jsp">
<forward name="success" path="/success.jsp"/>
</action>
and add the form bean inside <form-beans> </form-beans> tag
<form-beans>
<form-bean name="empForm" type="com.techfaq.form.EmpForm" />
</form-beans>
Step 3.
validator-rules.xml :
The validator-rules.xml file defines the Validator definitions available for a given application.
The validator-rules.xml file acts as a template, defining all of the possible Validators that are available to an application.
Example validator-rules.xml File :
<form-validation>
<global>
<validator
name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
</global>
</form-validation>
Step 4.
validation.xml File :
The validation.xml file is where you couple the individual Validators defined in the validator-rules.xml to components within your application.
validation.xml File
<form-validation>
<formset>
<form name="empForm">
<field
property="firstName"
depends="required">
<arg key="label.firstName"/>
</field>
<field
property="lastName"
depends="required">
<arg key="label.lastName"/>
</field>
</form>
</formset>
</form-validation>
In the empForm firstName and lastName are the required filed.
So in the above configuration you can see we add for both firstName and lastName.
You can see depends="required" - "required" property is defind in validator-rules.xml.
In the resource bundle : application_resource.propertis file
label.firstName=First Name
label.lastName=Last Name
#Error messages used by the Validator
errors.required={0} is required.
Based on the validation.xml File configuration .
Error in jsp will be : Java Script message.
First Name is required.
Last Name is required.
{0} will be filled by (First Name or Last Name) because validation.xml above configuration you have defind <arg key="label.firstName"/>.
—————
What are the important sections in Struts Configuration File ? struts-config.xml?
The five important sections are:
1. Form bean definition section
2. Global forward definition section
3. Action mapping definition section
4. Controller configuration section
5. Application Resources definition section
<struts-config>
<form-beans>
<form-bean name="CustomerForm" type="example.CustomerForm"/>
<form-bean name="LogonForm" type="example.LogonForm"/>
</form-beans>
<global-forwards>
<forward name="logon" path="/logon.jsp"/>
<forward name="logoff" path="/logoff.do"/>
</global-forwards>
<action-mappings>
<action path="/submitDetailForm"
type="example.CustomerAction"
name="CustomerForm"
scope="request"
validate="true"
input="/CustomerDetailForm.jsp">
<forward name="success" path="/ThankYou.jsp" redirect=?true? />
<forward name="failure" path="/Failure.jsp" />
</action>
</action-mappings>
<controller processorClass="org.apache.struts.action.RequestProcessor" />
<message-resources parameter="example.resource.ApplicationResources"/>
—————
What is ActionServlet?
The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.
Struts Flow start with ActionServlet then call to process() method of RequestProcessor.
Step 1. Load ActionServlet using load-on-startup and do the following tasks.
Any struts web application contain the ActionServlet configuration in web.xml file.
On load-on-startup the servlet container Instantiate the ActionServlet .
First Task by ActionServlet : The ActionServlet takes the Struts Config file name as an init-param.
At startup, in the init() method, the ActionServlet reads the Struts Config file and load into memory.
Second Task by ActionServlet : If the user types https://localhost:8080/app/submitForm.do in the browser URL bar, the URL will be intercepted and processed by the ActionServlet since the URL has a pattern *.do, with a suffix of "do". Because servlet-mapping is
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Third Task by ActionServlet : Then ActionServlet delegates the request handling to another class called RequestProcessor by invoking its process() method.
Step 2. ActionServlet calls process() method of RequestProcessor
—————
Can I have an Action without a form?
Yes. If your Action does not need any data and it does
not need to make any data available to the view or
controller component that it forwards to, it doesn't need
a form. A good example of an Action with no ActionForm is
the LogoffAction in the struts example application:
<action path="/logoff"
type="org.apache.struts.webapp.example.LogoffAction">
<forward name="success" path="/index.jsp"/>
</action>
This action needs no data other than the user's session, which
it can get from the Request, and it doesn't need to prepare any
view elements for display, so it does not need a form.
However, you cannot use the <html:form> tag without
an ActionForm. Even if you want to use the <html:form>
tag with a simple Action that does not require input,
the tag will expect you to use some type of ActionForm,
even if it is an empty subclass without any properties.
—————
What is Struts Validator Framework?
Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class.
Client side validation : validation-rule.xml (java script code should be in this file and in JSP file
<html:javascript formName="CreateForm"/>)
Server side Validation :
JSP page - should cointain
< html:errors/ >
Aactionform.validate() method
or
The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.
How you will display validation fail errors on jsp page? -
The following tag displays all the errors: < html:errors/ >
—————
What is the ActionForm and what are important methods in ActionForm?
ActionForm is javabean which represents the form inputs containing the request parameters from the View referencing the Action bean.
The important methods of ActionForm are : validate() & reset().
validate() : Used to validate properties after they have been populated; Called before FormBean is handed to Action. Returns a collection of ActionError as ActionErrors. Following is the method signature for the validate() method.
ActionErrors validate(ActionMapping mapping,HttpServletRequest request)
reset(): reset() method is called by Struts Framework with each request that uses the defined ActionForm. The purpose of this method is to reset all of the ActionForm's data members prior to the new request values being set.
—————
Can we have more than one struts-config.xml file for a single Struts application?
Yes, we can have more than one struts-config.xml for a single Struts application. They can be configured as follows:
<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-config1.xml, /WEB-INF/struts-config2.xml, /WEB-INF/struts-config3
</param-value>
</init-param>
<servlet>
—————
Topic: Struts FAQ
—————
—————
—————
—————
—————
—————
—————
—————
—————
—————