【问题标题】:Play framework - insert user-defined css file播放框架 - 插入用户定义的 css 文件
【发布时间】:2011-12-29 13:10:56
【问题描述】:

我尝试将用户定义的 css 文件插入到我的模板中。

型号:

@Entity
public class MyModel extends Model {

    // Some code stuff...

    /** User-defined css file. */
    public Blob stylesheet;

}

控制器

public class MyController extends Controller {

    // Some code stuff...

    /**
     * Displays user-defined css file for specified instance of MyModel.
     */
    public static void displayStylesheet(Long id) {
        final MyModel myModel = MyModel.findById(id);
        notFoundIfNull(myModel);
        response.setContentTypeIfNotSet(myModel.stylesheet.type());
        if (myModel.stylesheet.exists()) {
            renderBinary(myModel.stylesheet.getFile());
        }
    }

    /**
     * Displays page, that contains user-defined css file
     * for specified instance of MyModel.
     */
    public static void index(Long id) {
        render(id);
    }

}

查看

#{extends 'main.html' /}
#{set 'styles'}
  <link rel="stylesheet" href="@{MyController.displayStylesheet(id)}" />
#{/set}
<p>This page contains user-defined stylesheet.</p>

当我尝试通过 GET 请求显示样式表时一切正常:

http://localhost:9000/mycontroller/displaystylesheet?id=1188

但 FireBug 或 Google Chrome 开发者的面板并没有像来源那样显示这种风格。

更新:

我找到了解决方案,但它并不那么美观。控制器中的代码:

/**
 * Displays page, that contains user-defined css file
 * for specified instance of MyModel.
 */
public static void index(Long id) {
    final MyModel myModel = MyModel.findById(id);
    String styles = "";
    File stylesheet = myModel.stylesheet.getFile();
    try {
        FileInputStream stream = new FileInputStream(stylesheet);
        try {
          FileChannel fc = stream.getChannel();
          MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
          styles = Charset.defaultCharset().decode(bb).toString();
        }
        finally {
          stream.close();
        }
    }
    catch (IOException ioe){
        throw new RuntimeException(ioe);
    }
    render(id, styles);
}

它将所有样式放入一个字符串styles。然后我们可以在模板中渲染它:

<style type="text/css">
  ${styles}
</style>

可能有人可以提出更美丽的解决方案?

【问题讨论】:

    标签: html playframework


    【解决方案1】:

    您遇到的问题是内容类型返回为application/binary,因为您使用的是renderBinary,所以浏览器忽略它,因为它不是"text/css" 类型。

    使用renderBinary 时,renderBinary 代码根据所使用的参数以多种不同方式设置contentType。当您使用 File 时,它​​会根据文件名确定内容类型。

    所以,如果你能保证你的文件名是 CSS 类型,那么当它执行MimeType.getContentType(filename); 时,它会相应地设置 contentType。

    否则,您可以将代码更改为类似的内容

    public static void displayStylesheet(Integer id) {
    
        // ... read css data from file into a String
        String cssTextData = readCSSFromFile(); // <-- You can use the method you have used in your update here
    
        response.contentType = "text/css";
        renderText(cssTextData);
    }
    

    【讨论】:

    • 我的main.html 中有#{get 'styles' /}。浏览器显示 &lt;link rel="stylesheet" href="/mycontroller/displaystylesheet?id=1188" /&gt; 但无法加载此样式表,但是当我尝试通过 GET 请求显示样式表时一切正常。
    • 我已经进一步调查并且可以复制您的错误,并且找到了修复...(见编辑)。
    • 好的。我根据您的建议做了一个小重构。现在看起来好多了。谢谢。
    猜你喜欢
    • 2014-12-06
    • 2015-04-09
    • 1970-01-01
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多