java-Spring属性编辑器仅适用于表单吗?

我正在开发一个Spring应用程序,该应用程序只是在一组数据中搜索符合某些条件的事物.有两个主要视图:一个是简单的表单,可让用户配置搜索条件,而另一个则将结果显示为表格.

搜索字段之一是一组封闭的选项(大约10个).在代码的下面,我想将此作为枚举类进行处理. Web表单包括一个下拉菜单,允许用户从此集合中选择一个选项.我使用了form:select来执行此操作,并填充了一组描述值的字符串.

为了使表示和业务逻辑分开,枚举类对这些字符串不了解,因此我创建了一个属性编辑器以在两者之间进行转换.当我加载表单时,将选择控件设置为与我给它的枚举值关联的字符串.提交表单时,字符串将转换回我的枚举类型.一切正常.

对于结果页面(不是表单),我只是将要显示的数据添加到ModelMap中.目前,我必须先将枚举类型转换为字符串,然后才能将其添加到地图中.我想做的就是将枚举添加到地图中,并让属性编辑器在后台为我转换它,就像在表单中一样.我不知道如何.是否有可能做到这一点?也许我缺少真正明显的东西?

最佳答案
您可以使用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>

关于值属性

The value can either be a plain value to transform (a hard-coded String value in a JSP or a JSP expression),or a JSP EL expression to be evaluated (transforming the result of the expression). Like all of Spring’s JSP tags,this tag is capable of parsing EL expressions itself,on any JSP version.

其API

Provides transformation of variables to String,using an appropriate custom PropertyEditor from BindTag (can only be used inside 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时,这将很有用.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...