spring-form:带有枚举的options标签 [英] spring-form:options tag with enum

查看:124
本文介绍了spring-form:带有枚举的options标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到麻烦,显示一个具有正确值的下拉列表。我正在使用< spring-form:select> < spring-form:options> code>< spring-form:option> 标签,我只是无法得到它来显示正确的选项。使用以下代码,我只应该列出选项2,选项7和选项8。



*注意 - 我不想显示每一个可能的枚举值,但由于某种原因,Spring似乎想要显示它们。它似乎完全忽略了提供给< spring-form:options> 标签的列表。



JSP标签

 < spring-form:select path =selectOptions> 
< spring-form:option value =label =***选择选项***/>
< spring-form:options path =$ {availableOptions}/>
< / spring-form:select>

枚举

  public enum SelectOptions {

// CHECKSTYLE_OFF:LineLength

/ **
*选项1.
* /
OPTION_1(1,选项1),
/ **
*选项2.
* /
OPTION_2(2,选项2),
/ **
*选项3.
* /
OPTION_3(3,选项3),
/ **
*选项4.
* /
OPTION_4(4,选项4),
/ **
*选项5.
* /
OPTION_5(5,选项5
/ **
*选项6.
* /
OPTION_6(6,选项6),
/ **
*选项7。
* /
OPTION_7(7,选项7),
/ **
*选项8.
* /
OPTION_8(8,选项8),
/ **
*选项9.
* /
OPTION_9(9,选项9),
/ **
*选项10.
* /
OPTION_10(10,选项10);

private int id;
private String description;

/ **
*构造函数。
*
* @param id id
* @param描述描述
* /
private SelectOptions(final int id,final String description){
this.id = id;
this.description = description;
}

/ **
*检索与传入的id相关联的{@link SelectOptions}。
*
* @param id与SelectOptions相关联的id
* @返回SelectOptions
* /
public static SelectOptions getInstance(final int id){

(最终SelectOptions selectOptions:SelectOptions.values()){
if(selectOptions.id == id){
return selectOptions;
}
}

throw new IllegalArgumentException(SelectOptions无法用id [+ id +]确定);
}

/ **
*检索与传递的描述相关联的{@link SelectOptions}。
*
* @param描述与SelectOptions相关联的描述
* @返回SelectOptions
* /
public static SelectOptions getInstance(final String description){

(最终SelectOptions selectOptions:SelectOptions.values()){
if(selectOptions.description == description){
return selectOptions;
}
}

抛出新的IllegalArgumentException(SelectOptions无法用描述[+ description +]确定);
}

/ **
* Simple Getter。
*
* @返回id
* /
public int getId(){
return this.id;
}

/ **
*简单设置器。
*
* @param id要设置的ID
* /
public void setId(final int id){
this.id = id;
}

/ **
* Simple Getter。
*
* @返回描述
* /
public String getDescription(){
return this.description;
}

/ **
*简单设置器。
*
* @param描述设置
* /
public void setDescription(final String description){
this.description = description;
}
}

控制器

  public class SpringController实现SpringControllerInterface {

/ **
* /WEB-INF/jsp/myJSP.jsp。
* /
private static final String PAGE =/WEB-INF/jsp/myJSP.jsp;

/ **
* {@inheritDoc}
* /
@Override
public ModelAndView load(final Model model){

final ModelAndView mav = new ModelAndView(PAGE);

final列表< SelectOptions> availableOptions = this.getAvailableOptions();

mav.addObject(availableOptions,availableOptions);

return mav;
}

/ **
* {@inheritDoc}
* /
@Override
public ModelAndView save(final Model model){

final ModelAndView mav = new ModelAndView(PAGE);

// TODO

return mav;
}

/ **
* {@inheritDoc}
* /
@Override
public Model getModel(){
返回新的ModelImpl();
}

/ **
* @返回可用选项列表
* /
public List< SelectOptions> getAvailableOptions(){

final List< SelectOptions> availableOptions = Lists.newArrayList();

availableOptions.add(SelectOptions.OPTION_1);
availableOptions.add(SelectOptions.OPTION_7);
availableOptions.add(SelectOptions.OPTION_8);

return availableOptions;
}
}

模型

  public class ModelImpl implements Model {

private SelectOptions selectOptions;

/ **
*简单的Getter。
*
* @返回selectOptions
* /
@Override
public SelectOptions getSelectOptions(){
return this.selectOptions;
}

/ **
*简单设置器。
*
* @param selectOptions selectOptions设置
* /
@Override
public void setSelectOptions(final SelectOptions selectOptions){
this.selectOptions = selectOptions;
}


}


解决方案

看起来这个问题的解决方案是我在< spring-form:options> 标签中使用属性path。它应该是项目而不是路径。



更正的JSP片段:

 < spring-form:select path =selectOptions> 
< spring-form:option value =label =***选择选项***/>
< spring-form:options items =$ {availableOptions}/>
< / spring-form:select>


I'm having troubles displaying a dropdown list with the proper values. I'm using the <spring-form:select>, <spring-form:options> and <spring-form:option> tags, and I just can't get it to display the correct options. With the following code, I should only be listing "Option 2", "Option 7", and "Option 8".

*Note - I do NOT want to display every possible Enum value, but for some reason Spring seems to want to display them all. It appears to be completely ignoring the list that is provided to the <spring-form:options> tag.

JSP Tags

<spring-form:select path="selectOptions">
    <spring-form:option value="" label="*** Select Option ***" />
    <spring-form:options path="${availableOptions}" />
</spring-form:select>

Enum

public enum SelectOptions {

    // CHECKSTYLE_OFF: LineLength

    /**
     * Option 1.
     */
    OPTION_1(1, "Option 1"),
    /**
     * Option 2.
     */
    OPTION_2(2, "Option 2"),
    /**
     * Option 3.
     */
    OPTION_3(3, "Option 3"),
    /**
     * Option 4.
     */
    OPTION_4(4, "Option 4"),
    /**
     * Option 5.
     */
    OPTION_5(5, "Option 5"),
    /**
     * Option 6.
     */
    OPTION_6(6, "Option 6"),
    /**
     * Option 7.
     */
    OPTION_7(7, "Option 7"),
    /**
     * Option 8.
     */
    OPTION_8(8, "Option 8"),
    /**
     * Option 9.
     */
    OPTION_9(9, "Option 9"),
    /**
     * Option 10.
     */
    OPTION_10(10, "Option 10");

    private int id;
    private String description;

    /**
     * Constructor.
     *
     * @param id the id
     * @param description the description
     */
    private SelectOptions(final int id, final String description) {
        this.id = id;
        this.description = description;
    }

    /**
     * Retrieves the {@link SelectOptions} associated with the passed in id.
     *
     * @param id the id associated with the SelectOptions
     * @return the SelectOptions
     */
    public static SelectOptions getInstance(final int id) {

        for (final SelectOptions selectOptions : SelectOptions.values()) {
            if (selectOptions.id == id) {
                return selectOptions;
            }
        }

        throw new IllegalArgumentException("SelectOptions could not be determined with id [" + id + "]");
    }

    /**
     * Retrieves the {@link SelectOptions} associated with the passed in description.
     *
     * @param description the description associated with the SelectOptions
     * @return the SelectOptions
     */
    public static SelectOptions getInstance(final String description) {

        for (final SelectOptions selectOptions : SelectOptions.values()) {
            if (selectOptions.description == description) {
                return selectOptions;
            }
        }

        throw new IllegalArgumentException("SelectOptions could not be determined with description [" + description + "]");
    }

    /**
     * Simple Getter.
     *
     * @return the id
     */
    public int getId() {
        return this.id;
    }

    /**
     * Simple Setter.
     *
     * @param id the id to set
     */
    public void setId(final int id) {
        this.id = id;
    }

    /**
     * Simple Getter.
     *
     * @return the description
     */
    public String getDescription() {
        return this.description;
    }

    /**
     * Simple Setter.
     *
     * @param description the description to set
     */
    public void setDescription(final String description) {
        this.description = description;
    }
}

Controller

public class SpringController implements SpringControllerInterface {

    /**
     * /WEB-INF/jsp/myJSP.jsp.
     */
    private static final String PAGE = "/WEB-INF/jsp/myJSP.jsp";

    /**
     * {@inheritDoc}
     */
    @Override
    public ModelAndView load(final Model model) {

        final ModelAndView mav = new ModelAndView(PAGE);

        final List<SelectOptions> availableOptions = this.getAvailableOptions();

        mav.addObject("availableOptions", availableOptions);

        return mav;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ModelAndView save(final Model model) {

        final ModelAndView mav = new ModelAndView(PAGE);

        // TODO

        return mav;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Model getModel() {
        return new ModelImpl();
    }

    /**
     * @return a list of available options
     */
    public List<SelectOptions> getAvailableOptions() {

        final List<SelectOptions> availableOptions = Lists.newArrayList();

        availableOptions.add(SelectOptions.OPTION_1);
        availableOptions.add(SelectOptions.OPTION_7);
        availableOptions.add(SelectOptions.OPTION_8);

        return availableOptions;
    }
    }

Model

public class ModelImpl implements Model {

    private SelectOptions selectOptions;

    /**
     * Simple Getter.
     *
     * @return the selectOptions
     */
    @Override
    public SelectOptions getSelectOptions() {
        return this.selectOptions;
    }

    /**
     * Simple Setter.
     *
     * @param selectOptions the selectOptions to set
     */
    @Override
    public void setSelectOptions(final SelectOptions selectOptions) {
        this.selectOptions = selectOptions;
    }


}

解决方案

It looks like the solution to this problem was that I was using the attribute "path" in the <spring-form:options> tag. It should have been "items", not "path".

the corrected JSP fragment:

<spring-form:select path="selectOptions">
    <spring-form:option value="" label="*** Select Option ***" />
    <spring-form:options items="${availableOptions}" />
</spring-form:select>

这篇关于spring-form:带有枚举的options标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