Hi all please before mark my question as duplicated I want to tell you that I made a huge research including those questions
Unexpected survival of a #ViewScoped bean
ViewScope not destroy
ViewScope beans behaves like it has application scope
But I´m asking because I´m using a diferent version of the tools and don´t found any possible solution to my problem.
This is the situation
I´m using primefaces 5.1 Spring 3.1.0.RELEASE, Spring security 3.1.1.RELEASE,JSF 2.2.8 and Apache 8 .
I enter to my app and start to work, but if other person in other machine with different browser and different session id enter to the same xhtml view, he will see the data that I wrote. So looks like all the controllers of my aplication are in #ApplicationScoped but all of them are #ViewScoped
In the investigation process I did a debug and in the first acces to a view, that view call the controller, but when other person acces to the same view the controller is not called, I don´t know how the view gets the data if doesn´t call the controller.
This is one of my controllers
import javax.faces.bean.ViewScoped;
#ViewScoped
#ManagedBean
#Controller
public class CreateRepoController {
#Autowired
private IRepoFacade repositorioFacade;
#Autowired
private ISecureFacade secureFacade;
//Methods
}
Note I also tried with import org.omnifaces.cdi.ViewScoped; instead the javax viewScoped
the view is called from a menu
<p:submenu label="Repo">
<p:menuitem value="Create" url="/secured/createRepo.xhtml" />
</p:submenu>
All the application have the same behavior is not just one view
Thanks in advance for your time and answers
You have posted very few details. So I'll give you some hints on how to troubleshoot.
test your beans with session scope instead of view scope and check if you get a different behavior. view scope is sometimes tricky and may not work as expected. if you get a different request per user then investigate what do you have in your bean that is trigering the wrong behavior when switched to view scope.
avoid mixing CDI annotations with JSF annotations. I've had interoperativity problems when merging them.
Omnifaces ViewScope is a CDI annotation for JSF 2.0/2.1. In JSF 2.2 there is a standard annotation for CDI as part of Java EE 7 in javax.faces.view package.
try switching to all CDI if ypu still have problems. Instead of #ManagedBean use #Named and #ViewScoped from javax.faces.view.
Try do remove #Controller annotation and extend SpringBeanAutowiringSupport to enable the injections.
#ManagedBean
#ViewScoped
public class CreateRepoController extends SpringBeanAutowiringSupport implements Serializable {
#Autowired
private IRepoFacade repositorioFacade;
#Autowired
private ISecureFacade secureFacade;
//Methods
}
It works with with Spring 4.0.6 and PF 5.1.
Related
I am trying to integrate the Activiti-BPM Framework into a Java EE Webapplication.
The main goal at the Moment is, to inject an EJB as DelegateExpression into a Servicetask (to handle Database operations).
I read that Activiti does not (yet) work with EJBs, so i annotated the class like this:
#Named
#LocalBean
#Stateless
#Dependent
public class DatabaseWriter implements JavaDelegate {...}
and try to inject it in multiple ways like this:
activiti:delegateExpression="${DatabaseWriter}"
activiti:delegateExpression="DatabaseWriter"
activiti:delegateExpression="$DatabaseWriter"
respectivley to check if it is just a Bug or something:
activiti:expression="${DatabaseWriter.execute(execution)}"
In any case i get exceptions like "could not resolve DatabaseWriter, no bean foudn that implements JavaDelegate with name DatabseWriter ..."
I tested the Class itself - by not using cdi - JPA does nto work, but the class can be instantiated by Activiti and the execute method is executed - so the class code itself is ok.
I also tried injecting the class in a JAX-RS EJB - both with CDI and #EJB - it works. The problem definetly lies with my way of trying to bring it into Activiti.
I also made a Screenshot of the Problem (all code, xml, and a logmessage) for better understanding, if it helps:
http://s29.postimg.org/tmiknudjb/problem.jpg
Thanks for help or tipps!
Regards,
BillDoor
From the Oracle documentation
Annotating the boundary (Cart) with the #Named annotation makes the
Cart immediately visible for expression language (EL) expressions in
JSP and JSF. This #Named annotation takes the simple name of the
annotated class, puts the first character in lowercase, and exposes it
directly to the JSF pages (or JSP).
Activiti uses same expression language(EL) so my guess is to you try with;
activiti:delegateExpression="${databaseWriter}"
Also, I nevered use EJB and Activiti so I'm just guessing, but upper example is how it works with Spring.
Hope it helps.
I made a simple xhtml page for upload file by using <h:inputFile> component. And everything works fine. And in managed bean I used dependency injection for the Logger. I used a factory class and createLogger() method in, to enable injection for object of Logger class.
Everything is ok but, nothing works without a #Model annotation in managed bean.
Can somebody explain the meaning of the #Model annotation.
I can not find on internet proper explanation. A founded explanation of other annotation like as #Session, #Request, #Application etc.
What does the #Model annotation do?
Hej vmaric,
#Model == #RequestScoped + #Named
It exposes the Backing Bean directly to your JSF 2 or JSP and its context will be destroyed after the end of the servlet request.
So it should not be used for entities.
Here is a hint from the Weld Reference Guide:
Notice the controller bean is request-scoped and named. Since this combination is so common in web applications, there's a built-in annotation for it in CDI that we could have used as a shorthand. When the (stereotype) annotation #Model is declared on a class, it creates a request-scoped and named bean.
In Liferay, the Configuration Action class is defined in liferay-portlet.xml
The problem is, if I use any spring dependency injection, it's not working.
<portlet>
<portlet-name>search</portlet-name>
<icon>/icon.png</icon>
<configuration-action-class>com.mypack.MyConfigurationAction</configuration-action-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>search-portlet</css-class-wrapper>
<add-default-resource>true</add-default-resource>
</portlet>
Action Class implementation
public class MyConfigurationAction extends DefaultConfigurationAction {
private #Value("${test.property1}") String property1;
private #Value("${test.property2}") String property2;
}
How do I inject these properties into this Action class, without using ClassPathXmlApplicationContext and hard coding spring.xml file in the class
There are two ways to save preferences in portlet development[in liferay],
Through liferay specific way, which uses liferay-portlet.xml entry . cant be managed with spring.
JSR-286[portal agnostic], portlet EDIT mode.
While developing portlet with Spring MVC framework, its advisable to use portlet EDIT mode.
In Spring MVC portlet framework, you can map portlet requests by portlet mode.
For Example: Create controller class as below which will map to EDIT mode requests.
#Controller
#RequestMapping("EDIT")
public class PreferencesController
with two methods, one method with annotation #RenderMapping, responsible for view and other method with annotation #ActionMapping/#RequestMapping responsible for storing preferences.
Hope this would help.
Try this
portlet.xml
<supports>
.....
<portlet-mode>edit</portlet-mode>
</supports>
Controller class
#Controller
#RequestMapping(value = "EDIT")
public class XYZ{
}
HTH
First of all, "Configuration" is NOT "Edit" mode. If you enable Edit mode (as suggested by others), you'll get "Preferences" button in your portlet menu. It is a Liferay feature that you can override as per your requirement.
I have Not tried this myself but you can try to use #Autowired to AutoWire your MyConfigurationAction class (and possibly use #Required annotation if needed?). Don't forget to put <context:annotation-config/> in your applicationContext.xml file, if not already done.
I'm starting a new JSF (2) project. I realize that pure JSF has some limitation and I'm planning to drop in spring. JSF is a relatively new approach in spring (there is not even a tag spring-faces in stackoverflow :-) ), jsp being the classic way. I need the best available adjustment of the technology stack.
1) do i need to use spring web flow too?
2) is it viable to use just spring faces (that seems to come as a part of web flow) + spring mvc+ JPA?
3) is there any good example of such a configuration?
I'm using JSF 2 together with Spring 3 for Dependency Injection etc.
I'm not familiar with Web Flow and I don't use it.
In your faces-config.xml you can register the Spring SpringBeanFacesELResolver.
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
Then you can access Spring managed beans in your JSF code!
Have a look at the Spring documentation and the API docs.
If you do not have heavy wizard-specific views in your application, I doubt you'll actually need to use SWF.
The easiest solution is actually the one Sebi told you - register the Spring EL Resolver and mark your controller classes with appropriate stereotype (most usually, #Controller) and desired scope. From there on you should be able to get references to Spring-managed beans via manual- or autowiring. And that's all there is to it - no faces-config.xml bean managment and no "double IoC" overhead. Once it's in Spring context, the managed controller is dereferenced easily from facelet via #{} EL-notation.
For example:
TestController.java:
#Controller("myController")
#Scope("request")
public class TestController {
#Autowired
private SomeSpringManagedBean someBean;
private String someViewProperty;
public String getSomeViewProperty() {
return someViewProperty;
}
public void setSomeViewProperty(String arg) {
this.someViewProperty = arg;
}
......
}
TestView.jspx:
<p:inputText value="#{myController.someViewProperty}" />
We've lost about 2 weeks trying to tie in SWF together with JSF 1.2 - only to discover that once we actually got it working with the latest version of the IceFaces that support JSF 1.2, the IceFaces had a feature/bug so nasty that it simply wouldn't render the view and had gotten stuck in Phase 5 without throwing any exception or reporting anything useful (the issue became fixed in a 1.8.2-GA version of IceFaces that isn't obtainable without purchasing the license).
EDIT: I noticed basically a similar SO-thread here.
In my Java EE 6-webapp (running on latest GlassFish 3.1), I'm using JSF2-ManagedBeans and #ManagedProperty to inject them into other ManagedBeans. Now i would like to know if i can also inject a #ManagedBean into a #WebServlet, using #ManagedProperty. Some code:
#WebServlet(name = "vdd")
public class VddServlet extends HttpServlet
{
#ManagedProperty(value = "#{userIdentity}")
private UserIdentity identity;
}
The ManagedBean looks like this:
#ManagedBean
public class UserIdentity
{
...
}
Does it work like this? If not, what other ways do i have to inject a ManagedBean into a WebServlet (without CDI, which is currently not an option - since there are some issues in GF 3.1 B32/33 in combination with OSGi-Java EE-apps, but we are short on time)?
Using #ManagedProperty in a servlet is not possible since this works in #ManagedBean classes only. Further, injecting an object which has a lesser scope than the parent itself is also not possible since that would also only end up in concurrency problems. The injector would throw a runtimeexception for that. A servlet is in essence application scoped and shared among all users and your UserIdentity bean seems to be session scoped.
Since JSF runs on top of the Servlet API and stores the session scoped beans in, well, the session, you could in the servlet just grab it as session attribute:
UserIdentity identity = (UserIdentity) request.getSession().getAttribute("userIdentity");
Note that the FacesContext is usually also not available in a servlet other than FacesServlet, so using FacesContext in the servlet as suggested in a comment does not make any sense, that would only return null.