java - 无法使用 ${param.xxx} 使用 HTML 表单访问 JSP 参数

标签 java maven jsp tomcat intellij-idea

我正在关注 Udemy 上解释 JSP 和 Servlet 的教程: https://www.udemy.com/jsp-tutorial

本教程使用 Eclipse + Tomcat 服务器。

因为我是 IntelliJ 和 Maven 用户,所以我想使用这两者来设置我的环境。所以我从以下原型(prototype)创建了一个 Maven 项目:“org.apache.maven.archetypes:maven-archetype-webapp”并将我的 POM 配置如下:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>SomeGroupId</groupId>
  <artifactId>HelloWorldJavaServerPages</artifactId>
  <packaging>war</packaging>
  <version>1.0</version>
  <name>HelloWorldJavaServerPages Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.1</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>HelloWorldJavaServerPages</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <url>http://localhost:8080/manager/text</url>
          <server>Tomcat85Localhost</server>
          <username>admin</username>
          <password>admin</password>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

按照此处所述设置我的其他属性: Tomcat 8 Maven Plugin for Java 8

我能够构建我的项目并使用 tomcat 插件部署到 tomcat 服务器(或手动将 war 文件拖到 webapps 文件夹中)。

问题是当我有以下两个文件时:

学生表格.html

<html>
<head><title>Student Registration Form</title></head>
<body>
<form action="student-response.jsp">
    First name: <input type="text" name="firstName" />
    <br/>
    Last name: <input type="text" name="lastName" />
    <input type="submit" value="Submit" />
</form>
</body>
</html>

学生响应.jsp

<html>
<head><title>Student Confirmation Title</title></head>
<body>
    The student is confirmed: <%= request.getParameter("firstName")%> ${param.lastName}
</body>
</html>

request.getParameter 方法有效,但 ${param.lastName} 无效,它只是在浏览器中显示为纯文本:${param.lastName}。

使用 Eclipse(没有 Maven)它确实适用于两者,所以我想知道我在这里做的实际上不同/错误的地方以及为什么它不起作用。

预先感谢您的帮助。

最佳答案

一旦我发现大括号之间的引用被称为“表达式语言”,我决定在 stackoverflow 上再次搜索并找到这个: Expression Language in JSP not working

按照上面链接中的描述更改 web.xml 使其工作。

谢谢大家阅读我的问题,其实答案很简单。

关于java - 无法使用 ${param.xxx} 使用 HTML 表单访问 JSP 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47349084/

相关文章:

maven - maven 不使用 pom.xml 中指定的存储库?

javascript - NetBeans 调试突然停止工作

java - 使用 3 个循环时如何在单独的行中打印

java - 在 Android 的单元测试中覆盖 SLF4J 实现

java - 使用 JSF 和托管 Bean 将列表框的内容绑定(bind)到另一个列表框中的选定项

java - jsp <form :errors> 中没有消息显示

java - 在jsp页面中插入的特殊字符和中文字体在spring Controller 中不起作用

java - 如何在Java中为单向链表节点高效实现hashCode()?

java - 使用二进制文字时 int 的范围

java - 如何在 Maven 的 WAR 项目中指定 EJB 引用?