org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“chargesName" [英] org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'chargesName' cannot be found on null

查看:38
本文介绍了org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“chargesName"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

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

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

但是我得到了类似 Thymeleaf 的错误

<块引用>

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

这里是完整的代码...

/*** 这是固定收费表*/$(document).ready(function() {$("select").change(function(){var amttype = $(this).val();如果(amttype == 1){$("#amt1").show();$("#amt2").hide();$("#amt3").hide();}否则如果(amttype == 2){$("#amt1").hide();$("#amt2").show();$("#amt3").hide();}否则如果(amttype == 3){$("#amt1").hide();$("#amt2").hide();$("#amt3").show();}别的 {$("#amt1").hide();$("#amt2").hide();$("#amt3").hide();}});});

 <html xmlns:th="http://www.thymeleaf.org"><头><meta charset="ISO-8859-1"><title>在此处插入标题</title><!-- 引导 css 库 --><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"><身体><!-- Bootstrap/Jquery CDN 库文件--><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><!-- 外部 JQuery 脚本 --><script type="text/javascript" src="../js/formjs/setchargesform.js"></script><!-- 正文内容在这里--><div class="容器"><form method="post" th:object="${entSetCharges}" th:action="@{/updatesetcharges}"><div class="table-responsive"><table class="table table-hover"><头><tr><th>Charge Name</th><th>数量</th><th>费用/单位类型</th></tr></thead><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></选择></td></tr></tbody>

<button type="submit" class="btn btn-info">提交</button><button type="reset" class="btn btn-warn">重置</button></表单>

<!-- 正文内容结束--></html>

@RestController

@GetMapping(value="/setchargeslist")公共 ModelAndView doGetSetchargesList(){列表listCharges = new ArrayList<>();ModelAndView respondResult = new ModelAndView("SetCharges");尝试 {/*获取设置的费用清单*/listCharges = serSetCharges.doGetSetChargesList();if(listCharges!=null){responseResult.addObject("savedchargeslist",listCharges);}别的{responseResult.addObject("savedchargeslist",new ArrayList<>());}} 捕获(异常 e){//TODO: 处理异常}返回响应结果;}

实体

@Entity@Table(name="setcharges")公共类 EntSetCharges 实现了可序列化{/****/私有静态最终长serialVersionUID = 3827507518731160293L;@ID@GeneratedValue(策略=GenerationType.IDENTITY)@Column(name="pksetcharges")私人 int pkSetCharges;@Column(可为空=假)私人字符串费用名称;@ColumnDefault("0")私人 int perFlat;@ColumnDefault("0")每单位私人双倍;@ColumnDefault("0")每平方英尺私人双人房;@版本私有 int 版本;私人布尔 is_active;私人字符串 created_by;私人日期 created_ts;私人字符串 modified_by;私人日期 modified_ts;私人字符串批准_通过;私人日期批准_ts;

解决方案

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

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

解决方案:

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

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

My project based on spring boot,Thymeleaf,mysql,html and Jquery.

My scenario is to Get the List iterate and show the data in the table 1st column in the HTML and post the data (html-chargesName and input amount) which is getting from the dynamic input fields and save it into related columns,so it will need to the hit spring boot @RestController.

But I got Thymeleaf ERROR like

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'chargesName' cannot be found on null

Here is Full Code...

/**
 * 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

@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;

解决方案

You used the form at the top of your html page:

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

The th:field="*{chargesName} is bound to the object/bean inside your th:object="${entSetCharges}. Meaning that you are searching for a chargesName field inside your entSetCharges object which does not exist or is null.

Solution:

  • Maybe change your th:field="*{chargesName}" to ${savedcharges.chargesName} expression to access your object's field.

  • Try adding respondResult.addObject("entSetCharges",new EntSetCharges()); above your return line. Your view doesn't automatically create this object for you.

这篇关于org.springframework.expression.spel.SpelEvaluationException:EL1007E:在 null 上找不到属性或字段“chargesName"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