Managed Bean Annotation doesn't work in JSF 2.0 - jsf-2

I've encoutered strange issue with annotations in bean.
index.xhtml content:
<h:outputText value="#{ejb.helloWorld}" />
Ejb.java content:
package bean;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean(name="ejb")
#SessionScoped
public class Ejb implements Serializable
{
public String getHelloWorld()
{
return "Hello World";
}
}
From what we see above i should be able to call bean method... but i can't, it will work only if i manage bean in faces-config.xml file with content:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>ejb</managed-bean-name>
<managed-bean-class>bean.Ejb</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
Questions:
Why i have encountered such situation ?
How to resolve this issue ?
I'm using:
JSF 2.0 (imported to lib)
JBoss Server 4.2

My wild guess is that JSF 1.2 bundled by default with JBoss 4.2 is used (JBoss might not care about JSF version). As a result annotations are ignored, faces-config.xml file is used.
Try to use configuration suggested here (thats's google cached page).

Related

Primefaces PrimeExceptionHandlerELResolver within Spring Boot

i have a web application with Spring Boot 1.2.4(web,security,data-jpa), Primefaces 5.2 , JSF 2.2 (Mojarra 2.2.11) without any xml config file.
Only faces-config.xml for spring bean el resolver :
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
<!--el-resolver>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
</el-resolver-->
</application>
<!--factory>
<exception-handler-factory>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory
</exception-handler-factory>
</factory-->
</faces-config>
and all work fine.
Now, i want to handle the ViewExpiredException with primefaces exception handler :
http://www.primefaces.org/docs/api/5.2/org/primefaces/application/exceptionhandler/PrimeExceptionHandlerELResolver.html
My question is : can i register another el-resolver in faces-config.xml ? and if no, how can i fix the problem ?
thanks in advance.
EDIT :
i decide to use another aproche. many thanks to Session Timeout handling for Ajax calls
You can define multiple el-resolver in application node as shown below:
<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
<el-resolver>
org.primefaces.application.exceptionhandler.PrimeExceptionHandlerELResolver
</el-resolver>
</application>

How to integrate Struts 2 with Tiles 3

How do we integrate Struts 2 with Tiles 3? The struts2-tiles-plugin currently (2.3.4.1) works with an older version of tiles (version 2.0.6) this can be a bit of a nuisance.
This is a self-answer, to help others with their integration.
The solution is to add the required dependencies, load tiles with an appropriate listener and create a custom result type. Fortunately these steps are quite easy, once done you can follow normal tiles 2 examples for a how to define templates.
1) Dependencies (start with basic struts project but in this example I'll use conventions so it might just be best to add struts2-conventions-plugin, it will include struts2-core et al):
DO NOT INCLUDE struts2-tiles-plugin
groupId: org.apache.tiles, artifiactId: tiles-extras, version: 3.0.1
groupId: org.slf4j, artifiactId: jcl-over-slf4j, version: 1.5.8
groupId: org.slf4j, artifiactId: slf4j-jdk14, version: 1.5.8
Note: A higher version of the slf4j dependencies may work I have not tested this.
2) load tiles with an appropriate listener
This example includes the full web.xml, lines 3-5 are the only thing that should be new to someone familiar with struts2.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>org.apache.tiles.extras.complete.CompleteAutoloadTilesListener</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3) create a custom result type
We need to define a custom result type for use with our actions:
package com.quaternion.result;
import com.opensymphony.xwork2.ActionInvocation;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ServletDispatcherResult;
import org.apache.tiles.TilesContainer;
import org.apache.tiles.access.TilesAccess;
import org.apache.tiles.request.ApplicationContext;
import org.apache.tiles.request.servlet.ServletRequest;
import org.apache.tiles.request.servlet.ServletUtil;
public class TilesResult extends ServletDispatcherResult {
public TilesResult() {
super();
}
public TilesResult(String location) {
super(location);
}
#Override
public void doExecute(String location, ActionInvocation invocation) throws Exception {
//location = "test.definition"; //for test
setLocation(location);
ServletContext context = ServletActionContext.getServletContext();
ApplicationContext applicationContext = ServletUtil.getApplicationContext(context);
TilesContainer container = TilesAccess.getContainer(applicationContext);
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
ServletRequest servletRequest = new ServletRequest(applicationContext, request, response);
container.render(location, servletRequest);
}
}
4) We also need to tell struts2 about our result type:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple" />
<package name="tiles-package" namespace="" extends="struts-default">
<result-types>
<result-type default="true" name="tiles-result" class="com.quaternion.result.TilesResult"/>
</result-types>
</package>
</struts>
With that out of the way we can now use tiles in our projects, assume we have created a tiles definition called "test.definition" we can tell our action to use that definition by doing the following:
package com.quaternion.demo.action.test;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
#ParentPackage("tiles-package")
#Result(type="tiles-result", location="test.definition")
public class QuaternionResultTest extends ActionSupport{}
That's it, this will let you configure any version of struts2 with tiles 3+, please see http://tiles.apache.org/framework/index.html for further configuration details.
Thanks to Ken a new plugin was added to Struts 2 to support Tiles 3 result type, it should be available with upcoming new release - Struts 2.3.9
https://cwiki.apache.org/confluence/display/WW/Tiles+3+Plugin
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd">
use mentioned doctype in your tiles.xml

Tomcat 7 EL recognizing Method as Property

