Jsp 在doPost()之后保留相同的URL参数
我有一个jsp页面,根据url列出信息。 例如,url可以是: "" jsp将列出该特定“组”的所有“人员” 在这个Jsp页面(/WEB-INF/views/group.Jsp)中,我有一个表单可以添加一个新的“person”。 我只想在提交表单并在DB中添加用户后,返回第一个url:,再次查看新用户的列表 在我的doPost()方法中,我有:Jsp 在doPost()之后保留相同的URL参数,jsp,servlets,Jsp,Servlets,我有一个jsp页面,根据url列出信息。 例如,url可以是: "" jsp将列出该特定“组”的所有“人员” 在这个Jsp页面(/WEB-INF/views/group.Jsp)中,我有一个表单可以添加一个新的“person”。 我只想在提交表单并在DB中添加用户后,返回第一个url:,再次查看新用户的列表 在我的doPost()方法中,我有: public void doPost( HttpServletRequest request, HttpServletResponse response
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
InfoUser infoUser = new InfoUser();
infoUser.setName(request.getParameter("name"));
infoUser.setGroup_id(Integer.parseInt(request.getParameter("group_id")));
infoUser.setDescription(request.getParameter("description"));
infoUserDao.add(infoUser);
this.getServletContext().getRequestDispatcher("/WEB-INF/views/group.jsp").forward(request,response);
}
当然,在doPost()方法之后,我返回group.jsp,它有以下url:“
发布后是否仍有返回“”的选项?
感谢您的帮助您可以将用户重定向到正确的地址(这需要发出另一个请求): 或者,您可以在页面加载后使用JavaScript在浏览器中更新URL:
window.history.pushState("object or string", "Title", "/group?ID=27");
供您参考,这是我用来解决问题的代码:
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
InfoPoint infoPoint = new InfoPoint();
infoPoint.setName(request.getParameter("name"));
infoPoint.setLength_id(Integer.parseInt(request.getParameter("length_id")));
infoPoint.setDescription(request.getParameter("description"));
infoPointDao.ajouter(infoPoint);
int ID = infoPoint.getLength_id();
request.setAttribute("infoPoints", infoPointDao.lister(ID));
response.sendRedirect("/points?ID="+ID);
}
希望有一天它能帮助别人;) 你好,谢谢你的帮助。我知道我可以将用户重定向到正确的地址,但url中的ID是一个变量。如何在重定向中添加变量?如何使用
response.sendRedirect(String.format(“/group?ID=%s”,request.getParameter(“ID”))代码>
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
InfoPoint infoPoint = new InfoPoint();
infoPoint.setName(request.getParameter("name"));
infoPoint.setLength_id(Integer.parseInt(request.getParameter("length_id")));
infoPoint.setDescription(request.getParameter("description"));
infoPointDao.ajouter(infoPoint);
int ID = infoPoint.getLength_id();
request.setAttribute("infoPoints", infoPointDao.lister(ID));
response.sendRedirect("/points?ID="+ID);
}