【发布时间】:2011-04-16 21:12:51
【问题描述】:
我正在处理我的第一个 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
【问题讨论】:
-
您应该更具体地了解您目前的工作方式。也许向我们展示一个简化的代码 sn-p 以便我们改进它。
-
完成。我认为我的解释足够通用,但代码示例从未受到伤害(即使像这个一样丑陋)。基本上 operationForm() 逐个元素生成表单元素, executeOperation() 处理提交的参数。问题很明显 - 我无法验证表单输入,也无法检索作为多部分请求的一部分发布的文件。但是,我根本不知道更好。 Lift 似乎是一个很棒的框架,但我认为它确实需要更多的文档。