xpages不同数据库共享ssjs

        如果在xpages不同的数据库可以共享ssjs就方便了,类似于前端的js一样,这是以前的想法。最近在网上真的看到这篇类似文章,以下是做个简单的demo,有时间交流:QQ 873968102

demo下载

0)这里是原文

http://hasselba.ch/blog/?p=832

1)引用java类

package ch.hasselba.xpages.util.ssjs;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import com.ibm.xsp.util.StreamUtil;
import com.ibm.xsp.page.compiled.ExpressionEvaluatorImpl;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

public class SSJSUtil {
	private static final String NEWLINE = "\n";
    private static final String SSJS_EXPRESSION_BEGIN = "#{javascript:";
    private static final String SSJS_EXPRESSION_END = "}";

    /**
     * Loads SSJS code from a given URL and executes it
     * Declared methods and objects are reachable for other SSJS code
     * 
     * @param url of the SSJS code
     * @return Object resulting object from SSJS execution
     * @author Sven Hasselbach
     * @version 1.0.1
     * @category Utility
     */
    public static Object executeSSJSFromURL( final String url ){
        return execute( loadFromURL( url ) );
    }

    /**
     * loads a URL stream and converts it to a string
     * @param url of the resource
     * @return String containing the data loaded from given url
     * @author Sven Hasselbach
     * @version 1.0.1
     * @category Utility
     */
    public static String loadFromURL( final String url ){
        String ret = null;
        try{
            FacesContext fc = FacesContext.getCurrentInstance();
            InputStream in = StreamUtil.getInputStream(fc, url);
            ret = inputStreamToString( in );
        }catch(Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * executes given SSJS code and returns the result (if any)
     * functions / libraries are added to runtime 
     * 
     * @param ssjsCode code to execute
     * @return resulting object
     * @author Sven Hasselbach
     * @version 1.0.2
     * @category SSJS
     */
    public static Object execute( final String ssjsCode ){
        Object ret = null;

        try{
            String valueExpr = SSJS_EXPRESSION_BEGIN + ssjsCode + SSJS_EXPRESSION_END;
            FacesContext fc = FacesContext.getCurrentInstance();
            ExpressionEvaluatorImpl evaluator = new ExpressionEvaluatorImpl( fc );
            ValueBinding vb = evaluator.createValueBinding( fc.getViewRoot(), valueExpr, null, null);
            ret = vb.getValue(fc);
        }catch(Exception e){
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * converts the data from a given inputstream to a string
     * 
     * @param in InputStream to convert
     * @return String containing data from input stream
     * @throws IOException
     * @author Sven Hasselbach
     * @version 1.0.1
     * @category Utility
     */
    public static String inputStreamToString(final InputStream inStream) throws IOException {
        BufferedReader bufReader = new BufferedReader( new InputStreamReader(inStream) );
        StringBuilder strBuilder = new StringBuilder();
        String line = null;

        while ((line = bufReader.readLine()) != null) {
            strBuilder.append(line);
            strBuilder.append( NEWLINE );
        }
        bufReader.close();

        return strBuilder.toString();
     }
}

2)写个ssjs放到html路径下面

function test(){
	return "I am a ssjs test";
}

function hello(){
	return 'hello world!'
}

function geturl(){
	return context.getUrl();
}



3)在xpages页面引用

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">
	
	<xp:label id="label1">
		<xp:this.value>
			<![CDATA[#{javascript:
			importPackage( ch.hasselba.xpages.util.ssjs );
			var url=@Left(context.getUrl(),context.getUrl().getPath())+'/873968102.jss';
			SSJSUtil.executeSSJSFromURL(url);
			test();
         }]]>
		</xp:this.value>
	</xp:label>
	<xp:br></xp:br>
	
	<xp:label id="label2">
		<xp:this.value>
			<![CDATA[#{javascript:
			//SSJSUtil.executeSSJSFromURL(url);
			hello();
         }]]>
		</xp:this.value>
	</xp:label>
	<xp:br></xp:br>
	<xp:br></xp:br>
	<xp:br></xp:br>
	<xp:inputText id="inputText1" defaultValue="#{javascript:geturl();}"
		style="width:364.0px">
	</xp:inputText>
</xp:view>


发布了76 篇原创文章 · 获赞 17 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/weijia3624/article/details/51387186
今日推荐