How to call a controller from JS so the controller can perform its request mappings?

Jared Smith

i'm trying to have a JavaScript file call a JavaSpring MVC Controller, then the controller will perform its request mapping functions.

I'm not sure if it is also possible to pass a variable to the controller so it can know which specific mapping to perform. IF one could demonstrate how to do both that would be immensely helpful. If one can simply show me how to call a controller via JS that would be very helpful as well and answer the question.

my controller will look similar to the following:

@Controller
@RequestMapping(value = "/waterquality/scale")
public class NmWaidsController {
    @RequestMapping(value = {"","/"})
    public String homePageWaids(){
        return "waterquality/scale/calmix";
    }

I would like the JS to call this controller.

If possible I would like to do the following, and have the JS pass a variable to the method which would look like the following:

@Controller
@RequestMapping(value = "/waterquality/scale")
public class NmWaidsController {
    @RequestMapping(value = {"","/"})
    public String homePageWaids(int viewChoice){
        switch(viewChoice){
             case 1:
                 return "waterquality/scale/calmix";
                 break;
             case 2:
                 return "waterquality/scale/caloddo";
                 break;
             case 3:
                 return "waterquality/scale/calstiff";
                 break;
             default:
                 return error;
                 break;
    }

Any help would be much appreciated! Thanks in advance!

hina10531

Here's an example that you are looking for.

@RequestParam will help you out.

First, JSON object.

var loginData = { 
       memberId : "test1",
       memberPw : "test2"
}

Second, Ajax.

 $.ajax({
        type: "POST",
        url: "YouActionName",
        data: loginData,
        success: function (result) {
            // do something.
        },
        error: function (result) {
            // do something.
        }
    });

And finally, your controller. Remember the RequestParam's key names must be matched with the JSON's member key names. It's case sensitive, so be careful.

@RequestMapping("/YourActionName")
public String YourActionName(@RequestParam("memberId") String id, @RequestParam("memberPw") String pw ){
  return new ModelAndView("ExpectedReturnView");
} 

attaching event listner is really simple.

if you have html like this...

<div id="exampleDiv"></div>

Then using basic id selector, you can attach an event like this below...

$('#exampleDiv').click(function(e) {
    // do what you want when click this element.
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Call controller function from HTML dynamically created

来自分类Dev

How to send request parameters to spring webflow controller

来自分类Dev

How to perform Controller.destroy action asynchronously in rails?

来自分类Dev

How do i call a controller action from within my view using Razor?

来自分类Dev

How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

来自分类Dev

Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

来自分类Dev

Rails Call Custom Controller Action from Custom Form

来自分类Dev

我的Call Spring Controller失败

来自分类Dev

Angular.js“ Controller as ...” + $ scope。$ on

来自分类Dev

New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

来自分类Dev

How to call a route by its name from inside a handler?

来自分类Dev

Multi call for NSURLConnection with multi UITableView in Same Controller

来自分类Dev

Angular Get Controller from Element

来自分类Dev

How can I call super() so it's compatible in 2 and 3?

来自分类Dev

How to separate Controller file in angularjs

来自分类Dev

How to test controller class in MVC?

来自分类Dev

Angular js: Access data filtered in ng-repeat (ngRepeat) from controller

来自分类Dev

How to access Child Controller Scope in Parent Controller in Angular?

来自分类Dev

AngularJs-使用外部controller.js

来自分类Dev

从Angular Controller控制SoundCloud JS SDK

来自分类Dev

将Underscore.js注入Angular Controller

来自分类Dev

Sails.js,从Controller调用路由

来自分类Dev

从Angular Controller控制SoundCloud JS SDK

来自分类Dev

在JS脚本中调用Angular Controller

来自分类Dev

AngularJs-使用外部controller.js

来自分类Dev

Angular JS“ Controller as”语法不起作用

来自分类Dev

内部JS Angular Controller没有被调用

来自分类Dev

How can i increase the number of ticks, a controller goes through, in a staggered animation (Flutter)

来自分类Dev

Controller from Parent Layout is not access by child views

Related 相关文章

  1. 1

    Call controller function from HTML dynamically created

  2. 2

    How to send request parameters to spring webflow controller

  3. 3

    How to perform Controller.destroy action asynchronously in rails?

  4. 4

    How do i call a controller action from within my view using Razor?

  5. 5

    How to call Twig extension filter or function from PHP (controller / service / other twig extension etc )?

  6. 6

    Updates controller-value and call controller-function from a directive. controller-calue not updated before function is called

  7. 7

    Rails Call Custom Controller Action from Custom Form

  8. 8

    我的Call Spring Controller失败

  9. 9

    Angular.js“ Controller as ...” + $ scope。$ on

  10. 10

    New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

  11. 11

    How to call a route by its name from inside a handler?

  12. 12

    Multi call for NSURLConnection with multi UITableView in Same Controller

  13. 13

    Angular Get Controller from Element

  14. 14

    How can I call super() so it's compatible in 2 and 3?

  15. 15

    How to separate Controller file in angularjs

  16. 16

    How to test controller class in MVC?

  17. 17

    Angular js: Access data filtered in ng-repeat (ngRepeat) from controller

  18. 18

    How to access Child Controller Scope in Parent Controller in Angular?

  19. 19

    AngularJs-使用外部controller.js

  20. 20

    从Angular Controller控制SoundCloud JS SDK

  21. 21

    将Underscore.js注入Angular Controller

  22. 22

    Sails.js,从Controller调用路由

  23. 23

    从Angular Controller控制SoundCloud JS SDK

  24. 24

    在JS脚本中调用Angular Controller

  25. 25

    AngularJs-使用外部controller.js

  26. 26

    Angular JS“ Controller as”语法不起作用

  27. 27

    内部JS Angular Controller没有被调用

  28. 28

    How can i increase the number of ticks, a controller goes through, in a staggered animation (Flutter)

  29. 29

    Controller from Parent Layout is not access by child views

热门标签

归档