2013-11-14 9 views
1

Adobe의 웹 사이트 (참조 용으로 코드 헤더에 표시된 링크)의 기사에 따라 간단한 서비스 레이어 인터페이스를 작성하려고합니다. 템플릿/구성 요소를 기반으로 페이지를로드하려고하면 오류가 발생합니다. [componentname] _jsp 유형의 메소드 [methodname]이 정의되지 않았습니다. 공공 및 액세스 수정 자, 다른 서명 및 반환 형식, 호출 코드 위에 메서드를 이동하고 항상 동일한 결과를 얻는 간단한 단일 메서드를 사용하여 시도했습니다. 메서드 이름을 주석 처리하고 중괄호로 묶은 블록으로 코드를 포함하면 OK가 컴파일됩니다. 이것이 CQ/AEM v5.6.1에서 가능하지 않거나 잘못된 것이 있습니까?구성 요소 .jsp에서 메소드를 사용할 수 있습니까?

아래와 같은 전체 코드 및 예외가 나와 있습니다 (여러 가지 실험 버전이 주석 처리되고 원래 getPath (...) 메소드가 생략되어 있음). 시간 내 미리 감사드립니다.

<%-- 

    apipage component: implements service layer pages 
    see: http://blogs.adobe.com/contentmanagement/2012/08/19/how-to-integrate-with-other-web-services-without-creating-osgi-packages/ 

--%><% 
%><%@include file="/libs/foundation/global.jsp"%> 
    <%@page import="org.apache.commons.httpclient.HttpClient, 
        org.apache.commons.httpclient.params.HttpClientParams, 
        org.apache.commons.httpclient.DefaultHttpMethodRetryHandler, 
        org.apache.commons.httpclient.methods.PostMethod, 
        org.json.JSONObject" 
%><%@page session="false" %><% 
%><% 
    if (request.getParameter("action") != null) { 
     if (request.getParameter("action").equals("getFields") && 
       request.getParameter("param1") != null && 
        request.getParameter("param2") != null) { 
      //response.setContentType("application/json"); 
      //   out.write(getPath((String)request.getParameter("param1"), (String)request.getParameter("param2")); 
      String str1 = "param1"; 
      String str2 = "param2"; 
      //out.write(getPath(str1, str2)); 
      int i = doNothing(); 
     } 
    } else { 
     response.setContentType("application/json"); 
     out.write("{\"error\":\"error\"}"); 
    } 

    /*public*/ int doNothing() { 
     return 1; 
    } 
%> 

Error during include of component '/apps/trp/components/layouts/apipage' 
Error Message: 

org.apache.sling.api.scripting.ScriptEvaluationException: org.apache.sling.scripting.jsp.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 24 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp 
The method doNothing() is undefined for the type apipage_jsp 
21:    String str1 = "param1"; 
22:    String str2 = "param2"; 
23:    //out.write(getPath(str1, str2)); 
24:    int i = doNothing(); 
25:   } 
26:  } else { 
27:   response.setContentType("application/json"); 


An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp 
Syntax error on token "int", new expected 
28:   out.write("{\"error\":\"error\"}"); 
29:  } 
30: 
31: /*public*/ int doNothing() { 
32:   return 1; 
33:  } 
34: 


An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp 
doNothing cannot be resolved to a type 
28:   out.write("{\"error\":\"error\"}"); 
29:  } 
30: 
31: /*public*/ int doNothing() { 
32:   return 1; 
33:  } 
34: 


An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp 
Syntax error, insert ";" to complete Statement 
28:   out.write("{\"error\":\"error\"}"); 
29:  } 
30: 
31: /*public*/ int doNothing() { 
32:   return 1; 
33:  } 
34: 


