J-Framework Simple Web Example

This example demonstrates the usage of the j-framework in creating a simple web application.

Prerequisites:

  1. You should have JDK 17+ Installed. (Click Here).

  2. You should create a Maven Project.

  3. You should enable snapshot versions. (Click Here).

Project Content:

This section contains what you should add to your code structure for this example.

  1. Maven Project with pom.xml that has the following contents:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.jalalkiswani</groupId>
		<artifactId>j-app-web</artifactId>
		<version>7.0.0-SNAPSHOT</version>
	</parent>
	<artifactId>j-framework-web-simple-example</artifactId>
	<packaging>war</packaging>
</project>
  1. Faces Controller class located at src/main/java/com/app/Controller.java that has the following content:

package com.app;

import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;

import com.jk.web.faces.controllers.JKWebController;

@Named("controller")
@ViewScoped
public class Controller extends JKWebController {
	String name;

	public String sayHello() {
		success("Hello, " + name);
		return null;// return to the same page when the action finish
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
  1. Faces View located at src/main/webapp/index.xhtml that has the following content:

<!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:ui="http://xmlns.jcp.org/jsf/facelets"
	xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core"
	xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
	<h:form>
		<p:autoUpdate />
		<p:growl />
		<p:panelGrid columns="2" style="margin:auto">
			<f:facet name="header">
				<h:outputText value="j-framework-Web Demo" />
			</f:facet>
			<p:outputLabel value="Please enter you name: " />
			<p:inputText value="#{controller.name}" />
			<f:facet name="footer">
				<p:commandButton value="Say Hello" action="#{controller.sayHello()}" />
			</f:facet>
		</p:panelGrid>
	</h:form>
</h:body>
</html>
  1. Main java class located at src/main/java/com/app/App.java which contains the following:

package com.app;

import com.jk.web.embedded.JKWebApplication;

public class App {
	public static void main(String[] args) {
		JKWebApplication.run();
	}
}

Alternatively, you can clone or download the tutorial repository then import the project into your IDE.

How to run Project:

  1. Set up the project with the content shown above.

  2. Inside your IDE, go to the src/main/java/com/app/App.java class.

Main Class

  1. Next, run it as a Java Application.

Run as Java Application

  1. Your program will start running and will open your browser to show you the result of the run. Alternatively, after running the code, you can open any browser to localhost on port 8080, and it will show you the run output as well.

localhost:8080

Example Explanation

  1. This example prompts you to input your name in the given field.

Example Run 1

  1. After you fill out your name, if you press the [Say Hello] button, it will give you a suitable response using the name you just input.

Example Run 2