[英]Spring Boot: EL is not functioning in JSP
我是 Spring Boot 的初学者,练习在一个 JSP 页面上创建提交表单并将输入的详细信息返回到另一个 JSP 页面。 但是 ${ 表达式 } 不能正常工作。 它没有给出来自控制器的传递值,而是按原样显示表达式。
我的代码:
客户入口.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>
保存的详细信息.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>
客户表单控制器
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 页面中包含 jstl 库,把这一行放在你的 JSP 页面/页面中,它应该可以工作。
您可能希望在以下代码中更改前缀属性(您可以选择任何适合您的内容)
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
我有答案
我已将此添加到 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>
它开始工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.