【问题标题】:java.lang.IllegalStateException: No primary constructor found for java.util.Listjava.lang.IllegalStateException:找不到 java.util.List 的主构造函数
【发布时间】:2018-05-04 21:50:47
【问题描述】:

我的项目基于spring boot、Thymeleaf、mysql、html和Jquery。

我尝试将 EntSetCharges 列表 类型的数据发布到@controller,但它不成功..它会像

那样引发错误

java.lang.IllegalStateException: 找不到主构造函数 java.util.List

以前我处理过这个错误org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'chargesName' cannot be found on null

现在我在同一页面中遇到另一个错误

这是我的完整代码...

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
 <!-- bootstrap css lib --> 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
	<!-- Bootstrap/Jquery CDN library files -->
	<script src="https://code.jquery.com/jquery-3.2.1.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
	<!-- External JQuery Script -->
	<!-- <script type="text/javascript" src="../js/formjs/setchargesform.js"></script> -->
	
<!-- Body Content Goes Here -->	
	 <div class="container">
	<form method="post" th:object="${tempEntSetChargesList}" th:action="@{/updatesetcharges}">
	<div class="table-responsive"> 
	
	<table class="table table-hover">
	<thead>
	    <tr>
      <th>Charge Name</th>
      <th>Amount</th>
      <th>Charge/Unit Type</th>
    </tr>
    </thead>
    
    <tbody>
    <tr th:each="savedcharges:${savedchargeslist}">
      <td id="colhide" hidden="true">
      <label th:value="${savedcharges.pkSetCharges}" th:field="*{pkSetCharges}"></label>
      </td>
      
      <td>
      <label th:text="${savedcharges.chargesName}" th:value="${savedcharges.chargesName}" th:field="*{chargesName}"></label>
      </td>
      
      <td>
      <input id="amt1" class="form-control" th:field="*{tempUnitAmount}">
      </td>
      
      <td>
		<select id="societyname" class="form-control" th:field="*{tempunitType}">
		<option value="perFlat" selected="selected">perFlat</option>
		<option value="perUnit">perUnit</option>
		<option value="perSqrft">perSqrft</option>
		</select>
      </td>
    </tr>
    </tbody>
    
	</table>
	</div>
	
	<button type="submit" class="btn btn-info">Submit</button>
	<button type="reset" class="btn btn-warn">Reset</button>
	</form>
	</div>
	<!-- Body content finishes -->
</body>
</html>

@RestController

