gpt4 book ai didi

java - 名称以前未按照 JSP.5.3 引入

转载 作者:行者123 更新时间:2023-11-29 07:52:14 32 4
gpt4 key购买 nike

出于个人原因,我需要记忆一下 jsp 上的一些事情 :) 我有一个简单的登录页面:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</heaf>
<body>
<form action="LoginServlet.do" method="POST">
<p>
First name: <input type="text" size="20" name="first">
</p>
<p>
Last name: <input type="text" size="20" name="last">
</p>
<input type="submit" value="send">
</form>
</body>
</html>

bean 类:

package web.model;

public class User {
private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}
}

我从表单收集数据,创建用户实例,将其设置为请求属性并将请求转发到 result.jsp 页面:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
User user = new User();
user.setFirstName(request.getParameter("first"));
user.setLastName(request.getParameter("last"));
request.setAttribute("user", user);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}

页面代码:

<%@ page 
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
import="web.utils.Utils, web.model.User"
%>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
<%= ((User) request.getAttribute("user")).getFirstName() %>
<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="user" name="firstName" />
</body>
</html>

小脚本代码工作正常,堆栈跟踪:

SEVERE: Servlet /Web threw load() exception
org.apache.jasper.JasperException: file:/result.jsp(15,0) jsp:getProperty for bean with name 'firstName'. Name was not previously introduced as per JSP.5.3
at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1110)
at org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1125)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
at org.apache.jasper.compiler.Generator.generate(Generator.java:3517)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:138)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:722)

提前致谢

最佳答案

请更改usebean

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="firstName" name="user" />

在 getProperty 中,名称是 usebean 的 id 值。

检查这个http://profesores.elo.utfsm.cl/~agv/elo330/2s03/projects/Tomcat/doc/1.2/syntaxref1216.html#8856

关于java - 名称以前未按照 JSP.5.3 引入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280203/

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