Actions

Actions are the basic components Struts uses to support Controller function.  They closely resemble Servlets, but are invoked through an execute method that individual actions must override to provide function.  They return an ActionForward object that identifies the name of the next object to which control is passed.  These names are general and are bound to actual objects through the struts-config.xml file.

Details can be found in the Struts API.


Architecture
 

 


Example


public class ActionUser extends Action {

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
		
		// return value
		ActionForward forward = new ActionForward(); 

		//  get session state
		HttpSession session = request.getSession();	
		
		// FormBean version of input and return values		
		FormBeanUser formBean = (FormBeanUser) form;  
		FormBeanUser formBeanNew;

		//  DataBean version of input and return values
		BeanUser dataBean = mapFormBeanToDataBean( formBean ); 
		BeanUser dataBeanNew;

		// instantiate model
		ModelUser model = new ModelUser(); 

		// get userAction requested

		String userAction = ( (String)(request.getParameter("userAction")) ).trim(); 
				
//		handle userActions
		
		//  cruds actions
		
		if ((userAction.equalsIgnoreCase("update")) )  {
			
			//  update userId and role
			dataBean.setUserId( (String) session.getAttribute("userId") );
			dataBean.setRole( (String) session.getAttribute("role") );

			//  call model
			dataBeanNew = model.update( dataBean );
	
			if ( !(dataBeanNew.isOk()) )  {
					
				formBean.setMessage( "Error in update." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("failureUpdate");
					
			}  else  {
				
				formBeanNew = mapDataBeanToFormBean( dataBeanNew );
				
				formBean = formBeanNew;
				formBean.setMessage( "Update OK." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("successUpdate");
			
			}			


		//  non-cruds actions
					

		}  else if ( (userAction.equalsIgnoreCase("login")) )  {
				
			// call model			
			dataBeanNew = model.login( dataBean );
	
			if ( !(dataBeanNew.isOk()) )  {
					
				formBean.setMessage( "Error in Login." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("failureLogin");
					
			}  else  {
				
				// store userId and role to maintain user state
				session.setAttribute( "userId", dataBeanNew.getUserId() );  			
				session.setAttribute( "role", dataBeanNew.getRole() ); 

				formBeanNew = mapDataBeanToFormBean( dataBeanNew );

				formBean = formBeanNew;
				formBean.setMessage( "Login OK." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("successLogin");
			
			}
			
			
		}  else if ( (userAction.equalsIgnoreCase("loadUser")) )  {
			
			//  get userId and role from session and load into bean
			String userId = (String) session.getAttribute( "userId" );
			String role = (String) session.getAttribute( "role" );
			dataBean.setUserId( userId );
			dataBean.setRole( role );

			// call model			
			dataBeanNew = model.find( dataBean );
	
			if ( !(dataBeanNew.isOk()) )  {
					
				formBean.setMessage( "Error in retrieving user information." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("failureLoad");
					
			}  else  {
				
				dataBeanNew.setPasswordConfirm(dataBeanNew.getPassword());
				
				formBeanNew = mapDataBeanToFormBean( dataBeanNew );

				formBean = formBeanNew;
				formBean.setMessage( "Update user information." );
				request.setAttribute( "formBeanUser", formBean );
				forward = mapping.findForward("successLoad");

			}		
			
		}  else if ( (userAction.equalsIgnoreCase("cancel")) ) {

			formBean.setMessage( "If update user information is complete, use navigation panel on left to continue." );
			request.setAttribute( "formBeanUser", formBean );
			
			forward = mapping.findForward("successUpdate");
			
		}  else if ( (userAction.equalsIgnoreCase("Logout")) ) {

			// delete session		
			session.invalidate();

			// generate view
			forward = mapping.findForward("successLogout");			
			
		}  else  {

			forward = mapping.findForward("error");
			
		}
		
		return forward;
	}

	public static BeanUser mapFormBeanToDataBean ( FormBeanUser fb )  {
		
		BeanUser tb = new BeanUser();
		
		tb.setUserId    	( fb.getUserId() );
		tb.setNameFirst   	( fb.getNameFirst() );
		tb.setNameMiddle  	( fb.getNameMiddle() );
		tb.setNameLast    	( fb.getNameLast() );
		tb.setAddress1    	( fb.getAddress1() );
		tb.setAddress2    	( fb.getAddress2() );
		tb.setCity        	( fb.getCity() );
		tb.setState       	( fb.getState() );
		tb.setPostalCode   	( fb.getPostalCode() );
		tb.setCountry   	( fb.getCountry() );
		tb.setPhone   		( fb.getPhone() );
		tb.setFax    		( fb.getFax() );
		tb.setEmail       	( fb.getEmail() );
		tb.setPassword      	( fb.getPassword() );
		
		return tb;
		
	}
	
	private FormBeanUser mapDataBeanToFormBean ( BeanUser fb )  {
						
		FormBeanUser tb = new FormBeanUser();
		
		tb.setUserId    	( fb.getUserId() );
		tb.setNameFirst   	( fb.getNameFirst() );
		tb.setNameMiddle  	( fb.getNameMiddle() );
		tb.setNameLast    	( fb.getNameLast() );
		tb.setAddress1    	( fb.getAddress1() );
		tb.setAddress2    	( fb.getAddress2() );
		tb.setCity        	( fb.getCity() );
		tb.setState       	( fb.getState() );
		tb.setPostalCode   	( fb.getPostalCode() );
		tb.setCountry   	( fb.getCountry() );
		tb.setPhone   		( fb.getPhone() );
		tb.setFax    		( fb.getFax() );
		tb.setEmail       	( fb.getEmail() );
		tb.setPassword      	( fb.getPassword() );
		
		return tb;
						
	}

}