0

我有一个如下代码,其中包含变量名称和变量的 Javabean

package Model;

public class SampleServ 
{
    private String Name;

    public String getName() 
    {
        return Name;
    }

    public void setName(String name) 
    {
        Name = name;
    }
}

我创建了一个 JSP 文件并使用 Javabean 将值设置如下

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="Model.SampleServ" %>
<!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>Insert title here</title>
</head>
<body>
<jsp:useBean id="objUsers" class="Model.SampleServ"></jsp:useBean>
<jsp:setProperty property="Name" name="objUsers" value="Mugil"/>
<jsp:getProperty property="Name" name="objUsers"/>
</body>
</html>

现在页面显示以下消息如下

Feb 12, 2013 12:32:28 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/JSTL] threw exception [Cannot find any information on property 'Name' in a bean of type 'Model.SampleServ'] with root cause
org.apache.jasper.JasperException: Cannot find any information on property 'Name' in a bean of type 'Model.SampleServ'
    at org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibrary.java:797)
    at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1062)
    at org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1126)
    at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
    at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
    at org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
    at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
    at org.apache.jasper.compiler.Generator.generate(Generator.java:3474)
    at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:249)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:372)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:352)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:339)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:594)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:344)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
c

为什么会发生这种情况。

谢谢回复

4

2 回答 2

1

您的属性名称是“名称”,而不是“属性名称”

顺便说一句,您没有遵循标准的 java 命名约定。字段应该用 camelCase 定义,并且应该是私有的。即private String name;而不是public String Name;

于 2013-02-12T06:50:06.660 回答
0

尝试阅读该printStackTrace消息,它清楚地表明Cannot find any information on property 'propertyName' in a bean of type 'Model.SampleServ'

在这一行<jsp:setProperty property="propertyName" name="objUsers" value="Mugil"/> 中,您使用了property =" propertyName" 这应该是您在SampleServ中可用的属性名称,因此请使用property=¨Name¨并尝试遵循bean 属性的命名约定

编辑

SampleServ.java

public class SampleServ 
{
    private String name;

    public String getName() 
    {
        return name;
    }

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

在您的jsp页面中替换<jsp:setProperty property="propertyName" name="objUsers" value="Mugil"/>

<jsp:setProperty property="name" name="objUsers" value="Mugil"/>
于 2013-02-12T06:51:27.573 回答