gpt4 book ai didi

javascript - 如何处理JSP文件中的@RequestParam?

转载 作者:行者123 更新时间:2023-12-02 11:22:41 27 4
gpt4 key购买 nike

大家好!我有一个关于在 @RestController 中使用 @RequestParam 的问题。我想知道如何从客户端获取@RequestParam。服务器代码(@RestController):

@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public ResponseEntity<List<ProjectBean>> getAllBeans(@RequestParam(name = "head") Integer headId) {
Integer head = securityService.getLoggedAccountId();
List<ProjectBean> projects = (List<ProjectBean>) projectService.getByHead(head);
return new ResponseEntity<List<ProjectBean>>(projects, HttpStatus.OK);
}

和 JSP/JavaScript:

function loadProjects() {
$.ajax({
url : 'rest/projects',
method : 'GET',
headers : {
'Content-Type' : 'application/json',
},
success: function(data){
$.each(data, function(index, project) {
addProject(project);
});
}
});
}

此函数加载所有项目,但不具有确切的 headId

实体:

@Entity
@Table(name = "projects")

public class Project {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "project_name", nullable = false, length = 255)
private String projectName;

@Column(name="head")
private Integer head; //need to get projects with this value

@Column(name = "description")
private String description;

@Column(name = "photo")
private String photo;

@Column(name = "status", nullable = false, length = 200)
private String status;
}

最佳答案

我认为@NayoR 的建议是值得推荐的。但要真正回答您的问题,您需要在 js 中使用查询字符串,例如:

function loadProjects() {
$.ajax({
url : 'rest/projects?head=' + headId,
method : 'GET',
headers : {
'Content-Type' : 'application/json',
},
success: function(data){
$.each(data, function(index, project) {
addProject(project);
});
}
});
}

关于javascript - 如何处理JSP文件中的@RequestParam?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49812152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com