The code pasted below is a simple JSF program, with an idea of having a command button(in JSP, a JSF component), which when clicked should display a message written in the managed bean, come back and display the message in JSP.
**perfectJSP.jsp**
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test Page</title>
</head>
<body>
<f:view>
<h:form>
<h:commandButton value="Click" actionListener="#{Test.clicked}"></h:commandButton>
</h:form>
</f:view>
<h3> This brings us to the end of the program </h3>
</body>
</html>
**Test** (managed bean)
import javax.faces.event.*;
public class Test {
public void clicked(ActionEvent ae)
{
System.out.println("This is from the bean class");
}
}
**web.xml**
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>
javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>faces</servlet-name>
<servlet-class>
org.apache.myfaces.webapp.MyFacesServlet</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>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>faces</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
**faces-config.xml**
<faces-config>
<managed-bean>
<managed-bean-name>Test</managed-bean-name>
<managed-bean-class>Test</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
JBoss is the Application-server used. Unable to understand why the output is not generated.
You’re printing the message to the stdout which usually ends up in the server log. This does not end up in the HTML response which is what the client is supposed to retrieve.
If you want to display some message upon a JSF form submit, there are several ways:
Use
FacesContext#addMessage()with<h:messages/>. E.g.with
Conditionally render a component whenever the message is available. E.g.
with
Further, your managed bean class is not in a package. This is absolutely not recommended. Java classes which are supposed to be accessible by other Java classes must be placed in a package.
Unrelated to the concrete problem: you’re working with JSP which is considered deprecated since JSF 2.0 almost 2.5 years ago. JSP has been succeeded by Facelets. Make sure that you’re reading up to date JSF books/tutorials. See also our JSF wiki page.