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

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

问题描述


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

我的场景
用于获取列表迭代并显示HTML表格第1列中的数据,并发布从动态输入字段获取的数据(html-chargesName和输入数量),以及将它保存到相关的列中,所以它需要到达弹簧启动@RestController。



但是我得到了Thymeleaf ERROR,如


org.springframework.expression.spel.SpelEvaluationException:EL1007E:
无法在null上找到属性或字段'chargesName'

以下是完整代码...

  / ** *这是用于设置收费表单* / $(document).ready(function(){ $(select)。change(function(){var amttype = $(this).val(); if(amttype == 1){$(#amt1)。show(); $( #AMT2)隐藏()。 $( #AMT3)隐藏()。 } else if(amttype == 2){$(#amt1)。hide(); $( #AMT2)显示()。 $( #AMT3)隐藏()。 } else if(amttype == 3){$(#amt1)。hide(); $( #AMT2)隐藏()。 $( #AMT3)显示()。 } else {$(#amt1)。hide(); $( #AMT2)隐藏()。 $( #AMT3)隐藏()。 }}); };  

<!DOCTYPE html> < html xmlns:th =http://www.thymeleaf.org> < HEAD> < meta charset =ISO-8859-1> < title>在此插入标题< / title> <! - bootstrap css lib - > < link rel =stylesheethref =https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css> < /头> <身体GT; <! - 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 / javascriptsrc =../ js / formjs / setchargesform.js>< / script> <! - 正文内容转到此处 - > < div class =container> < form method =postth:object =$ {entSetCharges}th:action =@ {/ updatesetcharges}> < div class =table-responsive> < table class =table table-hover> < THEAD> < TR> < th> Charge Name< / th> <的第i;量及LT; /第> < 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 =amt1class =form-controlth:field =* {perFlat}> < input id =amt2class =form-controlth:field =* {perUnit}> < input id =amt3class =form-controlth:field =* {perSqrft}> < / TD> < TD> < select id =societynameclass =form-controlth:field =* {tempunitType}> < option value =1selected =selected> perFlat< / option> < option value =2> perUnit< / option> < option value =3> perSqrft< / option> < /选择> < / TD> < / TR> < / tbody的> < /表> < / DIV> < button type =submitclass =btn btn-info>提交< / button> < button type =resetclass =btn btn-warn>重置< / button> < /形式> < / DIV> <! - 正文内容完成 - > < /体> < / html>


$ b @RestController

  @GetMapping(value =/ setchargeslist)
public ModelAndView doGetSetchargesList()
{
列表< EntSetCharges> listCharges = new ArrayList<>();
ModelAndView respondResult = new ModelAndView(SetCharges);
尝试{
/ *获取设置费用列表* /
listCharges = serSetCharges.doGetSetChargesList();
if(listCharges!= null)
{
respondResult.addObject(savedchargeslist,listCharges);
}
else
{
respondResult.addObject(savedchargeslist,new ArrayList<>));
}

} catch(Exception e){
// TODO:处理异常
}
return respondResult;
}

ENTITY $ b

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

@版本
private int version;
私有布尔is_active;
私人字符串created_by;
私人日期created_ts;
private String modified_by;
私人日期modified_ts;
私人字符串approved_by;
私人日期approved_ts;


解决方案

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

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

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



解决方案:
$ b


  • 将您的 th:field =* {chargesName}更改为 $ {savedcharges.chargesName} 表达式以访问您的对象请尝试添加 respondResult.addObject(entSetCharges,new EntSetCharges());

  • $ c>高于你的回报线。您的视图不会自动为您创建此对象。

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天全站免登陆