java - Spring 属性编辑器只能处理表单吗?

标签 java spring

我正在开发一个 Spring 应用程序,它只在一组数据中搜索符合某些条件的内容。主要有两种 View :一种是允许用户配置搜索条件的简单表单,另一种是将结果显示为表格。

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

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

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

最佳答案

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

关于java - Spring 属性编辑器只能处理表单吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3064276/

相关文章:

java - 尝试连接 Lettuce 连接工厂时出错

java - 显示 ArrayList 中的文本

java - 打字时是否可以在文本字段中附加文本?

java - JPA @Column 注释 getter 不起作用

java - 在 spring mvc 应用程序中设置日期

c# - 是否可以在 C++ 中序列化和反序列化对象?

java - Protocol Buffer : How to import?

java - 微服务未从 : http://localhost:8888 Springboot 处的服务器获取配置

javascript - 管理来自 java spring 方法的 http.get 响应

java - JPA Criteria Query with IN operator for Set<?> with Spring DATA-JPA