·显示版本树
把svnkit.jar添加到项目中,用于实现svn功能。把jackson-all-1.6.2.jar添加到项目中,用于显示树列表。把javaee.ar添加到项目中。新建一个类(SVNUtil.class)实现svn功能
privateStringsvnRoot;
privateStringuserName;
privateStringpassword;
privateSVNRepositoryrepository;
/***
*构造方法
*@paramsvnRoot
*svn根目录
*/
publicSVNUtil(String svnRoot) {
this.svnRoot=svnRoot;
}
/***
*构造方法
*@paramsvnRoot
*svn根目录
*@paramuserName
*登录用户名
*@parampassword
*登录密码
*/
publicSVNUtil(String svnRoot, String userName, String password) {
this.svnRoot=svnRoot;
this.userName=userName;
this.password=password;
}
/***
*通过不同的协议初始化版本库
*/
privatestaticvoidsetupLibrary() {
//对于使用http://和https://
DAVRepositoryFactory.setup();
//对于使用svn:/ /和svn+xxx:/ /
SVNRepositoryFactoryImpl.setup();
//对于使用file://
FSRepositoryFactory.setup();
}
每次连接库都进行登陆验证
/***
*登录验证
*@return
*/
publicbooleanlogin(){
setupLibrary();
try{
//创建库连接
repository=SVNRepositoryFactory.create(SVNURL.parseURIEncoded(this.svnRoot));
//身份验证
ISVNAuthenticationManager authManager = SVNWCUtil
.createDefaultAuthenticationManager(this.userName,
this.password);
//创建身份验证管理器
repository.setAuthenticationManager(authManager);
returntrue;
}catch(SVNException svne){
svne.printStackTrace();
returnfalse;
}
}
下面的方法实现查询给定路径下的条目列表功能
/***
*
*@parampath
*@return查询给定路径下的条目列表
*@throwsSVNException
*/
@SuppressWarnings("rawtypes")
publicList<SVN> listEntries(String path)
throwsSVNException {
Collection entries =repository.getDir(path, -1,null,
(Collection)null);
Iterator iterator = entries.iterator();
List<SVN> svns =newArrayList<SVN>();
while(iterator.hasNext()) {
SVNDirEntry entry = (SVNDirEntry) iterator.next();
SVN svn =newSVN();
svn.setCommitMessage(entry.getCommitMessage());
svn.setDate(entry.getDate());
svn.setKind(entry.getKind().toString());
svn.setName(entry.getName());
svn.setRepositoryRoot(entry.getRepositoryRoot().toString());
svn.setRevision(entry.getRevision());
svn.setSize(entry.getSize()/1024);
svn.setUrl(path.equals("") ?"/"+entry.getName() : path +"/"+entry.getName());
svn.setAuthor(entry.getAuthor());
svn.setState(svn.getKind() =="file"?null:"closed");
svns.add(svn);
}
新建一个SVNServlet
添加一个方法用于把java对象转换为json字符串
/**
*将java对象转换为json字符串
*
*@paramobj
*:可以为map,list,javaBean等
*@returnjson字符串
*@createTime2010-11-23下午07:42:58
*/
publicstaticString object2Json(Object obj) {
try{
StringWriter sw =newStringWriter();
JsonGenerator gen =newJsonFactory().createJsonGenerator(sw);
mapper.writeValue(gen, obj);
gen.close();
returnsw.toString();
}catch(Exception e) {
e.printStackTrace();
}
returnnull;
}
protectedvoiddoGet(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
//TODOAuto-generated method stub
this.doPost(request, response);
}
protectedvoiddoPost(HttpServletRequest request,
HttpServletResponse response)throwsServletException, IOException {
//TODOAuto-generated method stub
Object svns =null;
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String path = request.getParameter("pid");
String url ="svn://localhost/svndemo1";
String usrName ="usrName";
String password ="password";
if(path ==null) {
path ="";
}
path =newString(path.getBytes("ISO-8859-1"),"UTF-8");
String type = request.getParameter("type");
PrintWriter out = response.getWriter();
try{
SVNUtil svnUtil =newSVNUtil(url, usrName, password);
if(svnUtil.login()) {
/*用于查询历史记录
if("history".equals(type)) {
List<SVN> svnl = svnUtil.getHistory(path);
HashMap<String, Object> sv =newHashMap<String, Object>();
sv.put("total", svnl.size());
sv.put("rows", svnl);
svns = sv;
}else{*/
svns = svnUtil.listEntries(path);
//}
//把java对象转换成json字符串
String json = SVNServlet.object2Json(svns);
out.print(json);
}else{
System.out.println("验证失败");
}
}catch(SVNException ex) {
ex.printStackTrace();
}
out.flush();
out.close();
}
新建一个index.jsp用户显示版本数列表,页面显示我使用了jquery-easyui模板
<%@pagelanguage="java"contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() +"://"
+ request.getServerName() +":"+ request.getServerPort()
+ path +"/";
%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<basehref="<%=basePath%>">
<title>SVN</title>
<metahttp-equiv="Content-Type"content="text/html; charset=utf-8">
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is my page">
<linkrel="stylesheet"type="text/css"
href="plugs/themes/default/easyui.css">
<linkrel="stylesheet"type="text/css"href="plugs/themes/icon.css">
<scripttype="text/javascript"src="plugs/jquery-1.4.2.min.js"></script>
<scripttype="text/javascript"src="plugs/jquery.easyui.min.js"></script>
<scripttype="text/javascript">
$(function() {
$('#test').treegrid({
title :'SVN列表',
nowrap :false,
rownumbers :true,
collapsible :false,
url :'svn?pid=',
idField :'url',
treeField :'url',
frozenColumns : [ [ {
title :'路径',
field :'url',
width : 350,
formatter :function(value) {
return'<span style="color:red">'+ decodeURI(value.substr(value.lastIndexOf("/"))) +'</span>';
}
} ] ],
columns : [ [ {
field :'name',
title :'名称',
width : 120
}, {
field :'size',
title :'文件大小(KB)',
width : 80,
rowspan : 2
}, {
field :'revision',
title :'版本号',
width : 80,
rowspan : 2
}, {
field :'author',
title :'作者',
width : 100,
rowspan : 2
}, {
field :'date',
title :'修改日期',
width : 130,
rowspan : 2
}, {
field :'commitMessage',
title :'注释',
width : 150,
rowspan : 2
}, {
field :'kind',
title :'操作',
width : 120,
align :'center',
rowspan : 2,
formatter :function(value) {
returnvalue=='file'?'<a onclick="download()" style="cursor: pointer;color:red">下载</a><a onclick="viewHistory()" style="margin-left:5px; cursor: pointer;color:red">历史版本</a>':'';
}
}] ],
onBeforeExpand :function(row, param) {
$(this).treegrid('options').url ='svn?pid='+encodeURI(decodeURI(row.url));
}
});
});
functiondownload(){
setTimeout(function(){
varnode = $('#test').treegrid('getSelected');
if(node !=null)
window.open("download?url="+encodeURI(decodeURI(node.url))+"&size="+node.size+"&name="+encodeURI(decodeURI(node.name))+"&revision="+node.revision);
},200);
}
functionviewHistory(){
setTimeout(function(){
varnode = $('#test').treegrid('getSelected');
if(node !=null) {
window.open("history.jsp?uri="+encodeURI(decodeURI(node.url)),"_blank","height=400,width=700,status=yes,toolbar=no,menubar=no,location=no");
}
}, 200);
}
</script>
</head>
<body>
<tableid="test"></table>
</body>
</html>
显示效果如下
版本树列表
历史版本