Spring Boot:JSP中的EL未正常工作

huangapple go评论69阅读模式
英文:

Spring Boot: EL is not functioning in JSP

问题

I am a beginner in Spring Boot and I'm practicing creating a submission form on one JSP page and returning the entered details to another JSP page. However, the ${expression} is not working correctly. Instead of displaying the passed values from the controller, it's showing the expression as it is.

Here's your code:

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.java

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

I'm using STS 4 and Java 14. I've searched extensively on Google, but nothing seems to help. I've also tried including the following dependencies one by one as suggested by others, but it didn't help:

<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>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

If you need further assistance, please let me know.

英文:

I am beginner to Spring Boot, practising creating a Submission Form on one JSP page and returning the entered details to another JSP page. but the ${ expression } is not working properly. Instead of giving passed values from controller it is showing the expression as it is.

My Code:

CustomersEntry.jsp

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

savedDetails.jsp

&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=ISO-8859-1&quot;
pageEncoding=&quot;ISO-8859-1&quot; isELIgnored=&quot;false&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;ISO-8859-1&quot;&gt;
&lt;title&gt;Customer Details&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;Following customer details have been saved:&lt;/h2&gt;&lt;br&gt;
&lt;h4&gt;Customer ID: {cid}&lt;/h4&gt;
&lt;h4&gt;Customer Name: {cname}&lt;/h4&gt;
&lt;h4&gt;Customer E-mail: {cemail}&lt;/h4&gt;
&lt;form action=&quot;/&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Add more Customers&quot;/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

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(&quot;/&quot;)
public String getForm() {
return &quot;CustomersEntry&quot;;
}
@PostMapping(&quot;/saveDetails&quot;)
public String saveDetails(@RequestParam(&quot;cid&quot;) String cid,
@RequestParam(&quot;cname&quot;) String cname,
@RequestParam(&quot;cemail&quot;) String cemail, ModelMap m) {
m.addAttribute(&quot;cid&quot;, cid).addAttribute(&quot;cname&quot;, cname).addAttribute(&quot;cemail&quot;, cemail);
return &quot;savedDetails&quot;;
}
}

pom.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;parent&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
&lt;version&gt;2.3.3.RELEASE&lt;/version&gt;
&lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
&lt;/parent&gt;
&lt;groupId&gt;com.example&lt;/groupId&gt;
&lt;artifactId&gt;FormSubmission&lt;/artifactId&gt;
&lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
&lt;name&gt;FormSubmission&lt;/name&gt;
&lt;description&gt;Demo project for Spring Boot&lt;/description&gt;
&lt;properties&gt;
&lt;java.version&gt;14&lt;/java.version&gt;
&lt;/properties&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.tomcat&lt;/groupId&gt;
&lt;artifactId&gt;tomcat-jasper&lt;/artifactId&gt;
&lt;version&gt;9.0.37&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.h2database&lt;/groupId&gt;
&lt;artifactId&gt;h2&lt;/artifactId&gt;
&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
&lt;scope&gt;test&lt;/scope&gt;
&lt;exclusions&gt;
&lt;exclusion&gt;
&lt;groupId&gt;org.junit.vintage&lt;/groupId&gt;
&lt;artifactId&gt;junit-vintage-engine&lt;/artifactId&gt;
&lt;/exclusion&gt;
&lt;/exclusions&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
&lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

I am using STS 4, Java 14.
I have searched a lot over google but nothing helps.
I've tried including following dependencies one by one as well, as per solution given by other people but no help.

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

答案1

得分: 0

你的 JSP 页面中没有包含 jstl 库
将这行代码放在你的 JSP 页面/页面中,应该可以工作。

你可能想要在下面的代码中更改 prefix 属性(你可以选择任何对你有效的内容)

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

You haven't included jstl library in your JSP page
put this line in your JSP page/pages and it should work.

You may want to change prefix attribute in the below code(you can choose anything which works for you)

&lt;%@ taglib prefix = &quot;c&quot; uri = &quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;

答案2

得分: 0

我已经得到了答案

我已经将这部分添加到了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>以下客户详情已保存:</h2><br>
<h4>客户ID: <c:out value="${cid}"/></h4>
<h4>客户姓名:<c:out value="${cname}"/></h4>
<h4>客户电子邮件: <c:out value="${cemail}"/></h4>
<form action="/">
<input type="submit" value="添加更多客户"/>
</form>
</body>
</html>

然后它开始工作。

英文:

I have got the answer

I have added this to the JSP page:

&lt;%@ taglib prefix = &quot;c&quot; uri = &quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;

And

&lt;dependency&gt;
&lt;groupId&gt;javax.servlet&lt;/groupId&gt;
&lt;artifactId&gt;jstl&lt;/artifactId&gt;
&lt;version&gt;1.2&lt;/version&gt;
&lt;/dependency&gt;

to the pom.xml file.

updated savedDetails.jsp --

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

And it starts working.

huangapple
  • 本文由 发表于 2020年9月14日 17:07:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63881289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定