@PostMapping(value="/updatesetcharges")
    public ModelAndView doUpdateSetCharges(@ModelAttribute List<EntSetCharges> tempEntSetChargesList)
    {
        ModelAndView respondResult = new ModelAndView();
        try {
            List<EntSetCharges> entSetChargesList = new ArrayList<>();
            for(EntSetCharges ent : tempEntSetChargesList)
            {
                if(ent.getTempunitType().equals("perFlat"))
                {
                    ent.setPerFlat(Integer.parseInt(ent.getTempUnitAmount()));
                }
                else if(ent.getTempunitType().equals("perUnit"))
                {
                    ent.setPerUnit(Double.parseDouble(ent.getTempUnitAmount()));
                }
                else 
                {
                    ent.setPerSqrft(Double.parseDouble(ent.getTempUnitAmount()));
                }
                entSetChargesList.add(ent);
            }

            Boolean result = serSetCharges.doUpdateSetCharges(entSetChargesList);

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return respondResult;
    }

实体

@Entity
@Table(name="setcharges")
public class EntSetCharges implements Serializable
{

/**
 * 
 */
private static final long serialVersionUID = 3827507518731160293L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="pksetcharges")
private int pkSetCharges;

@Column(nullable=false)
private String chargesName;
@ColumnDefault("0")
private int perFlat;
@ColumnDefault("0")
private double perUnit;
@ColumnDefault("0")
private double perSqrft;

@Version
private int version;
private Boolean is_active;
private String created_by;
private Date created_ts;
private String modified_by;
private Date modified_ts;
private String approved_by;
private Date approved_ts;

@Transient
private String tempunitType;

@Transient
private String tempUnitAmount;

@Transient
private LogEntSetCharges tempLogEntSetCharges;

代码更新 V1 在这里我做了一些更改,它正在访问控制器页面,但是对象是 EMPTY [] ..这是图像证明 @RestController

@PostMapping(value="/updatesetcharges")
    public ModelAndView doUpdateSetCharges(@ModelAttribute WrpSetCharges tempEntSetChargesList)
    {
        ModelAndView respondResult = new ModelAndView();
        try {
            List<EntSetCharges> entSetChargesList = new ArrayList<>();

            for(EntSetCharges ent : tempEntSetChargesList.getTempEntSetChargesList())
            {   //logics goes here

在 HTML 中

<form id="setchargesformid" method="post" th:object="${tempEntSetChargesList}" th:action="@{/updatesetcharges}">

包装类

public class WrpSetCharges {

private List<EntSetCharges> tempEntSetChargesList = new ArrayList<>();

public List<EntSetCharges> getTempEntSetChargesList() {
    return tempEntSetChargesList;
}

public void setTempEntSetChargesList(List<EntSetCharges> tempEntSetChargesList) {
    this.tempEntSetChargesList = tempEntSetChargesList;
}

}

【问题讨论】:

    标签: hibernate spring-boot spring-data-jpa thymeleaf


    【解决方案1】:

    将您的列表放入包装类中,并将该类用作您的 ModelAttributes

    class ListWrapper{
         List<EntSetCharges> tempEntSetChargesList;
    
         public List<EntSetCharges> getTempEntSetChargesList() {
             return tempEntSetChargesList;
         }
    
         public void setTempEntSetChargesList(List<EntSetCharges> tempEntSetChargesList) {
             this.tempEntSetChargesList = tempEntSetChargesList;
         }
     }
    

    并在此处搜索如何在百里香中绑定对象列表。 前任。 how-to-bind-an-object-list-with-thymeleaf 下面是列表中绑定项目的示例。

    <tbody>
    <tr th:each="currentClient, stat : *{clientList}">
       <td>
          <input type="checkbox" th:field="*{clientList[__${stat.index}__].selected}" />
          <input type="hidden" th:field="*{clientList[__${stat.index}__].clientID}" />
          <input type="hidden" th:field="*{clientList[__${stat.index}__].ipAddress}" />
          <input type="hidden" th:field="*{clientList[__${stat.index}__].description}" />
       </td>
       <td th:text="${currentClient.getClientID()}"></td>
       <td th:text="${currentClient.getIpAddress()}"></td>
       <td th:text="${currentClient.getDescription()}"></td>               
    </tr>
    </tbody>
    

    【讨论】:

    • 你能解释一下原因吗?
    • Spring 将调用 ModelAttributes 类的默认无参数构造函数,在您的情况下,您使用没有构造函数的 List 接口。并且使用 List 或其任何实现作为直接请求模型并不是一个好主意。
    • 现在我创建了一个包装类,现在它可以访问控制器,但是对象的内部是“EMPTY”[]...我需要在 HTML 中做任何更改吗?
    • 表示你列表中的item没有正确绑定,能不能显示你更新的视图模板
    • 使用输入字段绑定列表中的项目,但如果您不想在视图中显示输入字段,可以将其设置为隐藏。参见链接,例如 stackoverflow.com/questions/36500731/…
    【解决方案2】:

    就我而言,我使用@RequestBody 将输入映射到我的模型。所以会是

    @PostMapping(value="/updatesetcharges")
    public ModelAndView doUpdateSetCharges(@RequestBody List<EntSetCharges> tempEntSetChargesList){ 
    // Do your stuff and dont forget return
       return new ModelAndView();
    }
    

    别忘了在你的实体中添加空构造函数。

    【讨论】:

    • 您找到解决方案了吗?如果你这样做了,请更新。谢谢
    猜你喜欢
    • 2019-07-06
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2010-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-17
    相关资源
    最近更新 更多