struts 2 convention 플러그 인의 사용 (1)


 
 
struts 2 convention 플러그 인 에 관 한 글 은 인터넷 상에 서 아직 매우 적 습 니 다. 오늘 연 구 했 습 니 다.
공식 문서 http://cwiki.apache.org/WW/convention-plugin.html
 
 
 
첨부 파일 내 소스 환경 (다운로드 페이지 끝까지)
Dynamic Web Project
eclipse3.3
jdk1.6
tomcat6
 
필요 jar 목록
 
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-convention-plugin-2.1.6.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
  •  
  • com.sunflower.actions.NavigatorAction
  • com.sunflower.actions.child.ChildAction
  • error.jsp
  • index.jsp
  • next.jsp
  • child/next.jsp
  • 테마: action 에서 기본 실행 방법 이 execute 인지 index 요청 url: navigator. action 인지 확인 합 니 다.  유형: dispatcher 방법: execute 또는 index  기본 방법 실행
  • 주제: 역 밖으로 뛰 어 내 리 는 방법 테스트 url: navigator!redirect.action   형식: redirect (이전 버 전 은 ServletRedirectResult. class 사용) 방법: redirect  역 밖으로 뛰 어 나가다
  • 테마: redirectAction, basePackage 내 하위 패키지 action 의 맵, 그리고 매개 변수 전달 요청 url: navigator!redirectAction.action   유형: redirectAction (이전 버 전 은 ServeltAction RedirectResult. class 사용) 방법: redirectAction location:child/child(basePackage=com.sunflower.actions,this package=com.sunflower.actions.child) 주의 1: ChildAction 내 execute 방법 은 location = "next. jsp" 를 가리 키 며 결과 페이지 next. jsp 가 루트 디 렉 터 리 자원 인지 / child / next. jsp 자원 인지 확인 합 니 다. 2: redirectAction 방법 은 요청 역할 영역 에 msg 정 보 를 설정 하여 msg 가 표시 되 었 는 지 확인 합 니 다.  역 내 action 으로 건 너 뛰 기
  • 테마: 오류 점프 요청 url: navigator!error.action    오류 생 성
  • 테마: @ Action 의 사용 은 Result 의 location 과 상대 적 인 차이 점 을 사용 합 니 다. 주석 @ Action (value = "/ test / childtest") 을 사용 합 니 다. 요청 url: / test / childTest. action   메모: 클래스 는 여전히 Navigator Action 입 니 다.  next. jsp 는 절대 경로 입 니 다.
  • 테마: @ Actions 의 사용 설명 @ Actions ({@ Action (value = "/ test / action 1"), @ Action (value = "/ test / action 2")} 요청 url: / test / action1. action url:/test/action2.action 메모: 클래스 는 여전히 Navigator Action 입 니 다.  action1   action2


  •  
    Web. xml 관건 부분
     
     
    <!-- ============================   struts2       ============================ -->
    	<filter>
    		<filter-name>struts2</filter-name>
    		<filter-class>
    			org.apache.struts2.dispatcher.FilterDispatcher
    		</filter-class>
    	</filter>
    	<filter-mapping>
    		<filter-name>struts2</filter-name>
    		<url-pattern>*.action</url-pattern>
    	</filter-mapping>

     
     
    struts 2. xml 설정 은 다음 과 같 습 니 다.
     
     
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    	<!--        //-->
    	<!--++++++++++++++++++++++++++++++++++++++++++++++++     -->
    	<constant name="struts.devMode" value="true" />
    	<!--            -->
    	<constant name="struts.i18n.reload" value="true" />
    	<!--          -->
    	<constant name="struts.configuration.xml.reload" value="true" />
    	<!-- convention      -->
    	<constant name="struts.convention.classes.reload" value="true" />
    	<!--++++++++++++++++++++++++++++++++++++++++++++++++       -->
    	<!--    -->
    	<constant name="struts.ui.theme" value="simple" />
    	<!--    -->
    	<constant name="struts.locale" value="zh_CN" />
    	<!--       -->
    	<constant name="struts.i18n.encoding" value="UTF-8" />
    	<!--    -->
    	<constant name="struts.action.extension" value="action,do,jsp" />
    	<!--          -->
    	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
    	<!--   Struts 2     Action       -->
    	<constant name="struts.enable.SlashesInActionNames" value="false" />
    	<!--          -->
    	<constant name="struts.convention.result.path" value="/"/> 
    	<!-- action   -->
    	<constant name="struts.convention.action.suffix" value="Action"/> 
    	<!--         -->
        <constant name="struts.convention.action.name.lowercase" value="true"/> 
        <!--       action     。   HelloWorldAction。    ,actionName hello_world。 -->
        <constant name="struts.convention.action.name.separator" value="_"/> 
        <!--      -->
        <constant name="struts.convention.action.disableScanning" value="false"/> 
        <!--     -->
        <constant name="struts.convention.default.parent.package" value="default"/> 
        <!--        。      action      。basePackage        ,    ,           。locators locators.basePackage             。 -->
        <constant name="struts.convention.package.locators" value="actions"/> 
        <!--       -->
        <constant name="struts.convention.package.locators.disable" value="false"/> 
        <!--       -->
        <constant name="struts.convention.package.locators.basePackage" value="com.sunflower.actions"/>
        <!--       -->
        <constant name="struts.convention.exclude.packages" value="org.apache.struts.*,org.apache.struts2.*,org.springframework.web.struts.*,org.springframework.web.struts2.*,org.hibernate.*"/>
        <!--      -->
        <!--    jar,        , action     jar -->
        <constant name="struts.convention.action.includeJars" value="" />
        <!--      -->
        <constant name="struts.convention.relative.result.types" value="dispatcher,freemarker"/> 
        <!-- 
               true,    action      /login,   HelloWorldAction。result    success,     /WEB-INF/pages/login/hello_world.jsp(   hello_world_success.jsp      ,   “_”  <constant name="struts.convention.action.name.separator" value="_"/>    )。     action result    “error”,   /WEB-INF/pages /login/hello_world_error.jsp。 
    
                false,    action      /login,   HelloWorldAction。result    success,     /WEB- INF/pages/login/hello_world/index.jsp(   success.jsp      )。     action result    “error”,   /WEB-INF/pages /login/hello_world/error.jsp。 
         -->
        <constant name="struts.convention.result.flatLayout" value="true"/>
        <constant name="struts.convention.action.mapAllMatches" value="false"/> 
        <!--       action -->
        <constant name="struts.convention.action.checkImplementsAction" value="true"/>
        <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
    	<constant name="struts.convention.redirect.to.slash" value="true"/>
    	<package name="default" extends="struts-default">
    		<interceptors>
    			<interceptor-stack name="defaultStack">
    				<interceptor-ref name="exception" />
    				<interceptor-ref name="servletConfig" />
    				<interceptor-ref name="actionMappingParams" />
    				<interceptor-ref name="staticParams" />
    				<interceptor-ref name="params" />
    			</interceptor-stack>
    		</interceptors>
    	</package>
    </struts>
    

     
     
    Navigator 액 션 소스 코드
     
    package com.sunflower.actions;
    
    import org.apache.struts2.convention.annotation.Action;
    import org.apache.struts2.convention.annotation.Actions;
    import org.apache.struts2.convention.annotation.Result;
    import org.apache.struts2.convention.annotation.Results;
    
    import com.opensymphony.xwork2.ActionContext;
    
    @Results( {
    		@Result(name = "next", location = "/next.jsp", type = "dispatcher"),
    		@Result(name = "error", location = "error.jsp", type = "dispatcher"),
    		@Result(name = "redirectAction", location = "./child/child", type = "redirectAction"),
    		@Result(name = "redirect", location = "http://sunflowers.iteye.com", type = "redirect") })
    public class NavigatorAction {
    	private String actionName;
    
    	public void setActionName(String actionName) {
    		this.actionName = actionName;
    	}
    
    	public String index() {
    		outputMsg("method:index");
    		return "next";
    	}
    
    	public String execute() {
    		outputMsg("method:execute,no index method");
    		return "next";
    	}
    
    	public String error() {
    		try {
    			throw new Exception();
    		} catch (Exception e) {
    			outputMsg(e);
    			return "error";
    		}
    	}
    
    	public String redirect() {
    		System.out.println("   :rediret");
    		return "redirect";
    	}
    
    	public String redirectAction() {
    		outputMsg("navigatorAction     ,    navigator!redirectAction.action,      ");
    		return "redirectAction";
    	}
    
    	@Action(value = "/test/childTest")
    	public String action() {
    		outputMsg("@action ---method:action");
    		return "next";
    	}
    
    	@Actions( { @Action(value = "/test/action1"),
    			@Action(value = "/test/action2") })
    	public String actions() {
    		outputMsg("@actions ---method:actions,action=" + actionName);
    		return "next";
    	}
    	private void outputMsg(Object msg) {
    		System.out.println(msg);
    		ActionContext.getContext().put("msg", msg);
    	}
    }
    

     
     
     ChildAction 소스 코드
     
    package com.sunflower.actions.child;
    
    import org.apache.struts2.convention.annotation.Result;
    import org.apache.struts2.convention.annotation.Results;
    
    @Results( {
    		@Result(name = "next", location = "next.jsp", type = "dispatcher")})
    public class ChildAction {
    	public String execute() {
    		System.out.println("childAction       ");
    		return "next";
    	}
    }
    

     
     index. jsp 원본 코드
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>struts2 convention  </title>
    <style>
    * {
    	line-height: 2em;
    }
    
    a:visited,a:hover,a:active,a:link {
    	color: #00f;
    }
    </style>
    </head>
    <body>
      Action&nbsp;
    <ul class="horizontal">
    	<li>com.sunflower.actions.NavigatorAction</li>
    	<li>com.sunflower.actions.child.ChildAction</li>
    </ul>
        &nbsp;
    <ul class="horizontal">
    	<li>error.jsp</li>
    	<li>index.jsp</li>
    	<li>next.jsp</li>
    	<li>child/next.jsp</li>
    </ul>
    <ul>
    	<li>  :  action        execute  index<br />
    	  url:navigator.action&nbsp;&nbsp;<br />
    	  :dispatcher <br />
    	  :execute index <br />
    	<a href="./navigator.action">      </a></li>
    	<li>  :         <br />
    	  url:navigator!redirect.action&nbsp;&nbsp; <br />
    	  :redirect(     ServletRedirectResult.class) <br />
    	  :redirect <br />
    	<a href="./navigator!redirect.action">     </a></li>
    	<li>  :  redirectAction、basePackage   action   、      <br />
    	  url:navigator!redirectAction.action&nbsp;&nbsp; <br />
    	  :redirectAction (     ServeltActionRedirectResult.class) <br />
    	  :redirectAction <br />
    	location:child/child(basePackage=com.sunflower.actions,this&nbsp;package=com.sunflower.actions.child)
    	<br />
    	<font color="red">  </font><br />
    	1:ChildAction  execute      location="next.jsp",      next.jsp        /child/next.jsp  
    	<br />
    	2: redirectAction           msg  ,       msg <br />
    	<a href="./navigator!redirectAction.action">     action</a></li>
    	<li>  :    <br />
    	  url:navigator!error.action&nbsp;&nbsp; <br />
    	<a href="./navigator!error.action">    </a></li>
    	<li>  :@Action     Result location        <br />
    	    @Action(value="/test/childTest") <br />
    	  url:/test/childTest.action&nbsp;&nbsp; <br />
    	<font color="red">  </font>:    NavigatorAction <br />
    	<a href="./test/childTest.action">next.jsp     </a></li>
    	<li>  :@Actions   <br />
    	  @Actions({ @Action(value="/test/actions1"),
    	@Action(value="/test/actions2") })<br />
    	  <br />
    	url:/test/action1.action <br />
    	url:/test/action2.action <br />
    	<font color="red">  </font>:    NavigatorAction <br />
    	<a href="./test/action1.action?actionName=action1">action1</a>&nbsp;<a
    		href="./test/action2.action?actionName=action2">action2</a></li>
    </ul>
    </body>
    </html>
       
     next. jsp 소스 코드
     
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title><style>
    *{
    line-height:3em;
    }
    </style>
    </head>
    <body>
    <h1>struts2 &nbsp;&nbsp;&nbsp;convention      </h1>
        
    <br />
    ${msg}
    <br />
    <a href="#" onclick="window.history.go(-1);return false;">  </a>
    </body>
    </html>

     
     
     child / next. jsp 원본 코드
     
    <%@ page language="java" contentType="text/html; charset=UTF-8"
    	pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title><style>
    *{
    line-height:3em;
    }
    </style>
    </head>
    <body>
    child/next.jsp:{msg}
    </body>
    </html>

     
     
     
     
     
     
     
     
     
     
     
     
     
     

    좋은 웹페이지 즐겨찾기