简体   繁体   English

尝试在JavaScript中的Java变量上使用.get方法

[英]Attempting to use .get method on java variable in javascript

I'm trying to create a webpage (using the play framework) which displays the user's name, date of birth etc. Where I am getting stuck is attempting to display a list of unknown size in an html page. 我正在尝试创建一个网页(使用播放框架),该网页显示用户的姓名,出生日期等。我遇到的问题是试图在html页面中显示未知大小的列表。 I currently have: 我目前有:

@(currentUser : Profile)

@main("Profile") {
    <div>
        <h1><b>@currentUser.getFirstName() @currentUser.getLastName()</b></h1>
        <p>@currentUser.getDateofBirth</p>
        <b>Nationalities</b>
        <ul><p id="natInput"></p></ul>
        <script>
            for (var i = 0; i < "@currentUser.getNationalities().size()"; i++) {
                document.getElementById("natInput").innerHTML += "<li>" + "@currentUser.getNationalities().get(i)" + "</li>";
            }
        </script>
    </div>
}

Profile is a basic java class which only contains getters and setters, and .getNationalities returns a List. Profile是一个基本的Java类,仅包含getter和setter,.getNationalities返回一个List。 @Main is another html file which stores the base design of the website, not really relevant to this. @Main是另一个html文件,用于存储网站的基本设计,与此无关。

As you can see, I'm trying to use the .get method to loop through the list of nationalities for the user, but unfortunately the "i" variable used in the get is not recognised, as it is a javascript variable in html. 如您所见,我正在尝试使用.get方法为用户浏览国籍列表,但是不幸的是,get中使用的“ i”变量无法识别,因为它是html中的javascript变量。 Every other part of this code appears to work as expected, and the .get works when the "i" is replaced with an integer, such as 0 or 1. 该代码的所有其他部分似乎均按预期方式工作,并且将“ i”替换为整数(例如0或1)时,.get可以正常工作。

Any ideas how I could get each individual element of this list? 有什么想法可以让我获得此列表的每个元素吗? Any help would be greatly appreciated 任何帮助将不胜感激

The problem is that you are messing up different step of the page generation. 问题是您弄乱了页面生成的不同步骤。

First of all we should clear out the processing steps from a render command in a Play controller to a fully rendered page in the browser. 首先,我们应该清除从播放控制器中的render命令到浏览器中已完全渲染的页面的处理步骤。

Twirl is a template engine that allow you to generate HTML pages with Scala. Twirl是一个模板引擎,可让您使用Scala生成HTML页面。 This step is performed on server side, before the HTML page is sent to the browser. 在将HTML页面发送到浏览器之前,此步骤在服务器端执行。 In this step the Javascript code is not being considered. 在此步骤中,不考虑Javascript代码。

So the first step after the render command, the twirl template is picked and rendered, evaluating each Twirl/Scala instructions. 因此,在render命令之后的第一步,选择并渲染twirl模板,评估每个Twirl / Scala指令。 This will generate an HTML file with no more Scala code. 这将生成没有更多Scala代码的HTML文件。

This generated page is sent to the browser. 生成的页面被发送到浏览器。 Then the page is loaded into the browser and the Javascript code is evaluated as any other HTML page with JS. 然后将该页面加载到浏览器中,并且将Java代码与使用JS的任何其他HTML页面一起评估。


This would explain why you are not able to render the page the way you are doing. 这可以解释为什么您无法按照您的方式呈现页面。

A proper way, if your page is static, is to use only twirl instructions to iterate over the array Twirl Template - Iterating 如果页面是静态的,一种正确的方法是仅使用twirl指令来迭代数组Twirl模板-迭代

You should be able to rewrite your list generation like the following if getNationalities() method returns a Scala List 如果getNationalities()方法返回Scala列表,则应该能够像下面那样重写列表生成

<ul>
@for(nationality <- currentUser.getNationalities()) {
   <li>@nationality</li>
}
</ul>

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

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