【问题标题】:Struts2 Load ALL values in a s:select tag into a HashMap in the action classStruts2 将 s:select 标记中的所有值加载到动作类中的 HashMap 中
【发布时间】:2012-03-15 08:39:21
【问题描述】:

我已经对这个主题进行了足够的研究,没有任何运气:-(

我的要求是从具有 s:select 标记的 jsp 表单中加载在 action 类中声明的 HashMap。

这是我的动作课

public class AttributeAction extends ActionSupport {
    private HashMap<String, String> lstAttrTypesHashMap;
    public void setLstAttrTypesHashMap(HashMap<String, String> lstAttrTypesHashMap) {
        this.lstAttrTypesHashMap = lstAttrTypesHashMap;
    }

    public HashMap<String, String> getLstAttrTypesHashMap() {
        return lstAttrTypesHashMap;
    }

    public String renderPageAction() {
        lstAttrTypesHashMap.put("ENTRY1", "VALUE1");
        lstAttrTypesHashMap.put("ENTRY2", "VALUE2");
        lstAttrTypesHashMap.put("ENTRY3", "VALUE3");
        return SUCCESS;
    }

    public String searchAction() {
        logger.info("***************************************");
        logger.info("searchAction Started ...");
        logger.info("a.getType() = [" + a.getType() + "]");
        logger.info("getLstAttrTypesHashMap() = [" + getLstAttrTypesHashMap() + "]");
        return SUCCESS;
    } 
}

这是我在 jsp 中显示下拉菜单的方式

<s:select 
  key="a.type" 
  label="Select Object Type" 
  name="a.type" 
  list="lstAttrTypesHashMap" />

这里是struts.xml

<action name="attributeSearch" method="searchAction" class="com.frk.gid.action.AttributeAction">
    <result name="success">/AttributeResult.jsp</result>
    <result name="input">/AttributeInput.jsp</result>
</action>
    <action name="attributeRender" method="renderPageAction" class="com.frk.gid.action.AttributeAction">
    <result name="success">/AttributeInput.jsp</result>
</action>

当上面的 jsp 加载时,我可以看到下拉列表 populated 很好。但是,当我将其提交回操作时,我只能看到 selected value (a.type) 。哈希图恰好为空。我还需要做些什么来加载这个 HashMap 吗?我的理解是 Struts2 会在提交时自动从下拉列表中加载 HashMap - 显然不是......感谢任何输入!!!!

【问题讨论】:

  • S2 将根据 HTML 标准仅发送 select 中的选定值,因为 select 最终只是一个 HTML select 标记。
  • 为什么需要返回 Hashmap?如果您希望在更改某些内容时下拉列表值相同,您可以将 list="lstAttrTypesHashMap" 更改为 list="getLstAttrTypesHashMap"。或者,如果您正在做其他事情,您可以遍历 hashmap 以将值分配给隐藏值。
  • 正如@batbaatar 所说,使用隐藏变量或将其保存在会话中。

标签: forms struts2 hashmap populate


【解决方案1】:

非常感谢您的回复!

最后我决定去传递隐藏变量,它就像一个魅力。

另外,我有复杂的类型,比如 Map>,我必须编写自己的类型转换器来从字符串转换为字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2014-01-26
    • 1970-01-01
    • 2015-09-10
    • 2018-10-11
    相关资源
    最近更新 更多