【问题标题】:Manipulating form data server-side with JSoup on ColdFusion在 ColdFusion 上使用 JSoup 在服务器端操作表单数据
【发布时间】:2015-02-17 04:01:06
【问题描述】:

继我之前的问题 (How to replace all anchor tags with a different anchor using regex in ColdFusion) 之后,我想使用 JSoup 来处理来自 FormArgument 的内容,然后将处理后的内容插入数据库。

这是从表单发送到服务器的示例:

<form>
   <div id="Description" contenteditable="true">
   <p>
      Terminator Genisys is an upcoming 2015 American 
      science fiction action film directed by Alan Taylor. 

      <img id="Img0" src="http://www.moviepics.com/terminator1.jpg" />
      <img id="Img1" src="http://www.moviepics.com/terminator2.jpg" />
      <img id="Img2" src="http://www.moviepics.com/terminator2.jpg" />

      You can find out more by <a href="http://www.imdb.com">clicking here</a>
   </p>
   </div>
</form>

这是我的 CFC 目前将如何处理它(基本想法):

<cfquery>
INSERT INTO MyTable (Value1, Description)
VALUES
(
   <cfif structkeyexists(ARGUMENTS.Value1)>
      <cfqueryparam value="#ARGUMENTS.Value1#" cf_sql_type="nvarchar" />
   <cfelse>
      NULL
   </cfif>

   ,
   <!--- 
    Before the below happens, I need to replace the src 
    attributes of the img tags of Arguments.Description 
   --->
   <cfif structkeyexists(ARGUMENTS.Description)>
       <cfqueryparam value="#ARGUMENTS.Description#" cf_sql_type="nvarchar" />
   <cfelse>
       NULL
   </cfif>
)
</cfquery>

我知道&lt;div&gt; 不是表单元素,但不用担心它仍然提交给 CF11,就好像它是使用 JQuery serialize() 技巧的表单元素一样。

CF11 处理这个表单时,它会获取ARGUMENTS.Description 中的数据。我要做的是解析这个参数的内容,找到&lt;img&gt;标签,提取出src属性。

然后我将进行更多处理,但最终我需要将每个img 标记中的src 值替换为CF11 在服务器端创建的不同值。只有这样我才能将表单值插入到数据库中。

JSoup 可以协助完成此类任务吗?感觉就像一个简单的查找和替换任务,但我不知道如何去做。

【问题讨论】:

  • jSoup 非常适合这样的事情。您可以使用 RegEx,但可能会遇到流程失败的边缘情况。

标签: coldfusion jsoup coldfusion-11


【解决方案1】:

首先,您的标记中有一个错误,图像标签的 src 属性没有右引号。确保在尝试使用它之前修复它

<cfsavecontent variable="samform">
    <form>
    <div id="Description" contenteditable="true">
    <p>Terminator Genisys is an upcoming 2015 American science fiction action film directed by Alan Taylor. 

    <img id="Img0" src="http://www.moviepics.com/terminator1.jpg" />
    <img id="Img1" src="http://www.moviepics.com/terminator2.jpg" />
    <img id="Img2" src="http://www.moviepics.com/terminator2.jpg" />

    You can find out more by <a href="http://www.imdb.com">clicking here</a></p>
    </div>
    </form>
</cfsavecontent>

<cfscript>
jsoup = CreateObject("java", "org.jsoup.Jsoup");
alterform = jsoup.parse(samform);

imgs = alterform.select("##Description img");


for (img in imgs) {
    img.attr("src", "betterthan#listlast(img.attr("src"),"/")#");
}

imgs[2].attr("src", "TheyShouldHaveStoppedAtT2.gif");

writeOutput('<textarea rows="10" cols="100">#samform#</textarea><br>');
writeOutput('<textarea rows="10" cols="100">#alterform#</textarea>');
</cfscript>

如果您熟悉 css 选择器或 jquery 选择器,jSoup 选择几乎是第二天性。

它的作用是遍历#Description 中的每个img# 必须加倍,因为 CF)。然后它将 url 更改为基于当前 url 的东西,然后只是为了演示,我用其他东西覆盖了第二个 img 的 src 并在 textareas 中输出之前/之后。

【讨论】:

  • 我实际上需要更改 Argument 本身的内容,以便它现在包含不同的 src 属性。我想让他们将其插入数据库。那可能吗?我编辑了我的问题以提供更多代码/更好地理解我的意思
  • @volumeone 是的,你可以说img[1].attr("src",arguments.value1) 等,循环不是必需的,我只是向你展示了不同的技术。
  • 我可以只解析#ARGUMENTS.Description# 而不是整个表单吗?
  • @volumeone 您可以解析包含有效标记的任何变量。我只使用了整个表单,因为您说您是通过“JS Trickery”检索它,所以我只是从您的帖子中获取示例。只要您的 html 语法正确,就不需要对代码进行任何更改(因为它在您在此处发布的内容中缺少引号,但这可能已被隔离到此处。
  • 我非常感谢你给我看这个。它工作得很好!它也很快。它真的帮了我很多。
猜你喜欢
  • 2020-07-17
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多