gpt4 book ai didi

java - JSTL 填充服务器端变量

转载 作者:行者123 更新时间:2023-11-29 09:30:07 26 4
gpt4 key购买 nike

下载 NetBeans 项目 here .文件 -> 下载

使用 JSTL 1.2我试图让我的网络应用程序记住我的输入,然后在提交表单后将其放入输入框中,但由于某种原因它不记得了。我只有 1 个 .java 类和 .jsp 文件。

PersonController.java

package controller;

public class PersonController {

private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="controller.PersonController" %>

<jsp:useBean id="personController" class="controller.PersonController" scope="session"/>
<jsp:setProperty name="personController" property="name" param="name"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>PersonController</title>
</head>
<body>
<form method="post" action="index.jsp">
<input name="name" maxlength="30" type="text" id="name" value="<c:out value="${personController.name}"/>"><br/>
<input type="submit" name="button" value="Remember my name">
</form>
</body>
</html>

错误

  1. HTTP 状态 500 -/index.jsp(行:4,列:0)useBean 类属性 controller.PersonController 的值无效。

  2. 如果它没有显示错误 #1,那么它不会在使用之前的输入发布后填充输入字段 name

最佳答案

对于您的第一个错误,我调试了您的代码并且您创建了一个参数化构造函数。您需要在 PersonController 类中添加默认构造函数。它将解决您的“useBean 类属性 Controller 的值。PersonController 无效”错误。

对于设置的值,你正在使用Controller并提供了doGet方法,但它还没有被定义为Servlet类,它是一个普通的java类。您需要使用 HttpServlet 类对其进行扩展。

谢谢。

关于java - JSTL 填充服务器端变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com