jsp中的窗口取消.如何回到上一页.JS

Window cancel in jsp. How to return to previous page. JS

本文关键字:一页 JS 何回 窗口 取消 jsp      更新时间:2023-09-26

我的JAVA类控制器:

    public class Controller extends HttpServlet {
    private Chooser chooser = Chooser.INSTANCE;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        processRequest(req, resp);
    }
    private void processRequest(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String page = chooser.chooseCommand(req.getParameter("command")).execute(req, resp);
            req.getRequestDispatcher(page).forward(req, resp);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }
}

下一个,ENUM类,选择page:

public enum Chooser {
    INSTANCE;
    private Map<String, ICommand> commandMap = new HashMap<>();
    private Chooser() {
        // commands for profession
        commandMap.put("professions", new ProfessionCommand());
        commandMap.put("addProfession", new AddProfessionCommand());
        commandMap.put("saveProfession", new SaveProfessionCommand());
        commandMap.put("deleteProfession", new DeleteProfessionCommand());
        commandMap.put("editProfession", new EditProfessionCommand());
        public ICommand chooseCommand(String command) {
        return commandMap.get(command);
    }
}

界面,icommand:

public interface ICommand {
    String execute(HttpServletRequest request, HttpServletResponse resp);
}

我的类DeleteProfessionCommand:

public class DeleteProfessionCommand implements ICommand {
    private ApplicantDBProvider provider = ApplicantDBProvider.INSTANCE;
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse resp) {
        try {
            Long professionId = Long.parseLong(request.getParameter("id"));
            provider.deleteProfession(professionId);
        } catch (Exception e) {
            request.setAttribute("error", e);
            return "pages/error.jsp";
        }
        return "controller?command=professions";
    }
}

在我的jsp中,当我想删除行时,我使用了这个命令:

<a href="controller?command=deleteProfession&id=${profession.getId()}">Delete</a>

我的问题:当,我点击删除,我想得到提醒消息,那告诉我:"是否确定要删除",以及按钮"是"、"否"。我不知道该怎么做。因为,我只学java,没有更多的月。请帮助。谢谢。

您可以使用javascript实现,在jsp中为Delete添加锚标记

<a href="controller?command=deleteProfession&id=${profession.getId()}">Delete</a>

做这些改变,

<a class="deleteAnchor" data-command="deleteProfession" data-id="${profession.getId()}" href="javascript:void(0);">Delete</a>

然后,在jsp的末尾添加以下内容,

<script type="text/javascript" src="//code.jquery.com/jquery-2.1.3.js"></script>
<script type="text/javascript">
$('.deleteAnchor').on('click', function(){
    // confirm will give you a 'ok' and 'cancel' button instead of yes or no
    // you can use one of the other libraries like bootbox to get a nice confirmation dialog box with 'yes' and 'no' buttons
    var result = confirm('Are you sure to delete?');
    if(result) {
        //Delete
        $.ajax({
            url: 'controller',
            data: {'command' : $(this).data('command'), 'id' : $(this).data('id')},
            success: function() {
                //Do something on success
            }
        });
    } else {
        //Dont delete
    }
});
</script>

您可以参考bootbox获得更好的确认框