An error occurred at line: 32 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp 
Void methods cannot return a value 
29:  } 
30: 
31: /*public*/ int doNothing() { 
32:   return 1; 
33:  } 
34: 
35: /* 

Processing Info: 
Page = /content/trp/en/AfrescoAPI 
Resource Path = /content/trp/en/AfrescoAPI/jcr:content 
Cell = apipage 
Cell Search Path = apipage 
Component Path = /apps/trp/components/layouts/apipage  
Sling Request Progress: 

     0 (2013-11-14 10:41:01) TIMER_START{Request Processing} 
     0 (2013-11-14 10:41:01) COMMENT timer_end format is {<elapsed msec>,<timer name>} <optional message> 
     0 (2013-11-14 10:41:01) LOG Method=GET, PathInfo=/content/trp/en/AfrescoAPI.html 
     0 (2013-11-14 10:41:01) TIMER_START{ResourceResolution} 
     0 (2013-11-14 10:41:01) TIMER_END{0,ResourceResolution} URI=/content/trp/en/AfrescoAPI.html resolves to Resource=JcrNodeResource, type=cq:Page, superType=null, path=/content/trp/en/AfrescoAPI 
     0 (2013-11-14 10:41:01) LOG Resource Path Info: SlingRequestPathInfo: path='/content/trp/en/AfrescoAPI', selectorString='null', extension='html', suffix='null' 
     0 (2013-11-14 10:41:01) TIMER_START{ServletResolution} 
     0 (2013-11-14 10:41:01) TIMER_START{resolveServlet(JcrNodeResource, type=cq:Page, superType=null, path=/content/trp/en/AfrescoAPI)} 
     0 (2013-11-14 10:41:01) TIMER_END{0,resolveServlet(JcrNodeResource, type=cq:Page, superType=null, path=/content/trp/en/AfrescoAPI)} Using servlet /libs/foundation/components/primary/cq/Page/Page.jsp 
     0 (2013-11-14 10:41:01) TIMER_END{0,ServletResolution} URI=/content/trp/en/AfrescoAPI.html handled by Servlet=/libs/foundation/components/primary/cq/Page/Page.jsp 
     0 (2013-11-14 10:41:01) LOG Applying Requestfilters 
     0 (2013-11-14 10:41:01) LOG Calling filter: org.apache.sling.bgservlets.impl.BackgroundServletStarterFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: org.apache.sling.i18n.impl.I18NFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: org.apache.sling.rewriter.impl.RewriterFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.designimporter.CanvasPageDeleteRequestFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.adobe.cq.history.impl.HistoryRequestFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.WCMRequestFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.adobe.granite.optout.impl.OptOutFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.theme.impl.ThemeResolverFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.foundation.forms.impl.FormsHandlingServlet 
     0 (2013-11-14 10:41:01) LOG Calling filter: org.apache.sling.engine.impl.debug.RequestProgressTrackerLogFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.mobile.core.impl.redirect.RedirectFilter 
     0 (2013-11-14 10:41:01) LOG RedirectFilter did not redirect (MobileUtil.isMobileResource() returns false) 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.warp.TimeWarpFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.AuthoringUIModeServiceImpl 
     0 (2013-11-14 10:41:01) LOG Applying Componentfilters 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.personalization.impl.TargetComponentFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.WCMComponentFilter 
     0 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.WCMDebugFilter 
     0 (2013-11-14 10:41:01) TIMER_START{/libs/foundation/components/primary/cq/Page/Page.jsp#0} 
     0 (2013-11-14 10:41:01) LOG Including resource JcrNodeResource, type=trp/components/layouts/apipage, superType=null, path=/content/trp/en/AfrescoAPI/jcr:content (SlingRequestPathInfo: path='/content/trp/en/AfrescoAPI/jcr:content', selectorString='null', extension='html', suffix='null') 
     0 (2013-11-14 10:41:01) TIMER_START{resolveServlet(JcrNodeResource, type=trp/components/layouts/apipage, superType=null, path=/content/trp/en/AfrescoAPI/jcr:content)} 
    16 (2013-11-14 10:41:01) TIMER_END{16,resolveServlet(JcrNodeResource, type=trp/components/layouts/apipage, superType=null, path=/content/trp/en/AfrescoAPI/jcr:content)} Using servlet /apps/trp/components/layouts/apipage/apipage.jsp 
    16 (2013-11-14 10:41:01) LOG Applying Includefilters 
    16 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.personalization.impl.TargetComponentFilter 
    16 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.WCMComponentFilter 
    16 (2013-11-14 10:41:01) LOG Calling filter: com.day.cq.wcm.core.impl.WCMDebugFilter 
    16 (2013-11-14 10:41:01) TIMER_START{/apps/trp/components/layouts/apipage/apipage.jsp#1} 
    156 (2013-11-14 10:41:01) LOG SCRIPT ERROR: org.apache.sling.scripting.jsp.jasper.JasperException: Unable to compile class for JSP: //An error occurred at line: 24 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp/The method doNothing() is undefined for the type apipage_jsp/21:    String str1 = "param1";/22:    String str2 = "param2";/23:    //out.write(getPath(str1, str2));/24:    int i = doNothing();/25:   }/26:  } else {/27:   response.setContentType("application/json");///An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp/Syntax error on token "int", new expected/28:   out.write("{\"error\":\"error\"}");/29:  }/30: /31: /*public*/ int doNothing() {/32:  return 1;/33: }/34: ///An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp/doNothing cannot be resolved to a type/28:   out.write("{\"error\":\"error\"}");/29:  }/30: /31: /*public*/ int doNothing() {/32:   return 1;/33: }/34: ///An error occurred at line: 31 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp/Syntax error, insert ";" to complete Statement/28:   out.write("{\"error\":\"error\"}");/29:  }/30: /31: /*public*/ int doNothing() {/32:   return 1;/33: }/34: ///An error occurred at line: 32 in the jsp file: /apps/trp/components/layouts/apipage/apipage.jsp/Void methods cannot return a value/29:  }/30: /31: /*public*/ int doNothing() {/32:   return 1;/33: }/34: /35: /*/ 
    156 (2013-11-14 10:41:01) TIMER_END{140,/apps/trp/components/layouts/apipage/apipage.jsp#1} 
    172 (2013-11-14 10:41:01) LOG Found processor for post processing ProcessorConfiguration: {contentTypes=[text/html],order=-1, active=true, valid=true, processErrorResponse=true, pipeline=(generator=Config(type=htmlparser, config={}), transformers=(Config(type=linkchecker, config={}), Config(type=mobile, config=JcrPropertyMap [node=node /libs/cq/config/rewriter/default/transformer-mobile, values={component-optional=true, jcr:primaryType=nt:unstructured}]), Config(type=mobiledebug, config=JcrPropertyMap [node=node /libs/cq/config/rewriter/default/transformer-mobiledebug, values={component-optional=true, jcr:primaryType=nt:unstructured}]), Config(type=contentsync, config=JcrPropertyMap [node=node /libs/cq/config/rewriter/default/transformer-contentsync, values={component-optional=true, jcr:primaryType=nt:unstructured}]), serializer=Config(type=htmlwriter, config={}))} 
    172 (2013-11-14 10:41:01) TIMER_END{172,Request Processing} Dumping SlingRequestProgressTracker Entries 

답변

3

스크립틀릿은 악마입니다. 스크립틀릿을 사용해서는 안됩니다. 그들이 존재하는 것을 잊어 버려라. JSP EL과 JSTL을 배우십시오. MVC 프레임 워크를 사용하십시오.

그렇다면 여기에 코드가 작동하지 않는 이유가 있습니다. <%%> 안에있는 모든 것은 JSP 용으로 생성 된 서블릿의 서비스 메소드 안에있는 코드입니다. 그래서, 다음과 같은 JSP :

<html> 
    <% System.out.println("hello"); %> 
</html> 

다음과 같습니다 서블릿의 컨테이너에 의해 변환 :

public class YourJsp extends JspServlet { 

    public void jspService(HttpServletRequest request, HttpServletResponse response) { 
     JspWriter out = createJspWriter(response); 
     out.println("<html>"); 
     System.out.println("hello"); 
     out.println("</html>"); 
    } 
} 
당신은 당신의 코드가

같은 것을로 번역 할 것을 FOM 위의 예를 추론 할 수

public void jspService(HttpServletRequest request, HttpServletResponse response) { 
     ... 

     int doNothing() { 
      return 1; 
     } 
    } 

따라서 다른 메소드 내부에서 메소드를 선언 할 수 있습니다. 이는 유효하지 않은 Java 코드이므로 JSP가 유효하지 않은 것입니다.

<%! 
    int doNothing() { 
     return 1; 
    } 
%> 

위의 코드는 생성 된 서블릿의 클래스의 일부가 될 것입니다 :

는 JSP의 방법을 선언하려면 다음 구문이 필요합니다 ("선언"을 의미 느낌표를, 주) , 해당 jspService() 메소드가 아닙니다.

그렇다고해서 JSP에 스크립틀릿을 설치해서는 안됩니다. 더 적은 방법. Thos는 Java 소스 파일에 있어야합니다. 스크립틀릿은 10 년 이상 사용되지 않습니다. JSP는 뷰 구성 요소이며 Java의 컨트롤러로 준비된 데이터에서 HTML 마크 업을 생성하는 고유 한 책임이 있습니다.

2

당신은 선언 구문을 사용합니다 (<퍼센트를 ... %>!) :

<%! 
    public int doNothing() { 
     return 1; 
    } 
%> 
<% 
    int test = doNothing(); 
%> 
관련 문제