你可以使用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 適當的自定義屬性編輯器(僅能在BindTag使用)
如果您使用的MultiActionController我的建議你使用虛擬命令類作爲波紋管
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>
它是有用的內部當你的目標的命令不提供屬性編輯這就需要在另一個命令中使用。
謝謝 - 這讓我走上了正軌:) – jrb 2010-06-22 13:39:27