我正在尝试使用Spring Boot进行Crud操作,而我是新手。我已成功执行删除和创建零件。我在尝试编辑字段时遇到的问题。我正在使用MYSQL作为数据库。我在问题标题中提到了错误。解决它的任何帮助,请检查我的逻辑,我认为我的逻辑在/ showUpdate方法中是错误的。当我按下编辑按钮时,并没有带我去编辑页面并抛出此错误。
我的控制器类粘贴在下面:
Snapshot of Actual error i am having
package com.bilal.location.controllers;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.bilal.location.entities.Location;
import com.bilal.location.service.LocationService;
@Controller
public class LocationController {
@Autowired
LocationService service;
@RequestMapping("/showCreate")
public String showCreate() {
return "createLocation";
}
@RequestMapping("/savLoc")
public String saveLocation(@ModelAttribute("location") Location location,ModelMap modelMap) {
Location locationSaved = service.saveLocation(location);
String msg = "Location saved with id: " + locationSaved.getId();
modelMap.addAttribute("msg", msg);
return "createLocation";
}
@RequestMapping("/displayLocations")
public String displayLocations(ModelMap modelMap) {
List<Location> locations = service.getAllLocations();
modelMap.addAttribute("locations", locations);
return "displayLocations";
}
@RequestMapping("/deleteLocation")
public String deleteLocation(@RequestParam("id")int id,ModelMap modelMap) {
Location location = new Location();
location.setId(id);
service.deleteLocation(location);
List<Location> locations = service.getAllLocations();
modelMap.addAttribute("locations", locations);
return "displayLocations";
}
@RequestMapping("/showUpdate")
public String showUpdate(@RequestParam("id")int id,ModelMap modelMap) {
Optional<Location> location = service.getLocationById(id);
modelMap.addAttribute("location", location);
return "updateLocation";
}
@RequestMapping("/updateLoc")
public String updateLocation(@ModelAttribute("location") Location location,ModelMap modelMap) {
service.updateLocation(location);
List<Location> locations = service.getAllLocations();
modelMap.addAttribute("locations", locations);
return "displayLocations";
}
}
显示位置JSP页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Display Locations</title>
</head>
<body>
<h2>Locations:</h2>
<%--Table Starting from here --%>
<table>
<tr>
<th>id</th>
<th>code</th>
<th>name</th>
<th>type</th>
</tr>
<c:forEach items = "${locations}" var="location" >
<tr>
<td>${location.id}</td>
<td>${location.code}</td>
<td>${location.name}</td>
<td>${location.type}</td>
<td><a href= "deleteLocation?id=${location.id}">delete</a></td>
<td><a href= "showUpdate?id=${location.id}">edit</a></td>
</tr>
</c:forEach>
</table>
<%--Table Ending here --%>
<a href="showCreate">Add Location</a>
</body>
</html>
更新位置JSP页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Create Location</title>
</head>
<body>
<form action="updateLoc" method="post">
<pre>
id: <input type="text" name="id" value = "${location.id}" readonly/>
code: <input type="text" name="code" value = "${location.code}"/>
name: <input type="text" name="name" value = "${location.name}"/>
type: rural <input type ="radio" name="type" value ="RURAL" ${location.type=='URBAN'?'checked':'' }/>
Urban <input type ="radio" name="type" value= "URBAN" ${location.type=='RURAL'?'checked':'' }/>
<input type="submit" name="save"/>
</pre>
</form>
</body>
</html>
答案 0 :(得分:0)
仔细阅读错误消息。它表示您正在尝试访问.id
,但不是在您的位置,而是在Optional
上-不具有该属性。
检查您的代码:
@RequestMapping("/showUpdate")
public String showUpdate(@RequestParam("id")int id,ModelMap modelMap) {
Optional<Location> location = service.getLocationById(id);
modelMap.addAttribute("location", location);
return "updateLocation";
}
您不是要添加位置,而是可能包含位置的Optional。
您可以通过调用ìsPresent()
,例如
if (location.isPresent()) {
modelMap.addAttribute("location", location.get());
} else {
// ERROR?
}
有关可选的更多信息(如果您不熟悉的话:https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
答案 1 :(得分:0)
就我而言,它是由
解决的在控制器中
ModelAndView modelAndView = new ModelAndView();
Optional<Employee> employee = employeeRepository.findById( employeeDto.getId());
if (employee.isPresent()) {
modelAndView.addObject("employeeDto", employee.get());
System.out.println(employee);
} else {
System.out.println("Error Found"); // error message
}
在视图中