如何解决Spring Boot:EL在JSP中不起作用
我是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。 我在Google上搜索了很多,但没有任何帮助。 我也尝试按照其他人给出的解决方案,一个接一个地遵循依赖关系,但没有帮助。
<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文件。
更新了saveedDetails.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>
它开始工作。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。