如何在struts 2中迭代请求属性?

时间:2012-05-28 11:05:05

标签: java jsp struts2

我正在尝试在页面加载时从JSP页面名ActionSupport中的EnergyConsumptionAnalysis迭代请求属性,以使用相同的JSP页面使用struts2在浏览器上显示它,但是在运行时我得到空显示。谁能告诉我我在做什么?

JSP:

<s:action name = "FetchSite"></s:action>
  <s:iterator value="siteData">
        <s:property value = "name"/>
  </s:iterator>

行动支持:

public class EnergyAction extends ActionSupport implements  ServletRequestAware
{
    private HttpServletRequest request;
    @Override
    public String execute()
            throws Exception {
        //Get the site Data
        EnergyAnalysisBusinessHandler handler = new EnergyAnalysisBusinessHandler();

        //setting attribute as arraylist type
        request.setAttribute("siteData", handler.getSiteData());
        return SUCCESS;
   }

    public void setServletRequest(HttpServletRequest request){
        this.request = request;
    }

    public HttpServletRequest getServletRequest(){
    return request;
  }

struts.xml中:

<action name = "FetchSite" class = "com.javaworld.email.EnergyAction">
               <result name = "success">jsp/EnergyConsumptionAnalysis.jsp
               </result>
 </action>

2 个答案:

答案 0 :(得分:3)

1您正在尝试从值堆栈中获取siteData,但是您将其置于请求中。 这样你可以从请求中获取它:

<s:iterator value="#request.siteData">
        <s:property value = "name"/>
</s:iterator>

2将一些参数传递给.jsp的最简单方法是在Action中创建getter / setter。无需将参数放入请求中。

动作:

    public class EnergyAction extends ActionSupport
    {
        private Object siteData;

        @Override
        public String execute()
                throws Exception {
            //Get the site Data
            EnergyAnalysisBusinessHandler handler = new EnergyAnalysisBusinessHandler();
            siteData = handler.getSiteData();
            return SUCCESS;
       }

        public void setSiteData(Object siteData){
            this.siteData = siteData;
        }

        public Object getSiteData(){
            return siteData;
        }
    }

JSP:

<s:iterator value="siteData">
  <s:property value = "name"/>
</s:iterator>

答案 1 :(得分:0)

而不是:

<s:iterator value="siteData">
        <s:property value = "name"/>
  </s:iterator>

你可以用这个:

<s:iterator value="%{#request.siteData}">
        <s:property value = "name"/>
  </s:iterator>