以前没有按照JSP.5.3引入名称

时间:2013-11-29 07:10:15

标签: java jsp usebean

由于个人原因,我需要回忆一下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;
    }
}

我从表单中收集数据,创建User实例,将其设置为请求属性并将请求转发到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>

scriptlet代码正常工作,stacktrace:

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)

提前致谢

3 个答案:

答案 0 :(得分:3)

请更改usebean

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

在getProperty中,name是usebean的id值。

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

答案 1 :(得分:1)

您的名称和属性属性不匹配。 name是bean的id(是),property是你想要得到的bean的属性:

<jsp:getProperty property="firstName" name="user" />

但是,更重要的是,你正在编写你的JSP,好像我们还在1999年一样。从那时起,JSP EL和JSTL已经被引入,并且不应该在JSP中使用scriptlets和jsp:xxx标签。您的JSP应该只是:

<%@ page 
    language="java" 
    contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"
%>
<!DOCTYPE html>
<html>
  <head>
    <title>Result</title>
  </head>
  <body>
    ${user.firstName}
  </body>
</html>

或者,更好的是,您应该使用the JSTL并确保firstName中的特殊字符被正确转义:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<c:out value="${user.firstName}"/>

答案 2 :(得分:0)

检查此行

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

应该是

<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="*" name="user" />
所有属性的

注意 *

但是对于你的情况 它应该是

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