-- 作者:jiangqiangln
-- 发布时间:10/19/2009 10:34:00 AM
-- [求助]我把一个可运行的java程序改为jsp+javabean,错在哪里
能正常运行的java程序: package chy; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; /** * @author Vision * */ public class display { /** * @param args */ public String filePath = "src\\ontology\\test.rdf-xml.owl";// 本体文件 Model model;//定义本体模型 /** * 构造函数,加载本体模型 */ public display() { model = ModelFactory.createDefaultModel(); loadModel(); } /** * 提交sparql的查询,输出返回结果 * @param queryStr * @return */ public boolean runQuery(String queryStr) { Query query = QueryFactory.create(queryStr); QueryExecution qe = QueryExecutionFactory.create(query, model); ResultSet results = qe.execSelect(); while (results.hasNext()) { QuerySolution soln = results.nextSolution();//查询结果中的每一条(称之为满足条件的一个solution) System.out.print(soln.get("book").toString()); System.out.print("\t"); System.out.print(soln.get("hasName").toString()); System.out.print("\t"); //System.out.print(soln.get("o").toString()); /*Iterator iter = soln.varNames(); /*while (iter.hasNext()) { String var = (String) iter.next(); System.out.print(soln.get(var).toString()); System.out.print("\t"); }*/ System.out.println("\n"); } results = null; qe.close(); return true; } /** * 加载本地文件形式的本体模型,失败系统退出 * * @return */ private void loadModel() { InputStreamReader in; try { FileInputStream file = new FileInputStream(filePath); in = new InputStreamReader(file, "UTF-8");//处理中文 model.read(in, null); /* * 基于规则的推理代码段,有必要的加 String rule = "[owl:inverseOf: (?pa * owl:inverseOf ?pb) (?a ?pa ?b)->(?b ?pb ?a)]"; Reasoner reasoner = * new GenericRuleReasoner(Rule.parseRules(rule)); model = * ModelFactory.createInfModel(reasoner, model); */ in.close(); } catch (FileNotFoundException e) { System.out.println("无法打开本体文件,程序将终止"); System.exit(0); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } public static void main(String[] args) { display myLearn=new display(); String queryStr="prefix test: <http://vision.pku.edu.cn/test.owl#>"+ "select * "+ "from <http://vision.pku.edu.cn/test.owl>"+ "where "+ "{"+ "?book test:hasName ?hasName."+ "}"; myLearn.runQuery(queryStr ); } } 请看改后 javabean package cc; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import com.hp.hpl.jena.query.Query; import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QuerySolution; import com.hp.hpl.jena.query.ResultSet; import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.ModelFactory; /** * @author Vision * */ public class Success { /** * @param args */ public String filePath = "src\\ontology\\test.rdf-xml.owl";// 本体文件 Model model;//定义本体模型 ResultSet results; private String queryStr; /** * 构造函数,加载本体模型 */ public Success() { } public void setQuery(String queryStr){ this.queryStr=queryStr; } public String getQuery(){ return queryStr; } /** * 提交sparql的查询,输出返回结果 * @param queryStr * @return */ public ResultSet runQuery() { model = ModelFactory.createDefaultModel(); loadModel(); Query query = QueryFactory.create(queryStr); QueryExecution qe = QueryExecutionFactory.create(query, model); results = qe.execSelect(); return results; } /** * 加载本地文件形式的本体模型,失败系统退出 * * @return */ private void loadModel() { InputStreamReader in; try { FileInputStream file = new FileInputStream(filePath); in = new InputStreamReader(file, "UTF-8");//处理中文 model.read(in, null); /* * 基于规则的推理代码段,有必要的加 String rule = "[owl:inverseOf: (?pa * owl:inverseOf ?pb) (?a ?pa ?b)->(?b ?pb ?a)]"; Reasoner reasoner = * new GenericRuleReasoner(Rule.parseRules(rule)); model = * ModelFactory.createInfModel(reasoner, model); */ in.close(); } catch (FileNotFoundException e) { System.out.println("无法打开本体文件,程序将终止"); System.exit(0); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } } jsp程序: <jsp:useBean id="gh" class="cc.Success" /> <% String queryStr="prefix test: <http://vision.pku.edu.cn/test.owl#>"+ "select * "+ "from <http://vision.pku.edu.cn/test.owl>"+ "where "+ "{"+ "?book test:hasName ?hasName."+ "}"; gh.setQuery(queryStr); ResultSet results=gh.runQuery(); while (results.hasNext()) { QuerySolution soln = results.nextSolution();//查询结果中的每一条(称之为满足条件的一个solution) out.print(soln.get("book").toString()); out.print("\t"); out.print(soln.get("hasName").toString()); out.print("\t"); } %> 大家看下错误在哪里?谢谢
|