Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jsp中的窗口取消。如何返回上一页。JS_Javascript_Java_Jsp - Fatal编程技术网

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

Javascript jsp中的窗口取消。如何返回上一页。JS,javascript,java,jsp,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 { pr

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

}
我的类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>

我的问题: 当我单击“删除”时,我想获取allert消息,该消息告诉我: “确定要删除”和按钮“是”、“否”。 怎么做,我不知道。因为,我只学java,一个月也学不到。
请帮忙。谢谢。

您可以使用javascript实现这一点,在jsp中使用用于删除的锚定标记

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

$('.deleteAnchor')。在('click',function()上{
//确认将给您一个“确定”和“取消”按钮,而不是“是”或“否”
//您可以使用其他库(如bootbox)获得一个带有“是”和“否”按钮的确认对话框
var result=confirm('确定要删除吗?');
如果(结果){
//删除
$.ajax({
url:'控制器',
数据:{'command':$(this).data('command'),'id':$(this.data('id')},
成功:函数(){
//为成功做点什么
}
});
}否则{
//不要删除
}
});
您可以参考更好的确认框

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