用servlet在jsp中显示表

时间:2014-07-15 11:52:13

标签: java jsp servlets

我无法让表格显示在jsp上。你能看看我的代码,告诉我我做错了什么吗?谢谢!

这是java代码:

@WebServlet(name = "TurvingControllerServlet", urlPatterns = {"/TurvingController"})
@MultipartConfig

public class TurvingControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String INSERT_OR_EDIT = "/turvingen.jsp";
private static String LIST_TURVING = "/waarnemingen.jsp";
private Turving turv;

public TurvingControllerServlet() {
    super();
    turv = new Turving();
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String forward="";
    String action = request.getParameter("action");

    if (action.equalsIgnoreCase("delete")){
        String id = request.getParameter("id");
        turv.wisTurving();
        forward = LIST_TURVING;
        request.setAttribute("turving", turv.toonAlleTurvingen());    
    }  else if (action.equalsIgnoreCase("waarnemingen")){
        forward = LIST_TURVING;
        request.setAttribute("turving", turv.toonAlleTurvingen());
    } else {
        forward = INSERT_OR_EDIT;
    }

    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);
}
 @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Turving turving = new Turving();

    turving.setId(request.getParameter("id"));
    turving.setDatum(request.getParameter("datum"));
    turving.setTijd(request.getParameter("tijd"));
    turving.setPlaats(request.getParameter("plaats"));
    turving.setSpotternaam(request.getParameter("spotternaam"));
    turving.setVogelsoort(request.getParameter("vogelsoort"));
    turving.schrijfTurving();
    String id = request.getParameter("spotternaam");

    RequestDispatcher view = request.getRequestDispatcher(LIST_TURVING);
    request.setAttribute("turvingen", turv.toonAlleTurvingen());
    view.forward(request, response);
}
}

这就是JSP :(部分)

<table border=1>
    <thead>
        <tr>
            <th>Datum</th>
            <th>Tijd</th>
            <th>Plaats</th>
            <th>Spotternaam</th>
            <th>Vogelsoort</th>

        </tr>
    </thead>
    <tbody>
        <c:forEach  items="${turvingen}" var="turving">
            <tr>
                <td><c:out value="${turving.datum}" /></td>
                <td><c:out value="${turving.tijd}" /></td>
                <td><c:out value="${turving.plaats}" /></td>
                <td><c:out value="${turving.spotternaam}" /></td>
                <td><c:out value="${turving.vogelsoort}" /></td>


            </tr>
        </c:forEach>
    </tbody>
          </table>

我真的希望你能帮助我,我不知道我哪里出错......

2 个答案:

答案 0 :(得分:0)

您的属性名称不匹配。

您设置的属性称为turving。您请求的人称为turvingen

答案 1 :(得分:0)

来自servlet中的代码,

RequestDispatcher view = request.getRequestDispatcher(LIST_TURVING);
request.setAttribute("turvingen", turv.toonAlleTurvingen());
view.forward(request, response);

如果turv.toonAlleTurvingen()返回list。然后,在转发请求之前,需要初始化并填充servlet中的列表。

所以你需要这样做,

List list=new ArrayList();
list=turv.toonAlleTurvingen();

然后将列表转发给属性,

request.setAttribute("turvingen", list);

希望这会有所帮助!!