动态更改OC vfs文件,并发布到online

本文介绍了如何在/sites/default/test.jsp文件中写入hello深蓝涩文本,并将其发布到线上环境。通过使用OpenCms框架,实现了在不同环境下运行且不出现资源创建错误异常。

程序运行后,会在/sites/default/test.jsp文件中写入 "hello 深蓝涩" 文本,并发布到线上。在线上线下都可以运行,不会报资源创建错误异常。

 


<%@ page language="java" import="java.util.*,org.opencms.*,org.opencms.xml.types.*,java.util.UUID,org.opencms.main.*,org.opencms.*,org.opencms.xml.types.*,org.opencms.file.*"
 pageEncoding="ISO-8859-1"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 

  <title>My JSP 'add.jsp' starting page</title>

  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

 </head>

 <body>
  <%
  try {
   
          String resname="/sites/default/test.jsp";
                       
   //获取CmsObject 实例
    org.opencms.jsp.CmsJspActionElement cms = new org.opencms.jsp.CmsJspActionElement(pageContext, request, response);
   org.opencms.file.CmsObject object = OpenCms.initCmsObject("Guest");
      /* 登录到工作区*/
   object.loginUser("Admin", "admin");
      /* 获取上下文对象*/
   org.opencms.file.CmsRequestContext cmsContext =object.getRequestContext();
   /* 获取当前项目*/
   org.opencms.file.CmsProject curProject = cmsContext.getCurrentProject();   
   /*
    返回true如果该项目的在线项目
   */
   if(curProject.isOnlineProject()){
    /* 获取线下项目*/
    org.opencms.file.CmsProject offlineProject = object.readProject("Offline");
    /*设置当前项目的用户。*/
    cmsContext.setCurrentProject(offlineProject);
   }

    org.opencms.file.CmsResource res = object.readResource(resname);
   
          org.opencms.file.CmsFile file = object.readFile(res);
   file.setContents("hello 深蓝涩!!!".getBytes());
         //写入文件
   object.writeFile(file);
   //发布资源
    OpenCms.getPublishManager().publishResource(object,resname);
   
   } catch (Exception e) { 
                          e.printStackTrace(); 
                          out.println(e); 
        } 
     %>
 </body>
</html>


 

 

 

原文地址: yuxingliang.iteye.com/blog/1867470         转载请注明出处

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值