【问题标题】:How to manage html inside application which uses spray and scala如何在使用spray和scala的应用程序中管理html
【发布时间】:2015-01-19 06:18:18
【问题描述】:

我正在开发一个使用 Spray 和 Scala 的简单应用程序,我是这种环境的初学者。我正在研究一种用于通过 post 方法发送信息的表单。表格如下

 lazy val formPage =
  <html>
  <body>
  <form id="register">
  <fieldset> 
  <legend>Dodaj sędziego</legend> 
  <div> 
  <label>Imię
  <input id="first-name" name="first-name" placeholder="Imię" required autofocus> 
  </label>
  </div>
  <div> 
  <label>Nazwisko
  <input id="last-name" name="last-name" placeholder="Nazwisko" required autofocus> 
  </label>
  </div>
  <div> 
  <label>Numer identyfikacyjny
  <input id="number" name="number" required>
  </label>
  </div> 
  </fieldset>
  </form>
  </body>
  </html>

我称之为

  path("judge" / "new"){
    respondWithMediaType(`text/html`) {
      complete {
        formPage
      }
    }
  }

而且它在编译过程中会导致很多问题,例如:

[error] /project/src/main/scala/pl/vectorotic/MyService.scala:45: in XML literal: '=' expected instead of 'a'
[error]   <input id="first-name" name="first-name" placeholder="Imię" required autofocus>

想请教如何在scala程序中使用html。

【问题讨论】:

  • 我认为问题在于您的 requiredautofocus 属性不是有效的 XML/XHtml 语法。请改用autofocus="autofocus"。例如。见w3schools.com/tags/att_input_autofocus.asp
  • 它不起作用,但我很好奇为什么。

标签: html scala spray


【解决方案1】:

您的 formPage 不是字符串,而是 XML Elem 。使用三引号:

  lazy val formPage =
    """<html>
      <body>
        <form id="register">
          <fieldset>
            <legend>Dodaj sędziego</legend>
            <div>
              <label>Imię
                <input id="first-name" name="first-name" placeholder="Imię" required autofocus>
                </label>
              </div>
              <div>
                <label>Nazwisko
                  <input id="last-name" name="last-name" placeholder="Nazwisko" required autofocus>
                  </label>
                </div>
                <div>
                  <label>Numer identyfikacyjny
                    <input id="number" name="number" required>
                    </label>
                  </div>
                </fieldset>
              </form>
            </body>
          </html>"""

【讨论】:

  • 为什么在这里lazy val?是否值得在每种情况下使用?
  • 它就在那里,因为它也包含在原始问题中,但在这种情况下你不需要它。看看this SO question,它阐明了lazy 的实际作用。
猜你喜欢
  • 2017-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多