I have a project with multiple modules.
Two of them generate war files.
One war file is a REST application and provides a couple of resources.
The Other war file is a Angular JS web application (static content only) to talk to the REST backend.
For demo purposes I'd like to deploy both war-files very easily with mvn jetty:run
For development purposes I'd like to deploy them from my IDE (e.g. Eclipse Servers View).
When I do the deployment on a single Jetty Server (v9.0.7.v20131107) manually by starting the server and copiing the war-files to the deployment folder everything comes up.
When starting the jetty by mvn jetty:run both war files get deployed, but somehow the REST Resources do not get deployed.
I am using Jersey 2. When deploying manually I get a log message like
Nov 14, 2013 10:44:37 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.4 2013-10-24 18:25:49...
However this message is not been shown when starting with mvn jetty:run. Therefore I assume that Jersey does not kick in.
For Dependency-Injection I use spring.
This is the parent pom in /pom.xml with the jetty-maven-plugin configuration
<project ...>
...
<build>
<plugins>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.7.v20131107</version>
<configuration>
<scanIntervalSeconds>2</scanIntervalSeconds>
<contextHandlers>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module1/target/module1-${project-version}.war</war>
<contextPath>/module1</contextPath>
</contextHandler>
<contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
<war>module2/target/module2-${project.version}.war</war>
<contextPath>/module2/contextPath>
</contextHandler>
</contextHandlers>
</configuration>
</plugins>
</build>
...
</project>
This is the pom for module1 (the REST module)
<parent>
...
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module1</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<springVersion>3.1.4.RELEASE</springversion>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.4</version>
</dependency>
<!-- Dependencies to internal modules -->
...
<!-- Depenencies to internal modules END -->
</dependencies>
</project>
This is the web.xml for module1
<web-app ...
version="3.0">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
This is the applicationContext.xml for module1
<beans ...>
<context:annotation-config/>
<context:component-scan base-package="com.stackoverflow.zip"/>
<aop:aspectj-autoproxy/>
</beans>
This is the Module1Application class for module1
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
#ApplicationPath("/rest")
public class Module1Application extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(Resource1.class);
classes.add(Resource2.class);
classes.add(MultiPartFeature.class);
return classes;
}
}
This is the pom for module2 (the AngularJS app)
Very light :)
<project ...>
<parent>
...
</parent>
...
<modelVersion>4.0.0</modelVersion>
<artifactId>module2</artifactId>
<packaging>war</packaging>
</project>
Do you have any idea why the Jersey Application does not get instantiated while running with mvn jetty:run but when running it manually?
I appreciate any input on this topic.
Kind regards
- zip
Your javax.servlet version is 2.5 in pom.xml, but 3.0 in web.xml. Try upgrading it to 3.0 in pom.xml. Is it possible that the IDE provides a servlet 3.0 for you?
Also, since Jetty provides a javax.servlet container, try putting the Servlet Api into provided scope in pom.xml. Something like this:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<!-- http://stackoverflow.com/a/15601606 http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope -->
<!-- This allows us to compile the application locally but does not include the jar in the package step -->
<scope>provided</scope>
</dependency>
It's possible that you want to add a <load-on-startup>1</load-on-startup> element to your web.xml. Check out http://tutorials.jenkov.com/java-servlets/web-xml.html#load-on-startup
That's whole tutorial is pretty good.
Disclaimer: I'm not familiar with what the Spring dependencies are doing to help your application bootstrap itself. You can always try removing dependencies on a separate branch, and adding them back in as you get things running
I haven't had a chance to play with Jersey 2.x so far, but from what I see in your web.xml, it doesn't look like you have Jersey setup correctly.
In a non-Spring application, you would have something like the following:
<web-app ...>
...
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.foo.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
In a Spring-based one, you would use:
<web-app ...>
...
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.your.foo</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
</web-app>
Furthermore, check that you have these Maven dependencies:
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
Related
I am creating a simple webservice project that pulls data from db on countries and displays it in a JSON form, My project does not throw any error and builds successfully but it is unable to find deployed resource.
WorldInformation.java class
package com.webservices;
import java.util.List;
import com.webservices.models.Country;
import com.webservices.services.WorldInformationService;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/worldinformation")
public class WorldInformation {
WorldInformationService worldInformationService = new WorldInformationService();
#GET
#Path("/getCountries")
#Produces(MediaType.APPLICATION_JSON)
public List<Country> getCountries(){
System.out.println("reached point 1");
List<Country> countryList = worldInformationService.getCountries();
return countryList;
}
#POST
#Path("/setCountry/{country}/{countryCode}")
#Consumes(MediaType.TEXT_PLAIN)
public void setCountry(#PathParam("country") String country,#PathParam("countryCode") String countryCode){
worldInformationService.setCountry(country,countryCode);
}
}
This is web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<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">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
And this is what pom.xml looks like
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.webservices</groupId>
<artifactId>WorldInformation</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>WorldInformation</name>
<build>
<finalName>WorldInformation</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<!-- uncomment this to get JSON support
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-binding</artifactId>
</dependency>
-->
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
</dependencies>
<properties>
<jersey.version>3.0.0-M1</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
This is error snapshot
I can't seem to figure out how to get this resolved and what's going wrong, will deeply appreciate your help on this.
I think you need to add #ApplicationPath in your jax-rs application as well.
Note: The URL is also missing the context path.
For example: http://localhost:8080/<context-path>/servlet/path
From Oracle Docs :
The #ApplicationPath annotation is used to define the URL mapping
for the total application. The path specified by #ApplicationPath is the
base URI for all resource URIs specified by #Path annotations in the
resource class.
For example : Add a class WorldInformationApp annotated with #ApplicationPath in the project.
#ApplicationPath("/worldinfo")
public class WorldInformationApp extends Application {
}
Try to access the resource with this url : http://localhost:8080/<context-path>/worldinfo/worldinformation/getCountries
Note : I think you are using wrong libraries for jax-rs app.
For example : javax.ws.rs.* should be used instead of jakarta.ws.rs.*
Thanks for your valuable feedbacks, here is what was worked for me. As someone suggested. I was not looking at correct point. I was missing application name in the url I was trying to connect to. Though I ran into other problems but I managed to solve them and now its working for me.
localhost:8080/WorldInformation/worldinformation/getCountries
Thanks for your valuable help on this.
Currently, it seems like my jax-rs services is using with Jersey ServletContainer but running on Tomcat. This baffled me because according to my understanding, Jersey is a server that have more functionality than Tomcat, and don't contain Tomcat, but now, my project is using Jersey's library but running on Tomcat, how could this happen?
Below is my web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Jersey Servlet -->
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>main.java</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Below is my pom.xml(I used Maven):
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- servlet dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
Tomcat is a Java web server, which implements several, but not all, Java EE specifications. Another Java web server would be Jetty. They differ from full application servers like Glassfish or JBoss / WildFly in the number of Java EE specifications they implement. The rather minimal Tomcat implements JavaServer Pages and Java Servlets, which is enough for a lot of applications.
Jersey is a Java library for both serving and calling REST (or mainly HTTP, since not everything is REST) APIs. It's build on top of Java EE specifications, so it can be used on any server that implements these specifications, e.g. Tomcat.
In your web.xml file you can define multiple servlets. What the servlet does is defined by the <servlet-class> element. You could pass your own implementation on top of the HttpServlet. In your case, you are using the Jersey servlet, which then manages all requests to the URLs it is mapped to (<servlet-mapping>). You can now learn to work with Jersey, implemented your desired API behaviour and build a web archive (.war). This web archive can then be deployed to any web server, that implements the required specifications, e.g. Tomcat. If you start using other Java EE technologies like Enterprise JavaBeans, you need to check which server implementation implements this technology. You could use Glassfish, there would be no difference for Jersey.
EDIT: I forgot to say that Jersey is one possible (the reference) implementation for the JAX-RS specification, like Tomcat is one possible Java Servlet (and others) implementation. Nevertheless, one is a web server and the other is a web service library, so its not possible to compare them or say that one has "more functionality than" the other.
Tomcat is a Servlet container. If Jersey has a Servlet, Jersey can run in Tomcat.
I am trying to set up a simple Java web application using jersey for web services. However I have de following problem.
The tomcat server can’t find the resource http://localhost:8081/OnlineShop/rest/books/list but it can find my simple servlet http://localhost:8081/OnlineShop/index
I have the following web.xml
In the other hand I noticed that com.sun.jersey.spi.container.servlet.ServletContainer is present in my project because I added the dependency using maven however jersey.config.server.provider.packages is not present. Maybe that is the problem but I don’t know the exact dependency which I have to add.
My BookRest.java has the following code and is on the com.shop.rest package.
Finally my pom.xml has the following dependencies.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Please, please get rid of this whole project. You're obviously a beginner and seem to be just putting random configurations and dependencies together, maybe from different tutorials. Your dependencies are incompatible and your web.xml configuration is wrong. Like i said, scrap the whole project and start from scratch. If you are just beginning, you should use one of the startup apps.
You're in Netbeans, so just do the following
File → New Project
Maven → Project from Archetype
Search jersey-quickstart-webapp
Select the one with the Group ID org.glassfish.jersey.archetypes
The latest version should be displayed.
Should be self explanitory from there
You will that the only dependency added is
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
And the web.xml will look something like
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.stackoverflow.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This will get you a simple app up and running. You will see a dependency that you need to un-comment for JSON support. Un-comment it. Or better yet, un-comment it, then change jersey-media-moxy to jersey-media-json-jackson. Jackson is IMO a better JSON library.
Also keep the Jersey Documentation handy for some good reading and reference material for working with Jersey
I've got a strange issue concerning the usage of an el-tag in JSF. I have an nearly empty project with the following dependencies:
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>el-impl</artifactId>
<version>2.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>cupertino</artifactId>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>all-themes</artifactId>
<version>1.0.10</version>
</dependency>
And I have an bean, which starts like this:
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
#ManagedBean(name = "requester")
#ApplicationScoped
public class UserBean implements Serializable {
public String getRequestIdentificator() {
return requestIdentificator;
}
public void setRequestIdentificator(String requestIdentificator) {
this.requestIdentificator = requestIdentificator;
}
and web.xml looks like this:
<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"
id="DAS_ID">
<display-name>DAS</display-name>
<context-param>
<param-name>org.apache.myfaces.EXPRESSION_FACTORY</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.expressionFactory</param-name>
<param-value>com.sun.el.ExpressionFactoryImpl</param-value>
</context-param>
<context-param>
<description>Parameter required by Mojarra 2.0</description>
<param-name>com.sun.faces.allowTextChildren</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
Tell the runtime where we are in the project development
lifecycle. Valid values are:
Development, UnitTest, SystemTest, or Production.
The runtime will display helpful hints to correct common mistakes
when the value is Development.
</description>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/main.xhtml</welcome-file>
</welcome-file-list>
</web-app>
Unfortunately, everytime, something like <p:commandButton value="Request" action="#{requester.requestData}" /> is called, this results in
SEVERE: javax.el.PropertyNotFoundException: /main.xhtml #19,100 value="#{requester.requestIdentificator}": Target Unreachable, identifier 'requester'
resolved to null
I already searched for errors, the common errors are e.g. summarized here: Target Unreachable, identifier resolved to null, but none of this applies. Furthermore, NetBeans is able to use code-completion for everything I am doing (I also tried it with other things than requestIdentificator) - so it seems like code is written fully correct. The Bean seams never to be initialized (so the logging in the constructor is not called), but according to e.g. https://stackoverflow.com/a/11013290/2096209, is is not necessary to register the Bean in faces-config in JSF 2.x (I am using 2.1), so that can not be the problem.
I am running everything with mvn tomcat7:run. Has anybody an hint how to solve this?
I Often have this same problem using the javax.faces.bean package. Try importing
import javax.enterprise.context.ApplicationScoped;
instead of
import javax.faces.bean.ApplicationScoped;
And instead of the #ManagedBean annotation use #Named
import javax.inject.Named;
import javax.enterprise.context.ApplicationScoped;
#Named("requester")
#ApplicationScoped
public class UserBean implements Serializable {
Then build and redeploy. See if that helps
The problem was that I tried running it with mvn tomcat7:run. If I copy the war into a normal tomcat7-instance, it works, and as well, if I run it with mvn tomcat7:run-war. Seems to be an maven-tomcat-plugin bug.
This is the structure of my project (exactly these five files):
/p1
pom.xml
/src
/main
/java
/webapp
a.html
b.xhtml
/WEB-INF
faces-config.xml
web.xml
I'm deploying this WAR to GlassFish and I can successfully access this URL: http://localhost:8080/p1/a.html. When I'm trying to open http://localhost:8080/p1/b.xhtml I'm getting a message
The requested resource (/p1/b.xhtml) is not available.
What am I doing wrong?
ps. My dependencies from pom.xml:
...
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.facelets</groupId>
<artifactId>jsf-facelets</artifactId>
<version>${facelets.version}</version>
</dependency>
...
This is my web.xml (core part of it):
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
My faces-config.xml:
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
</application>
For the Maven side, things looks ok, except that facelets should also be provided. Actually, I use the following dependency:
<!-- This dependency will bring in everything we need for JAVA EE6 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
For the JSF part, nothing in the server logs? Just in case, could you add the following to your web.xml to see if you get more useful output:
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
As a side note, you don't need your faces-config.xml as Facelets is the default view handler in JSF 2.0. But this shouldn't be a problem.
PS: Personally, I prefer to map the Faces Servlet on something like *.jsf (to clearly de-correlate any mapped url from the actual .xhtml facelet page that will be processed by the Faces Servlet).
See also
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?