Map-backed Actionform alternative in Struts 2

前端 未结 2 578
死守一世寂寞
死守一世寂寞 2021-01-01 04:50

In Struts 1, I have used map-backed action form to get dynamic fields values.

public MyForm extends ActionForm {
    private final Map values = new HashMap()         


        
相关标签:
2条回答
  • 2021-01-01 05:15

    You should map the fields of the form to the action like this

    <s:textfield name="myForm.values['%{dynamicNames}']"/> 
    

    It doesn't clear what value is for dynamicNames, actually it should be the key for the object pushed on the value stack while iterating the map and as soon as you running model driven the code will look like

    <s:iterator value="values">
      <s:textfield name="myForm.values['%{key}']"/>
    </s:iterator>
    

    OGNL will take care of the mapping such names and populate values of the fileds in the form and in the action when you submit the form.

    In addition if you need to place the values entered by user to another object say myForm2 then you could use value attribute value="%{value}" of the textfield to populate the form from the first model.

    See the reference guide how to use model driven interface and model driven interceptor. Also there's a reference to get you know how objects from the form converted by type to the action objects.

    0 讨论(0)
  • 2021-01-01 05:24

    You can put your map directly to action class and in JSP use Struts2 tags to submit/get values.

    Action

    public class SaveAction extends ActionSupport {
        private Map<String, Object> map; 
    
        public String execute(){
          // do something with map
          return SUCCESS;
        }
    
        // getter/setter for map
    }
    

    JSP

    <s:form action="saveAction">
      <s:textfield name="map['somekey']" />
      <s:submit />
    </s:form>
    
    0 讨论(0)
提交回复
热议问题