Struts1의 다양한 Action들


struts-extras-1.3.8.jar에서 지원하며 다양한 액션을 구현 할 수 있다.

■ Action 상속 관계도



1. ForwardAction



 struts-config.xml 의 ActionMapping에서 action 요소에 parameter 속성으로 지정한 URL로 forward 하게 된다. 직접 해당 URL을 호출하면 RequestProcessor을 통해 바로 View page로 이동하지만
ForwardAction을 사용하게 되면 스트럿츠 컨트롤러 서블릿의 기능(폼 빈의 처리 등)을 사용 할 수 있다는 장점이 있다.

<!--직접 해당 URL 호출-->
<action path="/info/index" forward="/info/register.jsp"/>

<!-- ForwardAction을 사용해서 ViewPage요청 -->
<action path="/info/index"
            type=“org.apache.struts.actions.ForwardAction”
            parameter="/info/register.jsp” />


2. IncludeAction.

 

 ForwardAction과 동일한 방식이며 비즈니스 로직에서 처리 후 RequestProcessor로 진입하지 않고 바로 페이지로 이동한다. View page로의 빠른 이동이 가능하다는 장점이 있지만, Request Prcessor를 통과 하지 않기 때문에 page Encoding,validator 등 을 사용할 수 없다.

<!-- IncludeActiond 사용 -->
<action path="/index"
type = "org.apache.struts.actions.IncludeAction"
parameter = "/info/register.jsp"/>

3. DispatchAction

 하나의 Action에서 두개 이상의 ActionForward를 구현할 때 사용된다.


 JSP form 페이지에서 name이 struts-config.xml 에서 설정해논 parameter의 이름과 일치해야 실행된다.

- struts-config.xml

.... 생략 ....

    <action-mappings>
   
   <action path = "/dispatchIndex"
    type = "org.apache.struts.actions.ForwardAction"
    parameter = "/dispatch/register.jsp"/>
    
  <!-- unknow 은 input 속성을 대체하기 위한 속성이다. 입력 폼이 여러개 발생할 때 사용된다.  -->
  <action path = "/dispatch"
    name = "bean"
    scope = "request"
    unknown = "true"
    parameter = "method"
    type = "com.myhome.dispatch.InfoDispatchAction">
    
<!-- forward 이름이 메소드 마다 달라야 한다 -->
   <forward name="result" path = "/dispatch/result.jsp"/>
   <forward name="list" path = "/dispatch/list.jsp"/>
   <forward name="query" path = "/dispatch/modify.jsp"/>
   <forward name="update" path = "/dispatch/template.jsp"/>
   <forward name="delete" path = "/dispatch/template.jsp"/>
  </action>
    </action-mappings>

.... 생략 ....

- InfoDispatchAction .java

//하나의 Action에서 여러개의 action을 구현했다.

package com.myhome.dispatch;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.myhome.info.beans.InfoFormBean;
import com.myhome.info.dao.InfoDAO;
import com.myhome.info.dto.InfoDTO;


public class InfoDispatchAction extends DispatchAction{
 
 public ActionForward register(ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response)
          throws Exception{
  
  /*form parameter를 bean으로 받는다*/
  InfoFormBean bean = new InfoFormBean();
  bean.setName(request.getParameter("name"));
  bean.setSex(request.getParameter("sex"));
  bean.setTel(request.getParameter("tel"));
  
  /*bean property 를 dto로 복사한다.*/
  InfoDTO dto = new InfoDTO();
  BeanUtils.copyProperties(dto, bean);
  dto.setWdate(this.getWdate());
  
  /*info table에 연동한다.*/
  
  new InfoDAO().register(dto);
  
  /*result.jsp로 포워드하기 위해 리퀘스트 영역에 dto를 binding한다*/
  request.setAttribute("dto", dto);
  
  return mapping.findForward("result");
 }
 
 public ActionForward list(ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
         throws Exception{
  
  List<InfoDTO> list = null;
  
  list = new InfoDAO().getAllQuery();
  
  request.setAttribute("list", list);

  return mapping.findForward("list");
 }
 
 public ActionForward query(ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
         throws Exception{
  InfoFormBean bean = (InfoFormBean)form;
  InfoDTO dto = new InfoDTO();
  BeanUtils.copyProperties(dto, bean);
  
  //쿼리된 object를 리퀘스트 영역에 바인딩 한다.
  
  request.setAttribute("dto", new InfoDAO().getQuery(dto));

  return mapping.findForward("query");
 }
 
 public ActionForward update(ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
         throws Exception{
  /*form parameter를 InfoFormBean으로 받는다*/
  
  /**
   *  ActionForm의 역할
   *   Form parameter의 정보를 참조하기 위해
   *   ActionForm의 객체를 초기화 한다 - reset()
   * 
   *   form parameter의 정보를 받아 유효성 검사를 실시한다 - validate()
   * 
   *   참조한 폼 정보를 form-bean에 설정된 bean으로 전달한다.
   * */
  
  InfoFormBean bean = (InfoFormBean)form;
  
  /*bean의 객체를 Entity(DTO)로 property를 복사한다.*/
  InfoDTO dto = new InfoDTO();
  BeanUtils.copyProperties(dto, bean);
  
  new InfoDAO().update(dto);
  

  return mapping.findForward("update");
 }
 
 public ActionForward delete(ActionMapping mapping,
         ActionForm form,
         HttpServletRequest request,
         HttpServletResponse response)
         throws Exception{

  InfoFormBean bean = (InfoFormBean)form;
  
  /*bean의 객체를 Entity(DTO)로 property를 복사한다.*/
  InfoDTO dto = new InfoDTO();
  BeanUtils.copyProperties(dto, bean);
  new InfoDAO().delete(dto);
  return mapping.findForward("delete");
 }
 protected String getWdate(){
  return new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
 }
 
}


자세한 내용은 파일을 참조한다.





'FrameWork > Struts1' 카테고리의 다른 글

Struts1 FileDownload  (0) 2009.08.18
Struts1 fileUpload  (0) 2009.08.18
Struts1의 Action 4  (0) 2009.08.16
Struts1의 Action 3  (0) 2009.08.16
Struts1의 Action 2  (0) 2009.08.12
Struts1에서 ActionForm 사용하기.  (0) 2009.07.25
Struts1 에서 iBatis 사용하기  (0) 2009.07.05
Struts 1 을 사용하여 간단한 회원가입, 리스트 불러오기.  (0) 2009.07.05
Struts1 개발환경 설정.  (0) 2009.07.05
Struts(스트럿츠) 란?  (0) 2009.06.27

+ Recent posts