1. 程式人生 > 其它 >jsp使用EL表示式取不到後端ModelAndView傳來的Object值

jsp使用EL表示式取不到後端ModelAndView傳來的Object值

技術標籤:java

樣例如下:

後臺程式碼

    @RequestMapping("/update")
    public ModelAndView update() {
        /**
         * Model: 封裝資料
         * View: 展示資料
         */
        ModelAndView modelAndView = new ModelAndView();
        // 設定模型資料
        modelAndView.addObject("username", "lidantao");
        // 設定檢視名稱
        modelAndView.setViewName("success");
        return modelAndView;
    }

jsp程式碼

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>
    fasf
</title>
</head>
<body>

<h1>${username}</h1>
</body>
</html>

瀏覽器訪問結果

解決:

原因是jsp預設設定禁止使用EL表示式,所以開啟即可

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>

把isELIgnored設定成false,表示為,不忽略EL表示式。

修改後,再次訪問結果如下: