Map-backed Actionform alternative in Struts 2

余生颓废 提交于 2019-11-30 10:11:46

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.

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>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!