简体   繁体   中英

How to get my managed beans by name in JSF?

I have been working on writing a servlet for AJAX/JSON.

I want to reference my @ManagedBeans by name. I basically want to do the mapping between:

http://host/app/myBeanName/myPropertyName

and-

@ManagedBean(name="myBeanName")
public class MyBeanName {
    public String getMyProperty();
}

Can I load a bean by name from a regular servlet? Or if there is a JSF servlet or helper I could use for it please tell me about it?

Can I load a bean by name from a regular servlet? O

Yes if you have your bean in request scope then from servlet @RequestScope

YourBean bean = (YourBean)request.getAttribute("yourBeanName");

if it is in session scope @SessionScoped

YourBean bean = (YourBean)request.getSession().getAttribute("yourBeanName");

for application scoped bean @ApplicationScope

YourBean bean = (YourBean)getServletContext().getAttribute("yourBeanName");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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