英文:
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
<%@ 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>
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.
<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>
答案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)
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
答案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:
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
And
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
to the pom.xml file.
updated 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>
And it starts working.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论