4

出于个人原因,我需要在 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>

豆类:

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>

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

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)

提前致谢

4

3 回答 3

5

请更改使用bean

<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

于 2013-11-29T07:18:42.580 回答
1

您的名称和属性属性不匹配。name是 bean 的 id(是),并且property是您要获取的 bean 的属性:

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

但是,更重要的是,您在编写 JSP 时就像是在 1999 年一样。从那时起,JSP EL 和 JSTL 被引入,并且在 JSP 中不再使用 scriptlet 和 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>

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

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
...
<c:out value="${user.firstName}"/>
于 2013-11-29T07:16:26.957 回答
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" />
于 2013-11-29T07:15:07.210 回答