jsp:useBean范围 [英] jsp:useBean scope

查看:491
本文介绍了jsp:useBean范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSP代码是:

<jsp:useBean id="person" class="org.example.model.PersonModel" scope="session">
</jsp:useBean>
<br> Name : <jsp:getProperty property="name" name="person"/>
<br> Surname : <jsp:getProperty property="surname" name="person"/>

虽然我在请求中设置了java对象范围,而不是在我将转发请求到此Servlet的Controller Servlet中的 session 范围内。 < jsp:useBean> 如何获取请求属性,尽管标记中提到的范围是会话?如果它使用 pageContext.findAttribute()来获取属性,那么在< jsp:useBean>中使用scope属性有什么用? tag?

Although I set java object in the request scope and not in the session scope in the Controller Servlet from where I am forwarding the request to this Servlet . How does the <jsp:useBean> gets hold of the request attribute although scope mentioned in the tag is session? If it uses pageContext.findAttribute() to get the attribute, then what is the use of having the scope attribute in that <jsp:useBean> tag ?

推荐答案

PageContext#findAttribute() 分别扫描页面,请求,会话和应用程序范围,直到找到给定属性键的第一个非 null 属性值。另见 javadoc


在页面,请求,会话(如果有效)和应用程序范围内搜索命名属性(s )按顺序并返回关联的值或null。

Searches for the named attribute in page, request, session (if valid), and application scope(s) in order and returns the value associated or null.

这解释了为什么它找到请求作用域在转发servlet中设置,而不是在JSP中声明的会话作用域。这也在我们的EL维基页面中进行了解释。

That explains why it finds the request scoped one set in the forwarding servlet instead of the session scoped one declared in the JSP. This is also explained in our EL wiki page.

In无论如何,如果你使用的是servlet,你不应该在应该由servlet管理的模型对象上使用< jsp:useBean> < jsp:useBean> 就是一个不同的MVC级别,当实际使用servlet作为控制器时,这只会导致混乱和维护问题。在我们的Servlets维基页面的编码样式和建议部分中也明确提到了这一点。

In any case, if you're using a servlet, you should not be using <jsp:useBean> on model objects which are supposed to be managed by a servlet. The <jsp:useBean> follows namely a different MVC level which would only lead to confusion and maintenance trouble when actually using a servlet as controller. This is also explicitly mentioned in "Coding style and recommendations" section of our Servlets wiki page.

因此,您可以这样做,而不是所有< jsp:xxx> 的事情:

So, instead of all those <jsp:xxx> things, you can just do:

<br>Name: ${person.name}
<br>Surname: ${person.surname}

您只需要添加JSTL < c :out> 在重新显示用户控制的数据时防止潜在的XSS攻击漏洞(注意< jsp:getProperty> 不会这样做! )

You only need to add JSTL <c:out> to prevent potential XSS attack holes while redisplaying user-controlled data (note that <jsp:getProperty> doesn't do that!)

<br>Name: <c:out value="${person.name}" />
<br>Surname: <c:out value="${person.surname}" />

要了解有关JSTL的更多信息,请查看我们的JSTL维基页面

To learn more about JSTL, check our JSTL wiki page.

这篇关于jsp:useBean范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