简体   繁体   English

Spring MVC JSP Jquery jQuery调用控制器方法单击按钮后发布重定向错误

[英]Spring MVC JSP Jquery call controller method on button click post redirect error

  • Assumptions, I am new to Spring MVC, JSP, Scripting/Ajax 假设,我是Spring MVC,JSP,脚本/ Ajax的新手

    Here's my task. 这是我的任务。

In Spring MVC, I have buttons in my jsp page, on button click, I want to perform some task (controller method), which doesn't return anything. 在Spring MVC中,我的jsp页面中有按钮,单击按钮后,我想执行一些任务(控制器方法),该任务什么也不返回。 I want to show the same page. 我想显示同一页面。 It shouldn't reload the page nor should redirect to some other page. 它不应该重新加载页面,也不应该重定向到其他页面。

Here's what I am doing... 这就是我在做什么...

  1. I have a page with lots of buttons. 我的页面上有很多按钮。 I am using bootstrap css and button tag. 我正在使用引导CSS和按钮标签。 like, 喜欢,

    Start 开始

  2. On this button click, I am calling an Ajax to the method from controller, 单击此按钮后,我正在从控制器调用Ajax到方法,

      $('#startApp').click(function() { BootstrapDialog.show({ message : 'Sure ?!', buttons : [ { label : 'Ok', cssClass : 'btn-default', action : function(dialogItself) { $.ajax({ type : "POST", url : "/MyApp/startApp", success : function(response) { alert("Success"); } }); dialogItself.close(); } }, { label : 'Close', action : function(dialogItself) { dialogItself.close(); } } ] }); 
  3. This calls the controller method, 这将调用控制器方法,

      @RequestMapping(value = "/startApp", method = RequestMethod.POST) public void start() {// some operation.} 
  4. However, when I do this, operation is performed but in logs I am getting below error, 但是,当我执行此操作时,虽然执行了操作,但是在日志中却出现错误,

    root cause dispatcher: com.ibm.ws.jsp.webcontainerext.JSPErrorReport: JSPG0036E: Failed to find resource /WEB-INF/views/startApp.jsp 根本原因调度程序:com.ibm.ws.jsp.webcontainerext.JSPErrorReport:JSPG0036E:找不到资源/WEB-INF/views/startApp.jsp

Questions, 问题,

  • Is it the right way to do ? 这是正确的方法吗?
  • I don't want to redirect to startApp.jsp, I want it to return to my index.jsp (where the code resides), how can I achieve this ? 我不想重定向到startApp.jsp,我希望它返回到我的index.jsp(代码所在的位置),如何实现呢?

You need to return something to the client. 您需要将某些东西还给客户。 Spring default tries to send back startApp.jsp because that's what in the url (/startApp). Spring默认尝试发送回startApp.jsp,因为这是URL(/ startApp)中的内容。 Try this: this will send back an HTTP OK status (200). 尝试以下操作:这将发送回HTTP OK状态(200)。

@RequestMapping("/startApp", method = RequestMethod.POST)
public ResponseEntity start()
{
    return new ResponseEntity(HttpStatus.OK);
}

You can also send back a json by returning a POJO (it'll be automatically serialized by the Jackson JSON lib) if that's what you want, or even a simple string as the html content by returning a String. 如果需要的话,还可以通过返回POJO(它将由Jackson JSON库自动序列化)发送回json,或者通过返回String甚至将简单字符串作为html内容发送回json。

The purpose of ajax is to refresh a part of page. ajax的目的是刷新页面的一部分。 so re-directing to another page is meaningless, you could make the return type as String in your controller. 因此重定向到另一个页面是没有意义的,您可以在控制器中将返回类型设置为String

@RequestMapping(value = "/startApp", method = RequestMethod.POST)
@ResponseBody
        public String start() {
        // some operation
        return "sucess"
        }

Read my answer here Returning Hashmap From controller to JSP in Springmvc to know how to pass parameters in response 在这里阅读我的答案在Springmvc中将Hashmap从控制器返回到JSP,以了解如何在响应中传递参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM