我正在开发我的第一个Lift项目,并且需要动态生成表单。更确切地说,我有一些操作由附加了参数列表的对象表示 - 其中一些是字符串,一些数字,一些布尔值,一些文件。我需要通过表单为其参数提供操作对象的具体值。目前我只是遍历参数列表并直接生成表单作为XML,但我知道这是非常糟糕的样式 - 我无法绑定输入值,我无法绑定动作提交,我无法应用验证。我无法绕过请求参数,但我想解决这个问题。不幸的是,我不得不知道如何做到这一点,所以任何帮助将不胜感激。 我一直找到的所有示例都预先提供了固定数量的输入参数。
这是一些相关的代码。我希望这是可以理解的(我真的很惭愧公开展示它:-))
def operationForm(): NodeSeq = {
val operationCode = S.param("operation").openOr("")
val operationVariant = S.param("operationVariant").openOr("")
if (operationCode != "" && !operationVariant.isEmpty) {
val operation = LighthouseDAOs.operationsRegistry.findByCode(operationCode)
val params: List[Parameter] = if (operationVariant == "default") {
operation.getParameters.toList
} else {
operation.getParameters.filter(p => p.getVariant == operationVariant).toList
}
<lift:surround with="closableBox" at="content">
<form id="viewOperation" post={"/Deployment/" + S.param("location") + "/" + S.param("deployment")} method="post">
{params.map(p => getInputElem(p))}
<input type="submit" style="width: 150px;" value="Execute operation"/>
<input type="hidden" name="executeOperation" value="true"/>
</form>
</lift:surround>
} else {
<span></span>
}
}
private def getOperationVariants(operation: Operation): Set[String] = {
operation.getParameters.map(_.getVariant).toSet
}
def operationVariants(deployment: Deployment): NodeSeq = {
val operationCode = S.param("operation").openOr("")
if (operationCode != "") {
val operation = LighthouseDAOs.operationsRegistry.findByCode(operationCode)
val variants = getOperationVariants(operation)
if (variants.size > 1) {
<lift:surround with="closableBox" at="content">
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<th style="width: 160px;">Operation
{operation.getLongName}
variants</th>
</tr>{variants.map(v => {
<tr>
<td>
<a href={Path.path + "Deployment/" + encode(deployment.getLocation) + "/" + encode(deployment.getDeployedComponent.getCode) + "?operation=" + encode(operation.getCode) + "&operationVariant=" + encode(v)}>
{v}
</a>
</td>
</tr>
})}
</table>
</lift:surround>
} else {
<span></span>
}
} else {
<span></span>
}
}
def getInputElem(param: Parameter): Node = {
if (param.getChoice != null) {
<div>
<label for={param.getName}>
{param.getName}
</label>
<select id={param.getName} name={param.getName}>
{param.getChoice.flatMap(c => <option value={c}>
{c}
</option>)}
</select>
</div>
} else {
val paramType = param.getType match {
case Parameter.PASSWORD => "password"
case Parameter.BOOLEAN => "checkbox"
case Parameter.CLOB => "file"
case _ => "text"
}
<div>
<label for={param.getName}>
{param.getName}
:</label> <input type={paramType} id={param.getName} name={param.getName} stype="width: 300px;">
{param.getDefaultValue}
</input>
</div>
}
}
def executeOperation(deployment: Deployment): Elem = {
val operationCode = S.param("operation").openOr("")
val operationVariant = S.param("operationVariant").openOr("")
if (S.param("executeOperation").openOr("false") == "true") {
val op = LighthouseDAOs.operationsRegistry.findByCode(operationCode)
val params = op.getParameters
LogLH3.info("Executing operation: " + op.getLongName)
val operationInstallation = new OperationInstallation();
operationInstallation.setInstallationLocation(deployment);
operationInstallation.setInstalledOperation(op);
val operationCall = new OperationCall(operationInstallation);
if (operationVariant != "" && operationVariant != "default")
operationCall.setVariant(operationVariant)
params.filter(p => p.getVariant == operationVariant).foreach(p => operationCall.addParameterValue(op.createParameterValue(p.getName, S.param(p.getName).openOr(""))))
try {
LighthouseDAOs.operationInstallationRegistry.execute(operationCall)
S.notice("Operation " + op.getLongName + " was executed successfully.")
} catch {
case e: Exception => S.error(e.getMessage)
}
}
<span></span>
}
只是为了记录我正在使用Lift 2.2&amp; Scala 2.8.1
答案 0 :(得分:3)
你可以改进这个例子,使它更具设计友好性,但这是基本的想法。你需要以某种形式包装它。
基本上你有一个模板,用使用CSS选择器绑定动态生成的东西替换它的内容。
在你的模板中:
<div class="lift:MyForm.render">
Form will go here
</div>
摘录:
class MyForm extends Snippet {
def render = {
val fields = List("a", "b", "c")
var params: Map[String, String] = ... //or whatever
def setf(key: String)(value: String) = params = params.updated(key, value)
def getf(key: String)() = params.get(key)
"*" #> fields map {
field =>
<p>
<label>{field}</label>{SHtml.text(getf(field) _, setf(field) _)}
</p>
}
}
}