English 中文(简体)
Spring MVC: Bind from form:option to enum
原标题:Spring MVC: Bind from form:option to enum
  • 时间:2012-01-11 16:06:38
  •  标签:
  • spring-mvc

我期待着从我以我的形式挑选的盒子到某个特定圈子。

考虑这一点:

public enum OperatorDTO {
LESS_THAN ("<"),
GREATER_THAN (">"),
EQUALS ("="),
NOT_EQUALS("!=");

private String operator;

public String getOperator() {
    return operator;
}

private OperatorDTO(String operator)
{
    this.operator = operator;
}

以及我的表格中的本信:

<form:select path="rules[${counter.index}].operator">
    <form:options itemLabel="operator" itemValue="operator" />
</form:select>

该网页在倒塌盒中显示各种“和”、“和”符号。

However, when I submit my form I get errors when it attempts to bind the values back to the enums e.g. "No enum const class com.fmrco.insight.adminconsole.dto.enums.OperatorDTO.<"

履行这一约束力是否容易?

增 编

问题回答

Try to omit itemValue=“operator”

项目价值应当是固定名称,只要我记得它是一种违约行为。

表格标签是正确的,也是正确的。 此处遗漏的是转换器,该转换器将从<条码>格式:options 元件转换成ProtorDTOenum

<>1> 添加另外两种方法,供操作员使用

    // Enum level method to get enum instance by operator field.
    public static OperatorDTO getByOperator( final String p_operator ) {
        for ( OperatorDTO operatorDTO : OperatorDTO.values() ) {
            if ( operatorDTO.isOperatorEqual( p_operator ) ) {
                return operatorDTO;
            }
        }
        return null;
    }
    // Instance level method to compare operator field.
    public boolean isOperatorEqual( final String p_operator ) {
        return getOperator().equals( p_operator ) ? true : false;
    }

<>2> 创造习俗转化者

import org.springframework.core.convert.converter.Converter;

public class OperatorDTOConverter implements Converter<String, OperatorDTO> {
    public OperatorDTO convert( String source ) {
        return OperatorDTO.getByOperator( source.trim() );
    }
}

<>3> 配置中的登记转换器(此处为java config)

@Configuration
@EnableWebMvc
@ComponentScan( basePackages = { "your.base.package"} )
public class AppWebConfig extends WebMvcConfigurerAdapter {
@Override
    public void addFormatters( FormatterRegistry registry ) {
        registry.addConverter( String.class, OperatorDTO.class, new OperatorDTOConverter() );
    }

    ...
}




相关问题
multi-staged form using spring

please forgive me for this stupid questions. I just started developing web application using spring yesterday. The project that i worked on, have a multi staged form, that require users to complete ...

Rewrite spring-security redirect URLs

I m trying to get Tuckey UrlRewriteFilter to tidy up URLs for my webapp. One problem I ve got is that when spring-security notices that an anonymous user is trying to access a protected resource it ...

Spring Advice - submitting a form

I am having serious problems with code I have written with Spring so I have decided to start from scratch and ask for advice. Here are my requirements: When the page first loads I need a list of ...

How to implement Logout feature using Spring Web Mvc

I am new to Spring Web MVC.. Can I get some example or online link that shows me how to implement logout feature using spring web mvc ? I don t want to use the in built feature of spring security (i....

Exception thrown after processing onSubmitAction

I am very new to Spring and I have a simpleFormController with 2 methods. referenceData() with is called when the page loads and onSubmitAction() which is called on the submit of a form. I am ...

热门标签