首页 > 解决方案 > 在类型 [java.util.Optional] 上找不到属性 [id]

问题描述

我正在尝试使用 spring boot 执行 crud 操作,但我是新手。我已成功执行删除和创建部分。当我尝试编辑我的字段时遇到的问题。我使用 MYSQL 作为我的数据库。我在问题标题中提到了错误。任何帮助解决它,请检查我的逻辑我认为我的逻辑在 /showUpdate 方法中是错误的。当我按下编辑按钮时,它不会让我编辑页面并向我抛出这个错误。

我的控制器类粘贴在下面:

我遇到的实际错误的快照

    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>

标签: spring-boot

解决方案


仔细阅读错误信息。它说您正在尝试访问.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。您可以通过调用来检查 Optional 是否持有值ìsPresent(),例如

if (location.isPresent()) {
    modelMap.addAttribute("location", location.get());
} else {
    // ERROR?
}

更多关于可选,如果你不熟悉:https ://docs.oracle.com/javase/8/docs/api/java/util/Optional.html


推荐阅读