Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.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获取对Spring模型变量的访问_Javascript_Java_Spring_Spring Mvc_Thymeleaf - Fatal编程技术网

从JavaScript获取对Spring模型变量的访问

从JavaScript获取对Spring模型变量的访问,javascript,java,spring,spring-mvc,thymeleaf,Javascript,Java,Spring,Spring Mvc,Thymeleaf,如何在JavaScript中访问变量(在我的控制器中作为属性添加到模型中)以及如何使用它的属性 我的模型: public class Category { private Long id; private String name; private List<Recipe> recipes = new ArrayList<>(); } 公共类类别{ 私人长id; 私有字符串名称; 私有列表配方=新的ArrayList(); } 我的控制器: @R

如何在JavaScript中访问变量(在我的控制器中作为属性添加到模型中)以及如何使用它的属性

我的模型:

public class Category {
    private Long id;
    private String name;
    private List<Recipe> recipes = new ArrayList<>();
}
公共类类别{
私人长id;
私有字符串名称;
私有列表配方=新的ArrayList();
}
我的控制器:

@RequestMapping(path = "/", method = RequestMethod.GET)
public String showAllCategories(Model model) {
    List <Category> categories = categoryService.findAll();
    model.addAttribute("categories", categories);
    return "index";
}
@RequestMapping(path=“/”,method=RequestMethod.GET)
公共字符串showAllCategories(模型){
List categories=categoryService.findAll();
model.addAttribute(“类别”,categories);
返回“索引”;
}
在我的网页上,我需要显示所有类别(没问题,使用Thymeleaf th:each=“category:
${categories}”
),并且能够向categories变量添加新类别并设置其属性(例如id、名称、食谱)。
那么,如何在JavaScript中访问变量“categories”,以及如何在
JavaScript
中添加新元素和设置属性?

您可以对该方法进行ajax调用

// using jquery
 $.ajax({
    url:'/',
    success:function(response){
       // get the return pf the method here
       // can be assigned to any variable
    }
java方法更改

 @RequestMapping(path = "/", method = RequestMethod.GET)
public String showAllCategories(Model model) {
     //rest of code
    return (the value which which you want to assign to model property);
}

您可以对该方法进行ajax调用

// using jquery
 $.ajax({
    url:'/',
    success:function(response){
       // get the return pf the method here
       // can be assigned to any variable
    }
java方法更改

 @RequestMapping(path = "/", method = RequestMethod.GET)
public String showAllCategories(Model model) {
     //rest of code
    return (the value which which you want to assign to model property);
}

您必须创建一个API。例如 其思想是创建可由javascript调用的方法(例如,通过),并执行您需要在java中执行的操作

这里有一些伪代码:

@RequestMapping(path = "/addCategory", method = RequestMethod.GET)
public boolean AddCategory(Model model) {
  //Do your logic to add category

  if (/*everything went good*/) return true;
  else return fale;
}

您必须创建一个API。例如 其思想是创建可由javascript调用的方法(例如,通过),并执行您需要在java中执行的操作

这里有一些伪代码:

@RequestMapping(path = "/addCategory", method = RequestMethod.GET)
public boolean AddCategory(Model model) {
  //Do your logic to add category

  if (/*everything went good*/) return true;
  else return fale;
}

当您可以作为
JSON
直接访问对象时,为什么要使用
Model

JS片段使用角度:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
使用Jquery的JS代码片段:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
Java代码片段:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
@RestController
@请求映射
公共类控制器{
@RequestMapping(path=“/get”,method=RequestMethod.get)
公共列表showAllCategories(){
return categoryService.findAll();
}
}

当您可以作为
JSON
直接访问对象时,为什么要使用
Model
?:

JS片段使用角度:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
使用Jquery的JS代码片段:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
Java代码片段:

$http.get('/app/get').then(function(response) {
        $scope.categoryList = response.data;
 }, function(response) {
});
$.ajax({
        url: "/app/get",
        type: 'GET',
        success: function(response) {
        var categoryList = response.data;
        console.log(categoryList);
      }
});
@RestController
@RequestMapping
public class Controller {

@RequestMapping(path = "/get", method = RequestMethod.GET)
public List <Category> showAllCategories() {
    return categoryService.findAll();
 }
}
@RestController
@请求映射
公共类控制器{
@RequestMapping(path=“/get”,method=RequestMethod.get)
公共列表showAllCategories(){
return categoryService.findAll();
}
}