3

I'm doing a Primefaces project using Maven and Glassfish. I am following this tutorial by mkyong. I have followed all the steps but when I run my project, I get this error in my browser console.

Uncaught ReferenceError: $ is not defined
(anonymous function)

Here is part of the htmls that is generated:

<form id="j_idt6" name="j_idt6" method="post" action="/jsfTest/faces/hello.xhtml" enctype="application/x-www-form-urlencoded">
 <input type="hidden" name="j_idt6" value="j_idt6" />
 <div id="j_idt6:j_idt7" style="visibility:hidden"><textarea id="j_idt6:j_idt7_input"  name="j_idt6:j_idt7_input">This editor is provided by PrimeFaces dfsdf</textarea></div><script id="j_idt6:j_idt7_s" type="text/javascript">$(function() {PrimeFaces.cw('Editor','widget_j_idt6_j_idt7',{id:'j_idt6:j_idt7'},'editor');});</script> 
  <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="2416258137569200952:-2734382740508742283" autocomplete="off" />

And no primefaces editor is shown up. But there is no error in the server log. Here are my files:

hello.xhtml(the welcome file)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:p="http://primefaces.org/ui"
    >

<h:body>
<h:form>
    <p:editor value="#{editor.value}" />
</h:form>
</h:body>
</html>

my web.xml

 <!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >

 <web-app>
  <display-name>Archetype Created Web Application</display-name>

 <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
 </context-param>


<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>


<!-- Map these files with JSF -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>

</web-app>

my glassfish-web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server    
3.1 Servlet 3.0//EN"
   "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
  <parameter-encoding default-charset="UTF-8"/>
</glassfish-web-app>

my managed bean:

import javax.faces.bean.ManagedBean;

@ManagedBean(name = "editor")
public class EditorBean {

private String value = "This editor is provided by PrimeFaces";

public String getValue() {
    return value;
}

 public void setValue(String value) {
    this.value = value;
 }
}

finally, my 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.anixtech.jsf</groupId>
<artifactId>jsfTest</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>jsfTest Maven Webapp</name>
<url>http://maven.apache.org</url>

<repositories>
    <repository>
        <id>prime-repo</id>
        <name>Prime Repo</name>
        <url>http://repository.primefaces.org</url>
    </repository>
</repositories>

<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.3</version>
    </dependency>

    <!-- JSF 2 -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.1.11</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.1.11</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>

    <!-- EL -->
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>
    </dependency>

</dependencies>
<build>
  <finalName>jsfTest</finalName>
   <plugins>
      <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>2.3.2</version>
          <configuration>
              <source>1.6</source>
              <target>1.6</target>
          </configuration>
      </plugin>
  </plugins>
 </build>

I don't know what am I doing wrong. Any idea? Thanks in advance.

Community
  • 1
  • 1
QuestionEverything
  • 4,809
  • 7
  • 42
  • 61
  • 6
    The tutorial mentioned the `` in the XHTML file. Why exactly did you omit that? Why are you trying to do things differently from the tutorial? – BalusC May 16 '13 at 13:05
  • 2
    Hi BalusC, I don't understand two things: 1. How to thank you and 2. Why it didn't work without tag. You saved my day. :) – QuestionEverything May 16 '13 at 13:14
  • 2
    By the way, your web.xml root declaration is also different from the tutorial. You're dictating Servlet 2.3 instead of 2.5. This may expose other serious issues in the future as JSF2 requires a minimum of 2.5. As you're apparently using Glassfish 3, you'd better use a Servlet 3.0 compatible web.xml. – BalusC May 16 '13 at 13:20
  • I encountered this issue using the PrimeFaces 10 dialog framework to open a simple facelets page in a popup dialog. In my case, the page inside the dialog was very simple and contained only a single `p:graphicImage` component. I was getting a "_$ is not defined_" error that went away when I added a _hidden_ `p:commandButton`. The error was caused by PrimeFaces' CSS/nonce code that needed jQuery, but couldn't find it.. When I tried to manually include jQuery, the error changed to "_PrimeFaces is not defined_" so I added the hidden `p:commandButton` and that made all the errors go away. – jahroy Jun 30 '22 at 19:51

1 Answers1

4

This question had no answer and is not marked as answered, but BalusC solution worked form me and the person who asked the question, So I'll put hear and please mark the question as answered: You should add : <h:head> in the XHTML file

user1928596
  • 1,503
  • 16
  • 21