在jsp页面中,它仅打印首先访问的功能。如果首先访问功能1,则不会打印功能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>
最佳答案
也许不要调用两次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);
}