【问题标题】:Do Spring property editors only work on forms?Spring 属性编辑器是否仅适用于表单?
【发布时间】:2010-06-17 18:02:20
【问题描述】:

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

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

为了保持表示和业务逻辑分开,枚举类必须不知道这些字符串,所以我创建了一个属性编辑器来在两者之间进行转换。当我加载表单时,选择控件设置为与我给它的枚举值关联的字符串;提交表单时,字符串将转换回我的枚举类型。这一切都很好。

对于结果页面(不是表单),我只是将要显示的数据添加到 ModelMap 中。目前,我必须在将枚举类型添加到地图之前将其显式转换为字符串。我想做的只是将枚举添加到地图并让属性编辑器在后台为我转换它,就像它为表单所做的那样。我不知道怎么做。是否有可能做到这一点?也许我错过了一些非常明显的东西?

【问题讨论】:

    标签: java spring


    【解决方案1】:

    你可以使用 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 时很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-28
      • 2010-10-29
      • 1970-01-01
      • 2017-01-13
      • 2011-05-22
      • 2019-09-03
      相关资源
      最近更新 更多