thymeleaf模板引擎调用java后台方法出错

  • 主题: thymeleaf模板引擎调用java后台方法出错
  • 作者: Rocky_Yu
  • 时间: 20200402
  • 分类: 9.1-003
  • Version: 1.0.0

文章目录

  • thymeleaf模板引擎调用java后台方法出错
    • 问题描述
    • 问题原因
    • 解决方法


thymeleaf模板引擎调用java后台方法出错


问题描述

在thymeleaf中调用java后台的方法时在后台出现如下错误:

Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "commons.random(5, '.png')" (template: "/admin/test.html" - line 2, col 54)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1011E: Method call: Attempted to call method random(java.lang.Integer,java.lang.String) on null context object

前台源码为test.html:


 <html lang="en" xmlns:th="http://www.thymeleaf.org" th:with="pic=${commons.random(5, '.java')}"  >
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width,initial-scale=1"/>
    <title>用户登录title>
head>
<body>
<div style="margin: 0 auto; padding-bottom: 0%; padding-top: 7.5%; width: 380px;">
    <div>
        <div>
            <form method="post" id="loginForm">
                <div class="form-group">
                    <div class="col-xs-12">
                        <input  th:value="@{${pic}}" name="username" type="text"  placeholder="账号:"/>
                    div>
                div>
                <div class="form-group">
                    <div class="col-xs-12">
                        <input value="" name="password" type="password" placeholder="密码:"/>
                    div>
                div>
            form>
        div>
    div>
div>
body>
html>

可以看到我们再代码的刚开始调用了一个commons.random(5, '.java')的后台方法。

后台源码为:

package com.design;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.design.utils.Commons;

@RestController
public class Test {
	
	@GetMapping(value = "/test")
    public ModelAndView test(HttpServletRequest req) {
		
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", "tom");        
        mv.setViewName("/admin/test.html");
        return mv;
        }

}

后台代码是一个由@RestController注解的类,里面有个test请求的方法,现在我们启动springboot程序后,在浏览器中访问这个请求就会报上面的错误。

问题原因

从报错可以看到,是因为前台没有我们调用的commons这个实例对象不存在。所以会报错!

解决方法

解决方法就是在后台生成这这对象的实例,并返回给前台。

解决后的后台代码:

package com.design;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.design.utils.Commons;

@RestController
public class Test {
	
	@GetMapping(value = "/test")
    public ModelAndView test(HttpServletRequest req) {
		Commons commons = new Commons();  //添加
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", "tom");
        mv.addObject("commons", commons);  //添加
        mv.setViewName("/admin/test.html");
        return mv;
        }

}

其实就是在后台添加了一个前台要调用类的实例,然后将这个实例对象存到到Request中,返回给前台,然后前台就可以使用这个实例中的方法了。


欢迎各位批评指正!

你可能感兴趣的:(编程杂记)