HttpServletRequest for jsf [英] HttpServletRequest for jsf

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

问题描述

我有一个jsp网站,我在其中使用request.getAttribute获得属性. 我正在寻找一种在jsf中获取该属性的方法(在同一tomcat中运行).这是我在互联网上找到的:

i have a jsp site where i get an attribute using request.getAttribute. i'm looking for a way to get that attribute in jsf (running in the same tomcat). this is what i found on the internet:

HttpServletRequest requestObj = (HttpServletRequest)         
FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String value =  (String) requestObj.getAttribute("property");

但结果(值)保持为空.

but the result (value) stays null.

可能是什么原因导致它在第一种情况下有效,而在第二种情况下无效?

what could possible reasons be that it works in the first case but not in the second?

推荐答案

可能是什么原因导致它在第一种情况下有效,而在第二种情况下无效?

如果该属性不再存在,则仅涉及完全不同的请求.可能您已经发送了重定向,或者Web浏览器已发送了新请求,等等.由于您没有详细说明问题中的功能要求,因此很难给出问题的真正答案.您刚刚发布了一些代码段,并说为什么这行不通?".

If the attribute is not there anymore, then it simply concerns a completely different request. Probably you have sent a redirect, or the webbrowser has sent a new request, etc. The real answer to your problem is hard to give since you didn't elaborate anything about the functional requirement in your question. You just posted some code snippet and said "why doesn't this work?".

无论如何,请求属性只要请求/响应本身就存在,并且不保留在后续请求中.为此,您希望将其存储为会话属性,或者在重定向的情况下作为请求参数传递.或者,当您已经在使用JSF 2.0时,将其存储为视图作用域的bean的属性也应适用于随后要从同一视图访问该bean的情况.

At any way, the request attributes live as long as the request/response itself and they are not retained in subsequent requests. For that you'd rather like to store it as a session attribute instead, or to pass as a request parameter in case of a redirect. Or, when you're already on JSF 2.0, storing it as a property of a view scoped bean should also do for the case the bean is to be accessed from the same view subsequently.

要了解有关HTTP Servlet请求/响应的生命周期的更多信息,建议您通过

To learn more about the lifecycle of the HTTP servlet request/response, I'd suggest to get yourself through this answer.

无关:与具体问题无关:每当您需要从JSF内容中拖出原始Servlet API时,您就应该休息一下,如果您确实在做正确的事,请三思而后行.方式(阅读:无需使用原始的javax.servlet API).例如,不是应该成为一个完全有价值的JSF托管Bean吗?

Unrelated to the concrete problem: whenever you need to haul the raw Servlet API from under the JSF covers, then you should really take a break and think twice if you're really doing things the right way (read: without the need to fall back to the raw javax.servlet API). Shouldn't it better be a fullworthy JSF managed bean, for example?

通过

无需使用原始的Servlet API.

No need to utilize the raw Servlet API.

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

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