How to get Bean data in JSP

前端 未结 1 1546
梦如初夏
梦如初夏 2021-01-27 16:41

I have bean defined as PersonBean in session scope.

What I want is find the PersonBean.personId in jsp page.

I want it in jsp page, bec

相关标签:
1条回答
  • 2021-01-27 17:26

    You should avoid scriptlets in JSPs, but to answer your question: a JSP is a servlet. If a bean is stored in session scope under the attribute "PersonBean", then just get it from the session:

    PersonBean p = (PersonBean) request.getSession().getAttribute("PersonBean");
    

    If it's stored under the attribute "PersonalInformationDataBean", the code is of course

    PersonBean p = (PersonBean) request.getSession().getAttribute("PersonalInformationDataBean");
    

    The code new PersonBean() creates a new PersonBean instance, so of course, there is no reason for this instance to have the same ID as the instance stored in the session. This is pretty basic Java stuff, and I would advise learning a bit more about the Java language and basic OO concepts before using JSF.

    0 讨论(0)
提交回复
热议问题