为什么我不能在JSP中使用EL来打印值?

huangapple 未分类评论47阅读模式
英文:

Why am I not able to print values using EL in JSP?

问题

我正在使用IntelliJ Ultimate尝试基本的JSP和Servlet示例。

我有一个简单的 **Person类**:

    public class Person implements Serializable {
    
        public  String name = "Alexa";
    
        public String getName() {
            return name;
        }
    }
以及一个 **Servlet**:

    public class GenericServlet extends HttpServlet {
    
        protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Person person = new Person();
            RequestDispatcher rd = request.getRequestDispatcher("display.jsp");
            request.setAttribute("person", person);
            rd.forward(request, response);
            System.out.println("Passed to display jsp");
        }
    }
我只是想使用 **JSP** 打印人的姓名:

    <jsp:useBean id="person" scope="request" class="com.sample.Person"/>
    ......
    <h2>人的姓名是: <jsp:getProperty name="person" property="name"/></h2>
    <h2>人的姓名是: ${person.name}</h2>

我能够使用第一个h2标签打印,但不能使用第二个,即使我可以在JSP中自动完成person.name。

寻找缺失的部分。谢谢!
英文:

I am using Intellij Ultimate to try out basic examples of JSP and Sevlets.

I have a simple Person class:

public class Person implements Serializable {

    public  String name = &quot;Alexa&quot;;

    public String getName() {
        return name;
    }
}

And a Servlet:

public class GenericServlet extends HttpServlet {

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Person person = new Person();
        RequestDispatcher rd = request.getRequestDispatcher(&quot;display.jsp&quot;);
        request.setAttribute(&quot;person&quot;, person);
        rd.forward(request, response);
        System.out.println(&quot;Passed to display jsp&quot;);
    }
}

And I am simply trying to print the person name using JSP:

&lt;jsp:useBean id=&quot;person&quot; scope=&quot;request&quot; class=&quot;com.sample.Person&quot;/&gt;
......
&lt;h2&gt;Name of person is: &lt;jsp:getProperty name=&quot;person&quot; property=&quot;name&quot;/&gt;&lt;/h2&gt;
&lt;h2&gt;Name of person is: ${person.name}&lt;/h2&gt;

I am able to print using first h2 tag but not with second, even when I am able to auto-complete person.name in JSP.

为什么我不能在JSP中使用EL来打印值?

Looking for missing bits here. Thanks!

huangapple
  • 本文由 发表于 2020年5月3日 15:34:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61571048.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定