将 java bean 属性传递给 JSTL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16133956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:00:57  来源:igfitidea点击:

Passing java bean properties to JSTL

javajstl

提问by Delmon Young

I'm really new to Java and JSTL so sorry if this is a super simple question. I'm trying to take an example I found online and get it to work but I seem to be running into problems. All that is suppose to happen is you create a java bean and access properties from the java bean. But instead I'm getting a null pointer exceptionon the line that I'm calling the class in the JSTL jsp:useBean id="students" class="com.beans.Students". Here is the java class:

我对 Java 和 JSTL 真的很陌生,如果这是一个超级简单的问题,我很抱歉。我试图举一个我在网上找到的例子并让它工作,但我似乎遇到了问题。假设发生的一切就是您创建一个 java bean 并从 java bean 访问属性。但相反,我在调用 JSTL jsp:useBean id="students" class="com.beans.Students" 中的类的行上得到一个空指针异常。这是java类:

package com.beans;

public class Students implements java.io.Serializable
{
    private String firstName = null;
    private int age = 0;

    public Students() {
    }
    public String getFirstName(){
        return firstName;
    }
    public int getAge(){
        return age;
    }
    public void setFirstName(String firstName){
        this.firstName = firstName;
    }
    public void setAge(Integer age){
        this.age = age;
    }
}

JSTL in which I'm trying to access Java bean:

我试图在其中访问 Java bean 的 JSTL:

<jsp:useBean id="students" 
                class="com.beans.Students"> 
<jsp:setProperty name="students" property="firstName"
                 value="Zara"/>
<jsp:setProperty name="students" property="age" 
                value="10"/>
</jsp:useBean>

<p>Student First Name: 
<jsp:getProperty name="students" property="firstName"/>
</p>

<p>Student Age: 
<jsp:getProperty name="students" property="age"/>
</p>

Stack trace:

堆栈跟踪:

Caused by: org.apache.sling.api.SlingException: An exception occurred processing JSP page /students.jsp at line 18
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:574)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:499)
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
at org.apache.sling.scripting.jsp.JspServletWrapperAdapter.service(JspServletWrapperAdapter.java:59)
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:453)
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:358)
... 174 more
Caused by: java.lang.NullPointerException
at    org.apache.sling.scripting.jsp.jasper.runtime.JspRuntimeLibrary.internalIntrospecthelper(JspRuntimeLibrary.java:322)
at    org.apache.sling.scripting.jsp.jasper.runtime.JspRuntimeLibrary.introspecthelper(JspRuntimeLibrary.java:308)
at org.apache.jsp.students_002d_jsp._jspServ

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by clav

If you've put a Studentsobject on the request in your servlet doing something like request.setAttribute("students", myStudentObject);, then the JSTL equivalent of what you have on your page would be this:

如果您Students在 servlet 中的请求上放置了一个对象,执行类似的操作request.setAttribute("students", myStudentObject);,那么与您在页面上所拥有的内容等效的 JSTL 将是这样的:

<p>Student First Name: <c:out value="${students.firstName}"/></p>

<p>Student Age: <c:out value="${students.age}"/></p>

Make sure you include the JSTL core tags at the top of your page like this:

确保在页面顶部包含 JSTL 核心标签,如下所示:

<%@ taglib prefix="c" 
       uri="http://java.sun.com/jsp/jstl/core" %>

Using the c:outtag is good if you need to worry about XSS attacks, but if that's not a concern you can just skip the c:outtag and use an EL expression like this:

c:out如果您需要担心 XSS 攻击,使用该标签是很好的,但如果这不是问题,您可以跳过该c:out标签并使用像这样的 EL 表达式:

<p>Student First Name: ${students.firstName}</p>

<p>Student Age: ${students.age}</p>