【问题标题】:org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'chargesName' cannot be found on nullorg.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“chargesName”
【发布时间】:2018-05-04 12:26:12
【问题描述】:

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

我的场景 是获取列表迭代并在 HTML 的表第一列中显示数据并发布从动态输入字段获取的数据(html-chargesName 和输入金额)并将其保存到相关列中,因此需要命中弹簧靴@RestController。

但我得到了 Thymeleaf 错误,例如

org.springframework.expression.spel.SpelEvaluationException: EL1007E: 在 null 上找不到属性或字段“chargesName”

这是完整代码...

/**
 * This is for set charges form
 */

$(document).ready(function() {
	 $("select").change(function() 
			 {
		    var amttype = $(this).val();
		    if (amttype == 1) 
		    {
		      $("#amt1").show();
		      $("#amt2").hide();
		      $("#amt3").hide();
		      
		    } 
		    else if (amttype == 2) 
		    {
		    	  $("#amt1").hide();
			      $("#amt2").show();
			      $("#amt3").hide();
		    } 
		    else if (amttype == 3) 
		    {
		    	  $("#amt1").hide();
			      $("#amt2").hide();
			      $("#amt3").show();
		    }
		    else {
		    	  $("#amt1").hide();
			      $("#amt2").hide();
			      $("#amt3").hide();
		    }
		    
		  });
	
});
    <!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="${entSetCharges}" 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>
          <label th:text="${savedcharges.chargesName}" th:value="${savedcharges.chargesName}" th:field="*{chargesName}"></label>
          </td>
          
          <td>
          <input id="amt1" class="form-control" th:field="*{perFlat}">
          <input id="amt2" class="form-control" th:field="*{perUnit}">
          <input id="amt3" class="form-control" th:field="*{perSqrft}">
          </td>
          
          <td>
    		<select id="societyname" class="form-control" th:field="*{tempunitType}">
    		<option value="1" selected="selected">perFlat</option>
    		<option value="2">perUnit</option>
    		<option value="3">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

@GetMapping(value="/setchargeslist")
    public ModelAndView doGetSetchargesList()
    {
        List<EntSetCharges> listCharges = new ArrayList<>();
        ModelAndView respondResult = new ModelAndView("SetCharges");
        try {
            /*Get the set charges list*/
            listCharges = serSetCharges.doGetSetChargesList();
            if(listCharges!=null)
            {
            respondResult.addObject("savedchargeslist",listCharges);
            }
            else
            {
                respondResult.addObject("savedchargeslist",new ArrayList<>());
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
        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;

【问题讨论】:

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


    【解决方案1】:

    您使用了 html 页面顶部的表单:

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

    th:field="*{chargesName} 绑定到 th:object="${entSetCharges} 内的对象/bean。这意味着您正在 entSetCharges 对象内搜索不存在或为空的 chargesName 字段。

    解决方案:

    • 也许将您的 th:field="*{chargesName}" 更改为 ${savedcharges.chargesName} 表达式以访问您对象的字段。

    • 尝试在您的退货行上方添加respondResult.addObject("entSetCharges",new EntSetCharges());。您的视图不会自动为您创建此对象。

    【讨论】:

    • savedcharges.chargesName 是从 EntAssignCharges 获取的,但我需要将其存储到 EntSetCharges 中,不幸的是两者都有相同的列名“.chargesName”..
    • 错误:: org.thymeleaf.exceptions.TemplateProcessingException: 评估 SpringEL 表达式的异常:“entSetCharges.chargesName”(模板:“SetCharges” - 第 33 行,第 91 列)
    • 错误 2:原因:org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“chargesName”
    • 您可以尝试将新的 EntSetCharges 对象添加到您的模型吗?像这样:respondResult.addObject("entSetCharges",new EntSetCharges()); 告诉我发生了什么
    • 我的意思是在您的退货声明之前添加respondResult.addObject("entSetCharges",new EntSetCharges());
    【解决方案2】:

    我像 respondResult.addObject("entSetCharges",new EntSetCharges());

    一样初始化实体对象
    @GetMapping(value="/setchargeslist")
        public ModelAndView doGetSetchargesList()
        {
            List<EntSetCharges> listCharges = new ArrayList<>();
            ModelAndView respondResult = new ModelAndView("SetCharges");
            try {
                /*Get the set charges list*/
                listCharges = serSetCharges.doGetSetChargesList();
                if(listCharges!=null)
                {
                respondResult.addObject("savedchargeslist",listCharges);
                respondResult.addObject("entSetCharges",new EntSetCharges());
                }
                else
                {
                    respondResult.addObject("savedchargeslist",new ArrayList<>());
                }
    
            } catch (Exception e) {
                // TODO: handle exception
            }
            return respondResult;
        }
    

    【讨论】:

      猜你喜欢
      • 2021-08-22
      • 1970-01-01
      • 2018-11-25
      • 2018-03-31
      • 2018-08-07
      • 2016-11-16
      • 1970-01-01
      • 2018-01-09
      • 2019-04-27
      相关资源
      最近更新 更多