【问题标题】:How to display html:text using <logic:iterate> for List<String> in Struts1.1如何在 Struts1.1 中为 List<String> 使用 <logic:iterate> 显示 html:text
【发布时间】:2015-04-25 18:29:45
【问题描述】:

我的ActionForm 中有一个List&lt;String&gt;,它必须在JSP 页面中使用Struts1.1 表示为文本字段

 List<String> url=new ArrayList<String>();
 public List<String> getUrl() {
        return url;
    }
    public void setUrl(List<String> url) {
        this.url = url;
    }

以下是我在 JSP 页面中使用的代码

 <logic:iterate id="urlIterate" name="pForm" property="url">
    <html:text property="?????" name="urlIterate"/>
    </logic:iterate>

&lt;html:text&gt;List&lt;SomeClass&gt; 很有效,因为属性可以指向特定变量,List&lt;SomeClass&gt; 充当 bean。

 <logic:iterate id="listUserId" property="listUsers" name="pForm">
 <html:text name="listUserId" property="username"/>
 </logic:iterate>

其中 listUsers 是 ListUser 类型的类 私有列表列表用户; 在property="username"usernameUser类里面的一个变量

List&lt;String&gt;

<bean:write name="urlIterate" />

我没有发现使用&lt;bean:write&gt; 显示为文本的问题,因为它只有name 属性。 但是要使用&lt;html:text&gt;,我们需要添加另一个强制属性property

谁能帮帮我。 property='?????' 使用什么值才能使 html:text 正常工作。

提前致谢

提供更多代码.. UI 的 JSP 页面。

<%@taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>

<html>
<head>
</head>
<body>
<html:form action="/plist">
<h1>Struts &lt;logic:iterate&gt; example</h1>

  <logic:iterate property="listMsg" name="pForm"  id="listMsgId">
    <p>
    List Messages </p> <html:text property="listMsgId" indexed="true"/> 

</logic:iterate>

 <html:submit/>

  </html:form>
</body>
</html>

动作类:

public class PrintMsgAction extends Action {


    public ActionForward execute(ActionMapping mapping,ActionForm form,
        HttpServletRequest request,HttpServletResponse response) 
                throws Exception {

        List<OptionsListCollection> listoptions=new ArrayList<OptionsListCollection>();

                for(int i=0;i<10;i++){
                    listoptions.add(new OptionsListCollection("Option"+i, "OptionValue"+i));    
                }

                List<String> listMsg = new ArrayList<String>();

                listMsg.add("Message A");
                listMsg.add("Message B");
                listMsg.add("Message C");
                listMsg.add("Message D");

                            PrintForm pForm=(PrintForm)form;
                pForm.setListMsg(listMsg);



                return mapping.findForward("plist");
            }
}

和ActionForm

public class PrintForm extends ActionForm{

    private List<String> listMsg;

    public List<String> getListMsg() {
        return listMsg;
    }
    public void setListMsg(List<String> listMsg) {
        this.listMsg = new ArrayList<String>();

        this.listMsg = listMsg;

    }
}

【问题讨论】:

    标签: java jsp struts jsp-tags struts-1


    【解决方案1】:

    检查struts-logic-iterate-example。你的代码应该是 -

     <logic:iterate id="urlIterate" name="pForm" >
        <html:text property="urlIterate" indexed="true"/>
     </logic:iterate>
    

    【讨论】:

    • 嗨 Farrel,我之前尝试过使用property="url",但遇到了一个异常javax.servlet.ServletException: javax.servlet.jsp.JspException: No getter method for property url of bean urlIterate。您指定的链接与&lt;bean:write&gt; 相关,但与&lt;html:text&gt; 无关。
    • 现在我已经通过在我的代码的&lt;logic:iterate&gt; 中删除property="url" 来尝试您的代码,但仍然收到异常消息。 javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot create iterator for this collection
    • 可以添加表单吗?
    • 对不起,我没有考虑到 html:text 和 bean:write 之间的差异
    • 你能指定我应该添加哪个表单我创建的ActionForm类或JSP表单代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 2022-01-05
    • 2018-12-07
    • 1970-01-01
    • 2020-03-18
    相关资源
    最近更新 更多