【问题标题】:Spring Boot: EL is not functioning in JSPSpring Boot:EL 在 JSP 中不起作用
【发布时间】:2020-12-31 23:56:22
【问题描述】:

我是 Spring Boot 的初学者,练习在一个 JSP 页面上创建提交表单并将输入的详细信息返回到另一个 JSP 页面。但 ${ 表达式 } 无法正常工作。它没有给出从控制器传递的值,而是按原样显示表达式。

我的代码:

CustomersEntry.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Customer Portal</title>
</head>
<body>
<form method="post" action="saveDetails">
    Enter Customer ID: <input type="text" name="cid"><br>
    Enter Customer Name: <input type="text" name="cname"><br>
    Enter Customer E-mail: <input type="text" name="cemail"><br>
    <input type="submit" name="submit"><br>
</form>
</body>
</html>

savedDetails.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Customer Details</title>
</head>
<body>
<h2>Following customer details have been saved:</h2><br>
<h4>Customer ID: {cid}</h4>
<h4>Customer Name: {cname}</h4>
<h4>Customer E-mail: {cemail}</h4>

<form action="/">
<input type="submit" value="Add more Customers"/>
</form>
</body>
</html>

CustomerFormController

package com.ashish.demo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;


@Controller
public class CustomerFormController {

    @GetMapping("/")
    public String getForm() {
        return "CustomersEntry";
    }
    
    @PostMapping("/saveDetails")
    public String saveDetails(@RequestParam("cid") String cid,
            @RequestParam("cname") String cname,
            @RequestParam("cemail") String cemail, ModelMap m) {
        m.addAttribute("cid", cid).addAttribute("cname", cname).addAttribute("cemail", cemail);
        
        return "savedDetails";
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>FormSubmission</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>FormSubmission</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>14</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.37</version>
</dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我正在使用 STS 4、Java 14。 我在谷歌上搜索了很多,但没有任何帮助。 根据其他人提供的解决方案,我也尝试过一一包含依赖项,但没有帮助。

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.37</version>
</dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope> <!-- with or without this and with runtime as well. -->
        </dependency>
        <!-- jstl for jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

【问题讨论】:

  • 我建议不要将 JSP 与嵌入式容器一起使用,因为存在限制。其中之一是它需要 WAR,并且不能在带有 tomcat 的 JAR 中工作。
  • @M.Deinum 它从我参考的地方开始工作,在他们的最后工作正常。
  • 我没有说过它不会工作,我只注意到将 JSP 与嵌入式容器(至少是 Tomcat)一起使用是有局限性的。这些也记录在 Spring Boot 参考指南中。
  • @M.Deinum 我同意你的观点,它有局限性,但你说——不能在带有 tomcat 的 JAR 中工作。但它有效。
  • 由于 JSP a 的加载方式,它在较旧的 tomcat 版本中没有,可能是它在较新的版本中,但从历史上看它没有。尽管如此,仍然存在一些限制,强烈建议使用其他模板引擎。

标签: java spring-boot maven jsp


【解决方案1】:

您的 JSP 页面中没有包含 jstl 库 将此行放在您的 JSP 页面中,它应该可以工作。

您可能想在下面的代码中更改前缀属性(您可以选择任何适合您的东西)

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

【讨论】:

  • 通过这样做,我得到了内部服务器错误,状态 = 500,org.apache.jasper.JasperException:绝对 uri:[java.sun.com/jsp/jstl/core] 无法在 web.xml 或 jar 文件中解析与此应用程序一起部署。
【解决方案2】:

我有答案

我已将此添加到 JSP 页面:

<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

到 pom.xml 文件。

更新了 savedDetails.jsp --

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isELIgnored="false"%>
    <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Customer Details</title>
</head>
<body>
<h2>Following customer details have been saved:</h2><br>
<h4>Customer ID: <c:out value="${cid}"/></h4>
<h4>Customer Name:<c:out value="${cname}"/></h4>
<h4>Customer E-mail: <c:out value="${cemail}"/></h4>



<form action="/">
<input type="submit" value="Add more Customers"/>
</form>
</body>
</html>

它开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 2015-03-30
    • 1970-01-01
    相关资源
    最近更新 更多