简体   繁体   English

Visualforce将静态资源URL传递给Apex

[英]Visualforce passing static resource URL to Apex

I am trying to pass a static resource URL, via the apex:param tag. 我试图通过apex:param标签传递静态资源URL。 The code I have so far is: 到目前为止,我的代码是:

VisualForce: VisualForce:

<apex:selectList value="{!SelectedFamily}" onchange="renderGallery();" size="1" label="Product Family">
    <apex:actionFunction name="renderGallery" rerender="gallery" oncomplete="renderScripts();">
        <apex:param value="{!URLFOR($Resource.NoImage)}" assignTo="{!noImage}"/>
        <apex:param value="{!URLFOR($Resource.NoImageThumb)}" assignTo="{!noImageThumb}"/>
    </apex:actionFunction>
    <apex:actionFunction name="renderScripts" rerender="scriptPanel"> 
    </apex:actionFunction>
    <apex:selectOptions value="{!Family}" />
</apex:selectList>

Controller: 控制器:

public string noImage{ get; set; };
public string noImageThumb { get; set; }

My understanding of apex:param was that I would now be able to call the controller variables after the re-render had occurred and they would be populated with the static resource URL. 我对apex:param的理解是,现在可以在发生重新渲染后调用控制器变量,并且将使用静态资源URL填充它们。 But unfortunately I keep getting null. 但不幸的是,我一直在虚无。

Anyone have any idea why it isn't working? 有人知道为什么它不起作用吗?

It works if you provide names for your params --- Visualforce won't process params in this context unless they're named. 如果您为参数提供名称,它将起作用-Visualforce在这种情况下不会处理参数,除非它们被命名。

<apex:actionFunction name="renderGallery" rerender="gallery" oncomplete="renderScripts();">
       <apex:param name="noImg" value="{!URLFOR($Resource.NoImage)}" assignTo="{!noImage}"/>
       <apex:param name="noImgUrl" value="{!URLFOR($Resource.NoImageThumb)}" assignTo="{!noImageThumb}"/>
</apex:actionFunction>

If this doesn't work for you right away, please post the rest of your Visualforce code so that we can see where "gallery" and "scriptPanel" are in relation to your apex form tag --- getting rerender to work correctly is notoriously tricky, and entirely dependent on the hierarchical position of the DOM elements getting rerendered. 如果这对您立即不起作用,请发布其余的Visualforce代码,以便我们可以看到“ gallery”和“ scriptPanel”与您的顶点表单标签有关的地方---众所周知,重新渲染才能正常工作棘手,并且完全取决于要重新呈现的DOM元素的层次结构位置。 To guarantee that gallery and scriptPanel successfully rerender, put them in separate outputPanels outside the apex form tag, like this: 为了确保Gallery和scriptPanel成功地重新呈现,请将它们放在apex form标记之外的单独的outputPanels中,如下所示:

<apex:outputPanel id="scriptPanel">
     Selected Family: {!SelectedFamily}<br/>
</apex:outputPanel><br/><br/>


<apex:outputPanel id="gallery">
     No Image: {!noImage}<br/>
     No Image Thumb: {!noImageThumb}<br/>
</apex:outputPanel><br/><br/>

@MatthewKeefe, there's absolutely no reason why {!URLFOR()} can't be used as an extension variable --- it compiles to text (eg '/resource/123718923'), so Jim's solution here is actually pretty interesting, as it saves him from having to do a SOQL query on the StaticResource object in his controller. @MatthewKeefe,绝对没有理由不能将{!URLFOR()}用作扩展变量---它可以编译为文本(例如'/ resource / 123718923'),因此Jim在这里的解决方案实际上非常有趣,因为它使他不必在其控制器中的StaticResource对象上执行SOQL查询。

{!URLFOR($Resource.NoImage)} is not meant to be used as a controller/extension variable. {!URLFOR($Resource.NoImage)}不能用作控制器/扩展变量。 It is a direct reference to the static resource (no controller/extension required). 它是对静态资源的直接引用(无需控制器/扩展名)。

I would recommend you use output panels with a rendered property bound to an Apex variable that is controlled by the action function. 我建议您使用输出面板,该面板的渲染属性绑定到由动作功能控制的Apex变量。 That way you could show or hide each version of the image by showing or hiding the output panel. 这样,您可以通过显示或隐藏输出面板来显示或隐藏图像的每个版本。

Alternatively, you may want to look into using JavaScript Remoting for something like this. 另外,您可能需要考虑使用JavaScript远程处理来实现类似的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM