带有枚举的spring-form:options标签

我在显示带有正确值的下拉列表时遇到麻烦。我使用的<spring-form:select><spring-

form:options>并且<spring-

form:option>标签,我只是不能得到它,以显示正确的选项。使用以下代码,我应该只列出“选项2”,“选项7”和“选项8”。

*注意-我不想显示每个可能的Enum值,但是出于某种原因,Spring似乎希望显示所有这些值。似乎完全忽略了提供给<spring-form:options>标签的列表。

JSP标签

<spring-form:select path="selectOptions">

<spring-form:option value="" label="*** Select Option ***" />

<spring-form:options path="${availableOptions}" />

</spring-form:select>

枚举

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;

}

}

控制者

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;

}

}

模型

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;

}

}

回答:

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

更正后的JSP片段:

<spring-form:select path="selectOptions">

<spring-form:option value="" label="*** Select Option ***" />

<spring-form:options items="${availableOptions}" />

</spring-form:select>

以上是 带有枚举的spring-form:options标签 的全部内容, 来源链接: utcz.com/qa/399212.html

回到顶部