“Select” not populated from List in struts

后端 未结 1 1146
广开言路
广开言路 2021-01-20 05:27

I have the following sources:

struts.xml




        
相关标签:
1条回答
  • 2021-01-20 05:59
    <s:select list = "categories" 
               key = "product.search.category" />
    

    You are listing a List<String> and trying to access, through OGNL . (dot notation), fields that do not exist.

    In OGNL

    product.search.category 
    

    is the equivalent of Java

    getProduct().getSearch().getCategory()
    

    Since you are listing Strings, just omit key attribute, because both your key and value will be the String itself.

    It seems that you are confusing key with name too: key is the key of the <option> element, while name is the Action's attribute that will receive the chosen value through its Setter.

    <s:select list = "categories" 
              name = "chosenCategory" />
    

    EDIT: for a succesful living, implement Preparable Interface and load there your "static" data:

    public class ProductSearchAction extends ActionSupport implements Preparable {
        private List<String> categories;
        private String chosenCategory;
    
        @override
        public void prepare() throws Exception {      
            categories = new ArrayList<String>();
            categories.add("Eins");
            categories.add("Zwei");
            categories.add("Drei");
        }
    
        @Override
        public String execute() throws Exception {
            return SUCCESS;
        }
    
        /* GETTERS AND SETTERS */
    }
    

    And you must specify fully qualified class names for each tag in struts.xml...

    0 讨论(0)
提交回复
热议问题