I'm trying to run an existing application on Tomcat 7.0.28 using JSF 2.1.10
I have the following bean:
#Named
#Dependent
public class FormatterBean
{
public String replaceNewLineWithBrTag(String s)
{
return Formatter.replaceNewLineWithBrTag(s);
}
public String leftJustifyWithZeros(String string, Integer zeroTotal)
{
return Formatter.leftJustifyWithZeros(string, zeroTotal);
}
}
When I do something like this
<p:tooltip for="visualizar" value="#{formatterBean.replaceNewLineWithBrTag(adiantamento.observacao)}" />
I get the following error:
/page/rdv/adiantamento.xhtml #87,117
value="#{formatterBean.replaceNewLineWithBrTag(adiantamento.observacao)}":
Property 'replaceNewLineWithBrTag' not found on type
br.com.spdata.util.FormatterBean
Some details:
Same code works fine on Glassfish 3.1.2
The method leftJustifyWithZeros works fine
Any ideias?
Thanks,
Phillip
One thing to check is whether your web.xml is declared as using the Servlet 3.0 spec, e.g.:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
...

Facelet Method call where property required

Is it possible to invoke a method where property is expected in JSF 2.0 Facelet EL. For example:
<h:outputText value="#{pojo.methodName}" />
where pojo is an instance of POJO and methodName is the name of method. An error would be thrown because JSF expects to find getMethodName method.
Before someone asks why one would need this, consider any value we want to display in text which is computed and we don't have required getter method and no source code.
Update after BalusC Answer:
No rename possible because no source code available. methodName() didn't work. The only difference is that in actual code its chained pojo.
<h:outputText value="#{pojo1.pojo2.methodName()}" />
Since other properties are working for pojo2, I assume that its methodName which can not be invoked. Server says "The class does not have the property methodName"
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
Empty faces-config
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
</faces-config>
P.S. environment JBoss 6 and JSF 2
Add parentheses:
<h:outputText value="#{pojo.methodName()}" />
(which only works in EL 2.2, which is part of Java EE 6, so it only works out box in Tomcat 7, Glassfish 3, etc, see also Invoke direct methods or methods with arguments / variables / parameters in EL)
Or just rename the method:
public String getMethodName() {
// ...
}

glassfish 3.1 jdni lookup on EJB3

I have the following Interfaces MyLocalBean, MyRemoteBean and the stateless MyBean implements MyLocalBean, MyRemoteBean
Which of the following i don't need to get a simple java application to test this ...
ejb-jar.xml, glassfish-ejb-jar.xml, gf-client.jar
Here's ejb-jar.xml
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
<enterprise-beans>
<session>
<ejb-name>MyEJB</ejb-name>
<home>test.ejb.MyLocalBean</home>
<remote>test.ejb.MyRemoteBean</remote>
<ejb-class>test.ejb.MyBean </ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
what's missing here? would i be able to lookup using MyEJB?
and here's my lookup code
InitialContext ic;
ic = new InitialContext();
MyRemoteBean remoteBean =
(MyRemoteBean ) ic.lookup("java:comp/env/MyEJB");
[EDIT]
I've update the ejb-jar.xml as follows
<enterprise-beans>
<session>
<ejb-name>MyEJB</ejb-name>
<ejb-class>test.ejb.MyBean</ejb-class>
<ejb-local-ref>
<ejb-ref-name>MyEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>test.ejb.MyLocalBean</local>
<ejb-link>MyEJBClient.jar#MyEJB</ejb-link>
</ejb-local-ref>
</session>
</enterprise-beans>
I get an error ...
cannot Deploy MyBeanEAR
Deployment Error for module:
MyBeanEAR: Error occurred
during deployment: Exception while
deploying the app [MyBeanEAR] :
Error: Unresolved :
MyEJBClient.jar#MyEJB.
Please see server.log for more
details.
[\EDIT]
[EDIT]
Hi bkail, let me brake down the problem using the correct names.. Initially, this is what my eclipse-sts IDE created by default
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
<display-name>BatchOverrideEJB</display-name>
<ejb-client-jar>BatchOverrideEJBClient.jar</ejb-client-jar>
</ejb-jar>
and this is how i tried accessing the EJB
Properties p = new Properties();
p.put("org.omg.CORBA.ORBInitialHost","localhost");
p.put("org.omg.CORBA.ORBInitialPort","3700");
InitialContext ctx = new InitialContext(p);
ic = new InitialContext(p);
BatchOverrideManagerRemote batchOverrideRemote =
(BatchOverrideManagerRemote) ic.lookup("java:comp/env/BatchOverrideEJB");
running this i got the following error
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at za.co.sanlam.batchovveride.test.BatchOverrideTester.main(BatchOverrideTester.java:33)
I thought this error meant that my EJB its not bound to the context, so i modified the ejb-jar.xml as follows
<ejb-jar xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:ejb="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd" version="3.1">
<enterprise-beans>
<session>
<ejb-name>BatchOverrideEJB</ejb-name>
<ejb-class>com.test.ejb.BatchOverrideManagerBean</ejb-class>
<ejb-local-ref>
<ejb-ref-name>BatchOverrideEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.test.batchoverridemanager.ejb.BatchOverrideManagerLocal</local>
<ejb-link>BatchOverrideEJB</ejb-link>
</ejb-local-ref>
</session>
</enterprise-beans>
</ejb-jar
but now i get javax.naming.NameAlreadyBoundException: Use rebind to override
[/EDIT]
Try <ejb-link>MyEJB</ejb-link>. That said, test.ejb.MyLocalEJB doesn't sound like a home interface that extends EJBHome, so perhaps you meant to use <business-remote> rather than <remote> and <home>? If you're using a container that supports EJB 3.1, you'll probably find it easier to just use annotations:
#Stateless
public class MyBean implements MyLocal { ... }
#Local
public interface MyLocal { ... }
#Remote
public interface MyRemote { ... }
As an aside MyLocalBean and MyRemoteBean are unusual names for business interfaces. Typically, the Bean suffix is reserved for the EJB class itself.
Edit #2:
For the second edit, see this link:
http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#nonJavaEEwebcontainerRemoteEJB

Resources