如何在Lift中动态生成输入表单
|
我正在做我的第一个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和Scala 2.8.1
没有找到相关结果
已邀请:
1 个回复
埃庐
片段: