Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 播放框架-插入用户定义的css文件_Html_Playframework - Fatal编程技术网

Html 播放框架-插入用户定义的css文件

Html 播放框架-插入用户定义的css文件,html,playframework,Html,Playframework,我尝试将用户定义的css文件插入到我的模板中 型号: 控制器 看法 但FireBug或谷歌Chrome开发者的面板并不像谷歌那样显示这种风格 更新: 我找到了解决办法,但不是那么完美。控制器中的代码: /** * Displays page, that contains user-defined css file * for specified instance of MyModel. */ public static void index(Long id) { final MyM

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

型号: 控制器 看法 但FireBug或谷歌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);
}
它将把所有样式放在一个字符串中
style
。然后我们可以在模板中渲染它:

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

${style}

可能有人可以建议更完美的解决方案吗?

您遇到的问题是,由于您使用的是renderBinary,因此内容类型返回为
应用程序/二进制文件,因此浏览器会忽略它,因为它不是
文本/css”
类型

使用renderBinary时,renderBinary代码根据使用的参数以一系列不同的方式设置contentType。使用文件时,它根据文件名确定内容类型

因此,如果您可以保证您的文件名是CSS类型,那么当它执行
MimeType.getContentType(文件名)时它将相应地设置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);
}
公共静态void显示样式表(整数id){
//…将css数据从文件读入字符串

String cssTextData=readCSSFromFile();//我在我的
main.html
中有
{get'styles'/}
。浏览器显示
,但无法加载此样式表,但当我尝试通过get请求显示样式表时,所有工作正常。我已进一步调查,可以复制您的错误,并找到修复方法…(请参阅编辑).好的。我根据你的建议做了一个小的重构。现在看起来好多了。谢谢。
http://localhost:9000/mycontroller/displaystylesheet?id=1188
/**
 * 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);
}
<style type="text/css">
  ${styles}
</style>
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);
}