本文介绍了将java bean属性传递给JSTL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和JSTL的新手,很抱歉,如果这是一个非常简单的问题。我试图举一个例子,我在网上找到并开始工作,但我似乎遇到了问题。所有这一切都是你想要创建一个java bean并从java bean访问属性。但是我在JSTL jsp:useBean id =students
class =com中调用类的行上得到空指针异常。 beans.Students
即可。这是java类:

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 exception on the line that I'm calling the class in the JSTL jsp:useBean id="students" class="com.beans.Students". Here is the java class:

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;
    }
}

我试图访问Java bean的JSTL :

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

<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>

堆栈追踪:

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

非常感谢任何帮助!

推荐答案

如果您在servlet中的请求中放置了 Students 对象,请执行类似的请求.setAttribute(students,myStudentObject); ,然后你的页面上的JSTL相当于:

If you've put a Students object 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:

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

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

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

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

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

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

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

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

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

这篇关于将java bean属性传递给JSTL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:20