JavaServer Faces Technology again. Today I could not get much. Only few things in JSF. Created new class in Java then called that in faces-config.xml in request scope. After that used the properties and methods in JSP page. The problem I faced, was faces-config.xml has not came automatically into the structure. I need to manually add the file. Where is missed?
Here is JSF sample application, I have made. At the time of project creation, JavaServer Faces Technology framework need to select.
File Name:calc.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newPackage;
import java.math.BigInteger;
/**
*
* @author satya
*/
public class calc {
private int first, second, result;
/**
* @return the first
*/
public int getFirst() {
return first;
}
/**
* @param first the first to set
*/
public void setFirst(int first) {
this.first = first;
}
/**
* @return the second
*/
public int getSecond() {
return second;
}
/**
* @param second the second to set
*/
public void setSecond(int second) {
this.second = second;
}
/**
* @return the result
*/
public int getResult() {
return this.result;
}
public String Addition() {
this.result = (this.first + this.second);
try {
//// curently there is not change of fail as 1st and 2nd param takes int only
// for testing hardcode any string inside test. so you can see Failure.jsp page
Integer.valueOf(this.result);
return "success"; // <from-outcome>success</from-outcome>
}
catch (Exception e) {
return "failure";
}
}
}
FileName:faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="1.2"
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_1_2.xsd">
<managed-bean>
<managed-bean-name>calc</managed-bean-name>
<managed-bean-class>newPackage.calc</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<from-view-id>/welcomeJSF.jsp</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/Result.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/welcomeJSF.jsp</from-view-id>
<navigation-case>
<from-outcome>failure</from-outcome>
<to-view-id>/Failure.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
FileName: welcomeJSF.jsp:
Two libraries are added using Add library.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@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">
<%--
This file is an entry point for JavaServer Faces application.
--%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>JSP Page</title>
</head>
<body>
<h1><h:outputText value="JavaServer Faces"/></h1>
<h:form>
<h:outputLabel value="First Number"></h:outputLabel>
<h:inputText value="#{calc.first}" />
<br/>
<h:outputLabel value="Second Number"></h:outputLabel>
<h:inputText value="#{calc.second}" />
<br/>
<h:panelGroup>
<h:commandButton value="Add" action="#{calc.Addition}" />
</h:panelGroup>
</h:form>
</body>
</html>
</f:view>
FileName: Result.jsp:
<%--
Document : Result
Created on : Oct 6, 2010, 12:38:50 AM
Author : satya
--%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<h:outputLabel value="First Number" />:
<h:outputText value="#{calc.first}"/>
<br/>
<h:outputText value="Second Number"/>:
<h:outputText value="#{calc.second}"/>
<br/>
<h:outputLabel value="Result"/>:
<h:outputText value="#{calc.result}"/>
</body>
</html>
</f:view>
FileName: failure.jsp
<%--
Document : failure
Created on : Oct 6, 2010, 1:20:06 AM
Author : satya
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> Calc failed!</h1>
</body>
</html>
This was all from a JSF learner.