Posts Tagged ‘JSP’
25
Jan

For the use of JSF you only need a servlet container (no full application server).

JSF 1.2, 2.0: Servlet 2.5 / JSP 2.1 container

JSF 1.2 and JSF 2.0 depend only on Servlet 2.5 / JSP 2.1 containers. Note that EL 2.1 is a component of the JSP 2.1 Specification. This is the case in JEE 5 AppServers:

  • Tomcat 6
  • Glassfish 2
  • JBoss AS 5
  • Oracle WebLogic 11g
  • IBM WebSphere 7, etc.

E.g. So you can use JSF 2.0 in Tomcat 6, but not the features of EL 2.2, like call JSF action method with arguments. To tune Tomcat 6 towards Java EE 6 see here.

JSF 2.1, 2.2: Servlet 3.0 / JSP 2.2 container

JSF 2.1 and JSF 2.2 depend on Servlet 3.0 / JSP 2.2 containers. Note that EL 2.2 is a component of the JSP 2.2 Specification. This is the case in JEE 6 AppServers:

  • Tomcat 7
  • Glassfish 3
  • JBoss AS 6
  • Oracle WebLogic 12g
  • IBM WebSphere 8
  • or newer versions

For an overview of JEE 6 compatible servers: http://www.oracle.com/technetwork/java/javaee/overview/compatibility-jsp-136984.html
What’s new in JSF 2.1? It’s mainly a maintenance release:
http://javaserverfaces.java.net/nonav/rlnotes/2.1.0/whatsnew.html
http://it-republik.de/jaxenter/news/Was-ist-neu-in-JSF-2.1-057653.html (German)

, , , , , , , , , , , ,

18
Jan

This should work theoretically with every Servlet 2.5 / JSP 2.1 container, but tested with Tomcat 6 only…

Intro

If your customer forces you to run your WebApps in an old Servlet 2.5 / JSP 2.1 container ;-) , but you want to kick off using new JEE 6 stuff, then heres how you get started:

JSF 2.0

JSF 2.0 just needs Servlet 2.5 / JSP 2.1 container (JSF 2.1+ needs alredy Servlet 3.0). Download the reference implementation Mojarra or Apache MyFaces and put the two jars into the WEB-INF/lib:

jsf-api.jar
jsf-impl.jar

EL 2.2 or JBoss EL

Have you ever wished to pass an argument to your JSF-actions in your view? Like

<h:commandButton value="edit" action="#{bean.edit(user)}" />

with backing bean method

public void edit(User user) {...}

JBoss EL 2.0.1.GA

This is possible with JBoss EL implementation, see http://docs.jboss.org/seam/2.0.1.GA/reference/en/html/elenhancements.html. Add to your pom:

<dependency>
  <groupId>org.jboss.el</groupId>
  <artifactId>jboss-el</artifactId>
  <version>2.0.1.GA</version>
  <exclusions>
    <exclusion>
      <groupId>javax.el</groupId>
      <artifactId>el-api</artifactId>
    </exclusion>
  </exclusions>
</dependency>

and to your web.xml

<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
  <param-value>org.jboss.el.ExpressionFactoryImpl</param-value>
</context-param>

EL 2.2

Or use EL 2.2, which is a component of the JSP 2.2 spec. You need to put the two EL JARs into the WEB-INF/lib: el-api-2.2.0.jar, el-impl-2.2.0.jar. Add to your web.xml:

<context-param>
  <param-name>com.sun.faces.expressionFactory</param-name>
  <param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>

Note: If you also use CDI with Weld, this ends up in an exception:
LinkageError: loader constraint violation: when resolving interface method "javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;"
One way out is to replace in the apache-tomcat/lib the el-api.jar with el-api-2.2.jar and el-impl-2.2.jar and put in your pom.xml:

<dependency>
  <groupId>javax.el</groupId>
  <artifactId>el-api</artifactId>
  <version>1.0</version>
  <scope>provided</scope>
</dependency>

If you cannot change the server itself then stick to JBoss EL.

CDI

If you want to use CDI managed beans (@Named) instead of JSF managed beans (@ManagedBean) in a plain Servlet container, then you can use the reference implementation Weld: http://seamframework.org/Weld,
CDI is the new JEE 6-standardized way for managing beans: it’s more robust, and the same as in an EJB context. Furthermore you can use a conversation scope. I still like the JSF view scope, so maybe drop JSF management totally, or mix it.
For setup refer to http://docs.jboss.org/weld/reference/1.0.0/en-US/html/environments.html. There it is explained:

Just drop into WEB-INF/lib

weld-servlet.jar

and add to your web.xml:

org.jboss.weld.environment.servlet.Listener
<listener>
  <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
  <description>Object factory for the CDI Bean Manager</description>
  <resource-env-ref-name>BeanManager</resource-env-ref-name>
  <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>

Bean Validation

“Declare once, validate anywhere!” is the advertisement for Bean validation: Don’t put your validation in the views, DAOs/DTOs, etc., but put your validation only at one place:
your beans (although in real life in the database remain ranges, constraints, etc., so some redundancy remains…). If you have different pages with input data to the same data, then this is a big advantage for maintenance: change at one place.

public class Bean {
@Size(min=1)
private String name;
}

Again use the RI from Hibernate https://validator.hibernate.org/ and drop it to WEB-INF/lib:

hibernate-validator-4.0.2.GA.jar
all JARs in the lib directory of the downloaded zip file.

Note: JSF 2.0 directly supports Bean Validation. In JSF 1.2 you could use RichFaces 3.3.3.

JPA 2.0

Some drawbacks without a real AppServer is that

  • manage your transactions by yourself with EntityTransaction (no JTA transactions).
  • direct resource handling, i.e. use the persistence factory EntityManagerFactory to obtain an EnitityManager (no injections possible, because of the lack of EJBs), also don’t forget to close your EntityManager…

Get a JPA provider, e.g. Hibernate and EclipseLink. For EclipseLink, see http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial. For Hibernate just get the zip from http://www.hibernate.org/ and follow the good documentation.

You need to setup your persistence.xml and make some provider and database specific configurations.

, , , , , , , , , , , , , ,