你可以使用 Spring Tablib
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
并使用变换标记
<!--If you have a command which command name is account-->
<!--Default command name used in Spring is called command-->
<spring:bind path="account.balance">${status.value}</spring:bind>
或者
<spring:bind path="account.balance">
<spring:transform value="${account.balance}"/>
</spring:bind>
或者
<!--Suppose account.balance is a BigDecimal which has a PropertyEditor registered-->
<spring:bind path="account.balance">
<spring:transform value="${otherCommand.anotherBigDecimalProperty}"/>
</spring:bind>
关于值属性
值可以是要转换的普通值(JSP 或 JSP 表达式中的硬编码字符串值),或要评估的 JSP EL 表达式(转换表达式的结果)。与 Spring 的所有 JSP 标记一样,该标记本身能够在任何 JSP 版本上解析 EL 表达式。
它的 API
使用 BindTag 中的适当自定义 PropertyEditor 将变量转换为字符串 (只能在 BindTag 内部使用)
如果您使用 MultiActionController,我建议您使用 Dummy Command 类,如下所示
public class Command {
public BigDecimal bigDecimal;
public Date date;
/**
* Other kind of property which needs a PropertyEditor
*/
// getter's and setter's
}
在您的 MultiActionController 中
public class AccountController extends MultiActionController {
@Autowired
private Repository<Account, Integer> accountRepository;
public AccountController() {
/**
* You can externalize this WebBindingInitializer if you want
*
* Here it goes as a anonymous inner class
*/
setWebBindingInitializer(new WebBindingInitializer() {
public void initBinder(WebDataBinder dataBinder, WebRequest request) {
binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, numberFormat, true));
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy"), true));
}
});
}
public ModelAndView(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView()
.addAllObjects(
createBinder(request, new Command())
.getBindingResult().getModel())
.addObject(accountRepository.findById(Integer.valueOf(request.getParameter("accountId"))));
}
}
在你的 JSP 中
<c:if test="{not empty account}">
<!--If you need a BigDecimal PropertyEditor-->
<spring:bind path="command.bigDecimal">
<spring:transform value="${account.balance}"/>
</spring:bind>
<!--If you need a Date PropertyEditor-->
<spring:bind path="command.date">
<spring:transform value="${account.joinedDate}"/>
</spring:bind>
</c:if>
当您的 Target 命令不提供需要在另一个命令中使用的 PropertyEditor 时很有用。