窗口在jsp中取消。如何返回上一页。 JS

时间:2015-09-19 00:06:31

标签: javascript java jsp

我在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:

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);
    }

}

Interface,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>

我的问题: 什么时候,我点击删除,我想得到转发消息,告诉我: &#34;您确定要删除&#34;和按钮,&#34;是&#34;,&#34;否&#34;。 怎么做,我不知道。因为,我只学习java,而且没有更多的月份。 请帮忙。感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用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获取更好的确认框

相关问题