【问题标题】:eclipse is showing error on render() in play2eclipse 在 play2 中的 render() 上显示错误
【发布时间】:2014-07-19 20:01:49
【问题描述】:

我是 Play Framework 的新手,我正在尝试开发示例 java 应用程序

我创建了新的 html 页面 test.html

在控制器中,如果我们简单地以字符串的形式返回结果,即返回 ok("hello world"),它就可以工作,但它只是格式化所有样式/文本并在 UI 上显示“hello world”。

 public static Result test() {
        return ok("hello world");
        }

// 工作正常

但是当我尝试这个时它会出错

public static Result test(){
        return ok(test.render());
        }

//给出错误

它给出以下错误

[错误] /opt/ahsen/play-2.2.3/testapp/app/controllers/Application.java:15:views.html 中的渲染(java.lang.String,play.api.templates.Html)。测试不能应用于 ()

[错误] 返回 ok(test.render());

[error] (compile:compile) javac 返回非零退出代码

这是我的 test.html 文件

    @(title: String)(content: Html)

    <!DOCTYPE html>

    <html>
    <head>
    <title>@title</title>
    <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
    <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
    <script src="@routes.Assets.at("javascripts/jquery-1.9.0.min.js")" type="text/javascript"> 
    </script>
    </head>
    <body>
        @content
    </body>
</html>

请帮忙

【问题讨论】:

    标签: java eclipse playframework-2.0


    【解决方案1】:

    您的 test.html 模板需要两个参数

    @(title: String)(content: Html)
    

    因此您需要将它们传递给控制器​​中的模板

    public static Result test(){ 
      String title = "test title";
      Html content = // create content
      return ok(test.render(title, content));
    }
    

    更好的解决方案

    Test.html 看起来更像是一个更通用的布局模板。我会创建一个单独的文件来生成内容,而不是直接从控制器传递它。

    内容.html

    @(title: String)
    
    @test(title){
      <span>this is the content</span>
    }
    

    在控制器中它看起来像这样。

    public static Result test(){ 
      String title = "test title";
      return ok(content.render(title));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-18
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多