Action Methods into struts

An Action is class that mediator between an incoming request and the corresponding business logic.The controller will create an instance for each request (if necessary), and call the execute method. 
org.apache.struts.action.Action class have many method :-
Exception  if the application business logic throws an exception.
public 
ActionForward   execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) 
public ActionForward execute(ActionMapping mapping,ActionForm form,  HttpServletRequest request, HttpServletResponse response)      throws Exception
  
2. addErrors() :-
-protected void addErrors(HttpServletRequest request,  ActionMessages errors)
3. addMessages() :-
-protected void addMessages(HttpServletRequest request,
                           ActionMessages messages)
saveToken and istokenValid is the helper methods which enable you to add advanced functionality toyour struts application. saveToken and istokenValid both is to make sure that a form has not submitted twice.
In struts applicaion,transaction tokens to ensure that a form is not submitted twice.A transaction is unique string that is generated.The token is submitted with the form tag.
saveToken :
isTokenvalid
why we are using this concept :-We send the request to the server for first time it is going to accept it and if we are sending multiple requests as like that then from second request onwards it stops executing the code.So only first request only it works.
In bank applications if we send multiple times then improper action occurs and we may get unrelated problems.
Action isCancelled into struts
Struts provides a special input field called html:cancel.You can use this input field to cancel a form submission.when you use the isCancelled method in the action handler to see whether the action was cancelled.
Syntax of isCancelled method :-             protected  boolean isCancelled(HttpServletRequest request)
This method return the boolean value. if the current form's cancel button was pressed then return true otherwise  return false.
Create the input form :-login.jsp


<html:form  action="Loginaction" >
<html:text property="User name"/>
<html:password property="password"/>
<html:submit/>
<html:cancel/>
</html:form>

Create Actionhandler class :-
 


package r4r.co.in;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse respose)throws Exception {
return add(mapping,form,request,respose); }
public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse respose)throws Exception{ 
if(isCancelled(request)) {
System.out.println("This submission has been cancelled ::"); 
  return (mapping.findForward("home");     }
else if(isTokenValid(request)) {
LoginForm form1=(LoginForm)form;
return mapping.findForward("success"); }
else {
System.out.println("User can't submit the same form ");
return mapping.findForward("resubmit");
}
}

Action getLocale and setLocale into struts
Struts 1.3 supports the functionality of retrieving and changing the current locale settings.You can dynamically set the Locale for the user's session.GetLocale() and SetLocale() method are the helper of Action class.
 
GetLocale():- 
                  protected  Locale getLocale(HttpServletRequest request)  SetLocale() :-
                      protected  void setLocale(HttpServletRequest request, Locale locale)          
Example :-
 
 
Action saveMessages and getResources into struts
 saveMessages
Working with ActionMessages and ActionErrors is nearly identical but only difference is that the <html:errors> tag forced you to place HTML in the message resource file.This is not good. 
In <html:messages> tag, We can sets up a loop where you may easily stick in the HTML you want.
This forces <html:messages> to get the messages from Globals.MESSAGE_KEY in request scope. (if messages="true" is set), if any messages are required.


<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:messages id="msg" message="true">
<bean:write name="msg"/>
<br></html:messages>  

 
saveMessages :- 
Syntex of this method :-              protected  void saveMessages(HttpServletRequest request, ActionMessages messages) 
              protected  void saveMessages(HttpSession session, ActionMessages messages) 
        
getResources :-
Syntex of this method :-
                   protected  MessageResources getResources(HttpServletRequest request) :-
                  protected  MessageResources getResources(HttpServletRequest request, String key):- 
This method will return the specified message resources for the current locale.
This method
will return the default message resources for the current locale.
This method is used to get number of messages resources for that locale.
  This method save the specified messages keys into the appropriate request or session attribute for use by the <html:messages> tag.
and getResources method helper methods of Action into struts framework.ActionMessages to display dynamic i18n-enabled messages.
ATM machine,The First thing it often asks you is what language you speak,and press appropriate button.You could do the same thing for your site.
This is used to sets the locale to the new specified locale.Globals.LOCALE_KEY stores the  user's currently selected Locale into their HttpSession scope.
This methods returns the current selected locale on the client machine.
:- This method return true or false value.This method return 'true' if the token stored in the session with that submitted from the form  both are equals otherwise return false. 
-This method generates a unique token and saves it token in the session under the key org.apache.struts.action.TOKEN. 
This method is used to adds the specified messages keys into the appropriate request  attribute.
This method is used to adds the specified errors keys into the appropriate request attribute


1.execute() :-If The response has been completed then forward to which control should be transferred.This method throws an
Exception  if the application business logic throws an exception.
public 
ActionForward   execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) 
public ActionForward execute(ActionMapping mapping,ActionForm form,  HttpServletRequest request, HttpServletResponse response)      throws Exception
  
