【发布时间】: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