ajax - Spring Controller 在使用 Ajax/Post 传递整数数据时抛出 NullPointerException
问题描述
我正在尝试以 html 形式传递一组数据;
<form class="form-inline" id="createjobform" method="POST" th:action="@{/createJob}">
...
<div class="form-group">
<input type="text" class="form-control" id="nopth"
placeholder="Number Of People To Hire" name="nopth" />
</div>
<div class="form-group">
<input type="text" readonly="readonly" class="form-control"
id="listid" placeholder="ID" name="listid"
title="ID of the list which associated"
th:value="${findOneList.id}"/>
</div>
listid 在这里,来自另一个表(manytoone-onetomany),我想用这个 listid 添加新记录。当我在 phpmyadmin 上执行此操作时,它正在工作。但我想用 ajax 发布请求或不用 ajax 来做,不管实际上。我尝试了两种方法,但它显示相同的错误。
这是我的控制器;
@RequestMapping(value = "/createJob", method = RequestMethod.POST)
public @ResponseBody void createJob(@RequestBody Jobs jobs,
@RequestParam(value = "title", required = false) String title,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "nopth", required = false) Integer nopth,
@RequestParam(value = "lastDate", required = false) Date lastDate,
@RequestParam(value = "listid", required = false) Long listid,
HttpServletResponse hsr) throws IOException {
// if I do String nopth above and then
//jobs.setNopth(Integer.valueOf(nopth));
// error is NumberFormatException (cannot cast string to int)
jobs.setLastDate(lastDate);
jobs.setTitle(title);
jobs.setDescription(description);
jobs.setNopth(nopth);
Lists listttt = listsService.findOne(listid);
jobs.setLists(listttt);
jobsService.save(jobs);
mavHomepage.addObject("findOneList", listsService.findOne(jobs.getId()));
mavHomepage.addObject("mod", "VIEW_ONELIST");
hsr.sendRedirect("/oneList?id=" + listid);
}
所以错误是;
error: "Internal Server Error"
exception: "java.lang.NullPointerException"
message: "No message available"
path: "/createJob"
status: 500
在行jobs.setNopth(nopth);
错误也是;
error: "Internal Server Error"
exception: "org.springframework.dao.InvalidDataAccessApiUsageException"
message: "The given id must not be null!; nested exception is
java.lang.IllegalArgumentException: The given id must not be null!"
path: "/createJob"
status: 500
在行列表 listttt = listsService.findOne(listid);
这不是 ajax/post。当我做;
public @ResponseBody void createJob(@RequestBody Jobs jobs,
@RequestParam(value="listid, required="false") listid,
HttpServletResponse hsr){
Lists listttt = listsService.findOne(listid);
jobs.setLists(listttt);
...
}
和阿贾克斯;
var formData = {
title : $("#title").val(),
description : $("#description").val(),
nopth : $("#nopth").val(),
lastDate : $("#lastDate").val(),
listid : $("#listid").val(),
}
...
$.ajax({
type : "POST",
contentType : "application/json",
url : "/createJob",
data : JSON.stringify(formData),
....
同样的错误(给定的 id 不能为空!)
那么我必须如何传递来自整数/长值形式的数据?使用 AJAX 会更好。并且那个 listid 是 FOREIGN KEY。
模型类;
@Entity(name = "jobs")
public class Jobs {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "Title")
private String title;
@Column(name = "Description")
private String description;
@Column(name = "Number_Of_People_To_Hire")
private Integer nopth;
@Column(name = "Last_Application_Date")
private Date lastDate;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="listid")
private Lists lists;
...
@Entity(name = "lists")
public class Lists implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long id;
@Column(name = "List_Name")
public String listname;
@OneToMany(mappedBy = "lists", cascade = CascadeType.ALL)
private List<Jobs> jobs;
我尝试将 nopth 的类型更改为 String、Integer 等,然后输入 type="text or number" 没有变化。
解决方案
@RequestBody
此注解表示方法参数应绑定到 Web 请求的主体所以你必须像这样修复你的控制器:
@RequestMapping(value = "/createJob", method = RequestMethod.POST) @ResponseBody public void createJob( @RequestBody Jobs jobs, @RequestParam("listid") Long listid HttpServletResponse hsr) throws IOException { Lists listttt = listsService.findOne(listid); jobs.setLists(listttt); jobsService.save(jobs); ... }
您的 jquery 请求:
var formData = { title : $("#title").val(), description : $("#description").val(), nopth : $("#nopth").val(), lastDate : $("#lastDate").val() } var listid : $("#listid").val(), var settings = { "url": "http://localhost:8080/createJob?listid="+listid, "method": "POST", "data": JSON.stringify(formData) "headers": { "Content-Type": "application/json" } } $.ajax(settings).done(function (response) { console.log(response); });
推荐阅读
- appium - 如何附加appium服务器日志
- laravel-5 - 在 AppServiceProvider.php 引导中使用引用 db 安装 laravel 5 应用程序时出错
- azure - Azure Function Blob 触发器不响应手动导入
- hive - 如何根据 Impala 查询中的调整找到值的总和
- javascript - 如何让“appendChild”创建的 div 使用已经存在的样式
- docker - docker compose tcpdump 没有显示任何内容
- javascript - React useEffect 清理函数意外调用
- android - Android java.lang.IllegalStateException:已添加片段
- email - Plesk 电子邮件进入 Gmail 垃圾邮件并被 Outlook 拒绝
- python - 当 C++ 方法返回 std::map 时的 boost-python