ContactUs

Please send your Questions & Answers or Feedback to "mohan@javabook.org"

What is ActionMapping Class in Struts ?

ActionMappings is a collection of ActionMapping objects

The ActionMapping contains the knowledge of how a specific event maps to specific Actions.

The ActionServlet (Command) passes the ActionMapping to the Action class via the perform() method.

This allows Action to access the information to control flow.

The struts-config.xml determines what Action class the Controller calls.

The struts-config.xml configuration information is translated into a set of ActionMapping, which are put into container of ActionMappings.

Classes that end with s are containers.

What are the Validator Built-in Rules in Struts ?

required : field data provided

minlength : more than min length

maxlength : less than max length

range : in range of values

mask : correct format (takes regexp)

date : validates correct date format

email : validates correct E-Mail fromat

primitives: byte, integer, float, double, etc.

How to prevent multi-click using struts tokens ?

Mutli-click prevention using struts tokens- Prevent Duplicate Submission :

saveToken() : generate the token key and save to request/session attribute.

isTokenValid() : validate submitted token key against the 1 store in request/session.

resetToken() : reset the token key

 

Follow the steps to setup Mutli-click prevention using struts tokens.

 

Step 1: Action Class where saveToken() before JSP Page

 

First saveToken() then forward to your jsp.

Upon loading the form, invokes saveToken() on the action class to create and store the token key.

Struts will store the generated key in request/session.

public class LoadAction extends Action

{

public ActionForward execute(ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response)

{ ActionForward forward;

saveToken(request);

forward=mapping.findForward("empformpage");

// this is the jsp page where you want to struts tokens.

return forward;

}

}

Step 2:Store Token key as a hidden field ( empform.jsp) 

In the browser if you type : http://localhost:8080/testApp/loadActio.do

This will call execute() method of LoadAction. Then saveToken(request);//create and store the token key and mapping.findForward("empformpage"); forward to empform.jsp (below code is for empform.jsp)

<%@ page import="org.apache.struts.action.Action"%>

<%@ page import="org.apache.struts.taglib.html.Constants"%>

<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

 

 

 

Save

 

Step 3. Check Token is Valid ?

Once the form submitted, invokes isTokenValid() on the action class, it will validate the submitted token key(hidden field) with the token key stored previously on request/session. If match, it will return true.

public class SubmitAction extends Action

{

public ActionForward execute(ActionMapping mapping ,ActionForm form ,HttpServletRequest request,

HttpServletResponse response)

{

EmpForm frm=(EmpForm)form;

if(isTokenValid(request))

{ // This is Not Duplicate Submission of the form

// You can add your logic here

resetToken(request);

return mapping.findForward("sucess");

}

else

{ // This is Duplicate Submission of the form

// Return to the JSP to display the error message ( This is Duplicate Submission);

return mapping.findForward("duplicatesubmitpage");

}

}

}

 

Differences between web.xml and sturts-config.xml ?

web.xml :

It is used for the deployment descriptor for web applications

web.xml is used for making connection between web container & web application

It is read by container when we start the container.

sturts-config.xml :

It is used for deployment descripror for struts application.

It is used for making connection between view & controller

It is read by init() method of ActionServlet.

Explain life cycle of ActionForm ?

The lifecycle of ActionForm invoked by the RequestProcessor is as follows:

Retrieve or Create Form Bean associated with Action

"Store" FormBean in appropriate scope (request or session)

Reset the properties of the FormBean

Populate the properties of the FormBean

Validate the properties of the FormBean

Pass FormBean to Action

Related Posts Plugin for WordPress, Blogger...
Flag Counter