2. addErrors() :-
-protected void addErrors(HttpServletRequest request,  ActionMessages errors)
3. addMessages() :-
-protected void addMessages(HttpServletRequest request,
                           ActionMessages messages)
Action saveToken and isTokenValid into struts
saveToken and istokenValid is the helper methods which enable you to add advanced functionality toyour struts application. saveToken and istokenValid both is to make sure that a form has not submitted twice.
In struts applicaion,transaction tokens to ensure that a form is not submitted twice.A transaction is unique string that is generated.The token is submitted with the form tag.
saveToken :
isTokenvalid
why we are using this concept :-We send the request to the server for first time it is going to accept it and if we are sending multiple requests as like that then from second request onwards it stops executing the code.So only first request only it works.
In bank applications if we send multiple times then improper action occurs and we may get unrelated problems.
Action isCancelled into struts
Struts provides a special input field called html:cancel.You can use this input field to cancel a form submission.when you use the isCancelled method in the action handler to see whether the action was cancelled.
Syntax of isCancelled method :-             protected  boolean isCancelled(HttpServletRequest request)
This method return the boolean value. if the current form's cancel button was pressed then return true otherwise  return false.
Create the input form :-login.jsp


<html:form  action="Loginaction" >
<html:text property="User name"/>
<html:password property="password"/>
<html:submit/>
<html:cancel/>
</html:form>

Create Actionhandler class :-
 


package r4r.co.in;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse respose)throws Exception {
return add(mapping,form,request,respose); }
public ActionForward add(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse respose)throws Exception{ 
if(isCancelled(request)) {
System.out.println("This submission has been cancelled ::"); 
  return (mapping.findForward("home");     }
else if(isTokenValid(request)) {
LoginForm form1=(LoginForm)form;
return mapping.findForward("success"); }
else {
System.out.println("User can't submit the same form ");
return mapping.findForward("resubmit");
}
}

Action getLocale and setLocale into struts
Struts 1.3 supports the functionality of retrieving and changing the current locale settings.You can dynamically set the Locale for the user's session.GetLocale() and SetLocale() method are the helper of Action class.
 
GetLocale():- 
                  protected  Locale getLocale(HttpServletRequest request)  SetLocale() :-
                      protected  void setLocale(HttpServletRequest request, Locale locale)          
Example :-
 
 
Action saveMessages and getResources into struts
 saveMessages
Working with ActionMessages and ActionErrors is nearly identical but only difference is that the <html:errors> tag forced you to place HTML in the message resource file.This is not good. 
In <html:messages> tag, We can sets up a loop where you may easily stick in the HTML you want.
This forces <html:messages> to get the messages from Globals.MESSAGE_KEY in request scope. (if messages="true" is set), if any messages are required.


<%@taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<html:messages id="msg" message="true">
<bean:write name="msg"/>
<br></html:messages>  

 
saveMessages :- 
Syntex of this method :-              protected  void saveMessages(HttpServletRequest request, ActionMessages messages) 
              protected  void saveMessages(HttpSession session, ActionMessages messages) 
        
getResources :-
Syntex of this method :-
                   protected  MessageResources getResources(HttpServletRequest request) :-
                  protected  MessageResources getResources(HttpServletRequest request, String key):- 
 
This method will return the specified message resources for the current locale.
This method
will return the default message resources for the current locale.
This method is used to get number of messages resources for that locale.
  This method save the specified messages keys into the appropriate request or session attribute for use by the <html:messages> tag.
and getResources method helper methods of Action into struts framework.ActionMessages to display dynamic i18n-enabled messages.
ATM machine,The First thing it often asks you is what language you speak,and press appropriate button.You could do the same thing for your site.
This is used to sets the locale to the new specified locale.Globals.LOCALE_KEY stores the  user's currently selected Locale into their HttpSession scope.
This methods returns the current selected locale on the client machine.
:- This method return true or false value.This method return 'true' if the token stored in the session with that submitted from the form  both are equals otherwise return false. 
-This method generates a unique token and saves it token in the session under the key org.apache.struts.action.TOKEN. 
This method is used to adds the specified messages keys into the appropriate request  attribute.
This method is used to adds the specified errors keys into the appropriate request attribute
Action saveToken and isTokenValid into struts

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More