2012-02-27 2 views
4

이것은 두 부분으로 나뉜 질문입니다.선행 결과를 Java 함수에서 반환 할 수 있습니까?

Tim Tripoon의 Fancy XPage Typeahead 스크립트를 여러 응용 프로그램에서 사용하여 특정 데이터베이스의 서로 다른 여러보기를 기반으로 선행 검사 목록을 반환합니다.

  1. 는 블로그에 나열된 서버 사이드 자바 스크립트 XPages 네이티브 선행 입력 기능에 의해 선택 될 수있는 동일한 결과를 반환하는 자바 클래스로 변환 할 수 있습니다.

  2. 이 클래스는 모든 서버에 배포되는 확장 라이브러리의 일부가 될 수 있으므로 즉시 사용할 수 있도록 모든 응용 프로그램에서 사용할 수 있으며, 그렇다면 XPage에서 어떻게 호출해야합니까?

답변

7

그렇습니다. Java로 변환 할 수없는 SSJS의 예는 생각할 수 없습니다. 여기에 Tim Tripcony의 SSJS가 포팅되었습니다. 자바.

import java.util.HashMap; 
import java.util.Map; 
import lotus.domino.*; 
import com.ibm.domino.xsp.module.nsf.NotesContext; 
public class TypeAhead { 
    public static String directoryTypeAhead(String searchValue) { 
     String returnList = ""; 
     try { 

      Database directory = NotesContext.getCurrent().getCurrentSession().getDatabase("", "names.nsf"); 
      View allUsers = directory.getView("($Users)"); 
      Map<String, HashMap<String, String>> matches = new HashMap<String, HashMap<String, String>>(); 
       Map<String, String> individualMatches = new HashMap<String, String>(); 
      Map<String, Boolean> includeForm = new HashMap<String, Boolean>(); 
      includeForm.put("Person", Boolean.TRUE); 
      includeForm.put("Group", Boolean.TRUE); 
      ViewEntryCollection matchingEntries = allUsers.getAllEntriesByKey(searchValue, false); 
      ViewEntry entry = matchingEntries.getFirstEntry(); 
      int resultCount = 0; 
      while (entry != null) { 
       Document matchDoc = entry.getDocument(); 
       String matchType = matchDoc.getItemValueString("Form"); 
       if ((Boolean)includeForm.get(matchType)) { 
        String fullName = matchDoc.getItemValue("FullName").elementAt(0).toString(); 
        if (matches.get(fullName) == null) { 
         resultCount++; 
         Name matchName = NotesContext.getCurrent().getCurrentSession().createName(fullName); 
         individualMatches = new HashMap<String, String>(); 
         individualMatches.put("cn", matchName.getCommon()); 
         individualMatches.put("photo", matchDoc.getItemValueString("photoUrl")); 
         individualMatches.put("job", matchDoc.getItemValueString("jobTitle")); 
         individualMatches.put("email", matchDoc.getItemValueString("internetAddress")); 
         matches.put(fullName, (HashMap<String, String>) individualMatches); 
        } 
       } 
       if (resultCount > 9) { 
        entry = null; 
       } 
       else { 
        entry = matchingEntries.getNextEntry(entry); 
       } 
      } 
      returnList = "<ul>"; 
      for (Map<String, String> match : matches.values()) {  
       String matchDetails = "<li><table><tr><td><img class=\"avatar\" src=\"" + match.get("photo") + "\"/></td><td valign=\"top\"><p><strong>" + match.get("cn") + "</strong></p><p><span class=\"informal\">" + match.get("job") + "<br/>" + match.get("email") + "</span></p></td></tr></table></li>"; 
       returnList += matchDetails; 
      } 
      returnList += "</ul>"; 
     } catch(Exception e) { 
      System.out.println(e); 
     } 
     return returnList; 
    } 
} 

지금까지 모든 확장 라이브러리를 만드는 당신은 정말 당신이 플러그인 항아리에 넣어되고 싶은 생각 얻을 후 새 8.5을 사용할 수있는 기능 및 업데이트 사이트를 만들해야 .3 모든 서버에 그것을 복제하는 기능.

당신은 당신의 xpage의 내부에서 다음을 수행하여이 코드를 사용할 수 있습니다 : 당신이 어떤 XSP 라이브러리 (예 : XSP 스타터 키트)에 토비의 예와 같이 클래스를 추가하는 경우

<xp:inputText id="inputText1" value="#{viewScope.someVar}"> 
    <xp:typeAhead mode="partial" minChars="1" valueMarkup="true" 
    var="searchValue" 
    valueList="#{javascript:return com.tobysamples.demo.TypeAhead.directoryTypeAhead(searchValue);}"> 
    </xp:typeAhead></xp:inputText> 
+1

를, 그것은 런타임에 사용할 수 있습니다 해당 라이브러리에 대해 정의 된 종속성이있는 모든 응용 프로그램. 또한 라이브러리를 전역으로 설정하면 종속성이 정의되지 않은 경우에도 모든 응용 프로그램에서 사용할 수 있습니다. –

+3

P. 당신은 정말로 StringBuilder와 같은 것을 사용하여 단지 연결 대신 모든 마크 업을 구성해야합니다. 자바 스크립트에서 + = 접근법은 괜찮습니다. (거대한 연결의 경우 배열로 푸시 한 다음 조인을하는 것이 더 효율적이지만) 자바에서 문자열 연결은 비효율적입니다. –

0

SSJS를 Java로 변환하는 것이 어렵지 않다고 생각합니다. 관리 Bean을 사용하면 필드로 java 메소드에 즉시 액세스 할 수 있습니다. 그리고 확장 라이브러리의 멋진 기능을 향상시켜야합니다. 모두가이 멋진 기능에 도움이 될 수 있습니다.

관련 문제