简体   繁体   English

如何为通过JSP从Java bean获取的变量赋值?

[英]how to assign a value to a variable fetched from java bean over JSP?

I am working on JSP and servlets. 我正在研究JSP和servlet。 I need to fetch values from java bean and assign it some other variable over JSP. 我需要从Java bean中获取值,并通过JSP为它分配其他变量。

Usually I fetch the value in html tags as ${abcd.variable_name} 通常,我在html标记中将值提取为$ {abcd.variable_name}

but this thing can't be used it we want to get some value in <% %> 但是这个东西无法使用,我们想在<%%>中获得一些价值

That depends on where the bean is stored. 这取决于bean的存储位置。 If it is stored in the request scope as a request attribute, just get it back as request attribute: 如果将其作为请求属性存储在请求范围中,只需将其作为请求属性取回即可:

<%
    Bean bean = (Bean) request.getAttribute("bean");
    // ...
%>

Or if it's stored in the session scope as a session attribute, just get it back as session attribute: 或者,如果将其作为会话属性存储在会话范围中,则只需将其作为会话属性取回即可:

<%
    Bean bean = (Bean) session.getAttribute("bean");
    // ...
%>

Or if it's stored in the application scope as an application attribute, just get it back as application attribute: 或者,如果将其作为应用程序属性存储在应用程序范围中,则只需将其作为应用程序属性取回即可:

<%
    Bean bean = (Bean) application.getAttribute("bean");
    // ...
%>

However, you're doing the desired job at the wrong place. 但是,您在错误的位置进行了所需的工作。 It has to be done in a normal Java class like a servlet or at least the action class of the MVC framework you're using, if any. 必须在普通的Java类(例如servlet)中完成,或者至少在您使用的MVC框架的操作类(如果有)中完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM