JSP页面只接收第一个arrayList

时间:2015-01-12 20:37:58

标签: java jsp model-view-controller taglib

在jsp页面中,它只打印首先访问的功能。如果首先访问function-1,则不打印function-2。如果首先访问Function-2,则不打印Function-1。请提出任何建议。

控制器:

populateTf(tfCount, request, response);
populateSa(saCount, request, response);

response.sendRedirect("testPage.jsp");

//FUNCTION-1
void populateTf(int tfCount, HttpServletRequest request, HttpServletResponse response){       
    ArrayList<TF> tfList = new ArrayList<TF>();

    String field1 = request.getParamter("xyz");
    String field2 = request.getParamter("xyz");
    String field3 = request.getParamter("xyz");

    tfList.add(new TF(field1, field2, field3));

    HttpSession session = request.getSession();
    session.setAttribute("tfList", tfList);
}

//FUNCTION-2
void populateSa(int saCount, HttpServletRequest request, HttpServletResponse response){       
    ArrayList<SA> saList = new ArrayList<SA>();

    String field1 = request.getParamter("xyz");

    saList.add(new SA(field1));

    HttpSession session = request.getSession();
    session.setAttribute("saList", saList);
}

商业对象:

public class TF{
    private String field1, field2, field3;
    public TrueFalse(String field1, String field2, String field3) {
        this.field1 = field1;
        this.field2 = field2;
        this.field3 = field3;
    }
    /* GETTERS AND SETTERS */
}    

public class SA{
   private String field1;
   public ShortAnswer(String field1) {
      this.field1 = field1;
   }
   /* GETTERS AND SETTERS */ 
} 

这是JSP页面:

<table>      
        <c:forEach items="${sessionScope.tfList}" var="tf">
        <tr>
                <td>${tf.field1}</td>
                <td>${tf.field2}</td>
                <td>${tf.field3}</td>
        </tr>
        </c:forEach>
</table>

<table>         
        <c:forEach items="${sessionScope.saList}" var="sa">
        <tr>
                <td>${sa.field1}</td>               
        </tr>
        </c:forEach>
</table>

1 个答案:

答案 0 :(得分:0)

也许不是两次调用request.getSession(),而是创建一个全局变量来保存servlet中的会话,并只调用request.getSession()一次。我也没有看到需要将请求和响应传递给这两个函数。

private HttpSession session = null;

public void doGet(HttpServletRequest request, HttpServletResponse  response) throws IOException, ServletException
{
    session = request.getSession();
    ....
    populateTf(tfCount);
    populateSa(saCount);
    ....
}

private void populateTf(int tfCount)
{       
    ArrayList<TF> tfList = new ArrayList<TF>();

    .... //where is field1, field2, field3 coming from anyway?

    tfList.add(new TF(field1, field2, field3));

    session.setAttribute("tfList", tfList);
}

private void populateSa(int saCount)
{       
    ArrayList<SA> saList = new ArrayList<SA>();

    .... //where is field1 coming from anyway?

    saList.add(new SA(field1));

    session.setAttribute("saList", saList);
}