DEV Community

Cover image for WebForms.java Update to WebFormsJS 1.6
Elanat Framework
Elanat Framework

Posted on • Originally published at elanat.net

WebForms.java Update to WebFormsJS 1.6

Good news for Java developers: You can now experience WebForms Core technology in Java. Elanat has update the WebForms class for Java with the latest version of WebFormsJS, 1.6. The WebForms class on the server and the WebFormsJS library on the client constitute the WebForms Core technology.

By using WebForms Core technology, you don't need to develop a front-end, because all HTML tags are managed from the server.

The video below demonstrates the functionality of WebForms Core technology for content management.

Download Links

You can download the WebForms classes for the Java programming language from the link below.

WebForms.java

To download the latest version of WebFormsJS, visit the following link.

Download WebFormsJS

Example: Using WebForms Core in Spring Boot

Below is an example demonstrating how to use Webforms Core with Spring Boot. In this example, the background color and text color of the "form" tag will change to the colors selected in the TextBoxes after clicking the button.

In the view below, after the "form" tag, there is a "p" tag that holds the value of the "WebFormsTag" variable. The value of the "WebFormsTag" variable will be set through the controller.

View (default.html file)

<!DOCTYPE html>
<html>
<head>
  <title>Using WebForms Core</title>
  <script type="text/javascript" src="/script/web-forms.js"></script>
</head>
<body>
    <h1>WebForms Core Technology in JAVA</h1>
    <form method="post" action="/" >
        <label for="TextColor">Set Text Color</label>
        <input name="TextColor" id="TextColor" type="text" /><br><br>
        <label for="BackgroundColor">Set Background Color</label>
        <input name="BackgroundColor" id="BackgroundColor" type="text" /><br><br>
        <input name="Button" type="submit" value="Click to send data" />
    </form>
    <p th:utext="${WebFormsTag}"></p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

This controller class has a three method; Each method handles different types of HTTP requests and processes the input data using the WebFormsCore library to dynamically update the form on the webpage.

Controller

package com.example.demo;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import WebFormsCore.WebForms;
import WebFormsCore.InputPlace;
import WebFormsCore.Fetch;
import WebFormsCore.HtmlEvent;

@Controller
public class MyController {

    @GetMapping("/")
    public String PageLoad(Model model) {

        WebForms form = new WebForms();

        form.setGetEvent("BackgroundColor", HtmlEvent.OnKeyUp, "/set-color");
        form.setGetEvent("TextColor", HtmlEvent.OnKeyUp, "/set-color");

        model.addAttribute("WebFormsTag", form.exportToWebFormsTag(null));

        return "default"; // This will map to default.html
    }

    @GetMapping("/set-color")
    public ResponseEntity<String> PageLoad(@RequestParam(name = "set-text-color", required = false) String Color) {

        WebForms form = new WebForms();

        form.saveValue("BackgroundColor","*");
        form.setBackgroundColor("BackgroundColor", Fetch.saved("*"));
        form.saveValue("TextColor","*");
        form.setTextColor("TextColor", Fetch.saved("*"));
        form.setCache();

        return ResponseEntity.ok(form.response());
    }

    @PostMapping("/")
    @ResponseBody
    public String PageLoad(@RequestParam("BackgroundColor") String backgroundColor, @RequestParam("TextColor") String textColor) {

        WebForms form = new WebForms();

        form.setBackgroundColor(InputPlace.tag("form"), backgroundColor);
        form.setTextColor(InputPlace.tag("form"), textColor);
        form.setDisabled(InputPlace.name("Button"), true);
        form.addTag(InputPlace.tag("form"), "h3", null);
        form.setText(InputPlace.tag("h3"), "The background color and the text color was set!");
        form.delete(InputPlace.tag("h3"));
        form.assignDelay(3, -1);

        return form.response(); 
    }
}
Enter fullscreen mode Exit fullscreen mode

The GIF image below shows how the above code works.

WebForms Core in JAVA

My first experience with Spring Boot

As someone who first created a simple MVC example with the Spring Boot framework, I must say that the crucial aspect of this framework is Mapping. In this example, I specified the "/set-color" path in the SetGetEvent method in the WebForms class and coded the necessary commands using @GetMapping("/set-color").

Initialization

First, we need to import the classes required in the WebForms Core technology. WebForms is the main class in this technology and the other classes play a supporting role.

import WebFormsCore.WebForms;
import WebFormsCore.InputPlace;
import WebFormsCore.Fetch;
import WebFormsCore.HtmlEvent;
Enter fullscreen mode Exit fullscreen mode

GET request (Initial Page Load):

@GetMapping("/")
public String PageLoad(Model model) {

    WebForms form = new WebForms();

    form.setGetEvent("BackgroundColor", HtmlEvent.OnKeyUp, "/set-color");
    form.setGetEvent("TextColor", HtmlEvent.OnKeyUp, "/set-color");

    model.addAttribute("WebFormsTag", form.exportToWebFormsTag(null));
    return "default"; // This will map to default.html
}
Enter fullscreen mode Exit fullscreen mode

PageLoad Method with GetMapping("/")

  • Initializes a new WebForms instance.
  • Sets events for BackgroundColor and TextColor inputs that trigger the "/set-color" path on the OnKeyUp event.
  • Exports the WebForms data to the model attribute WebFormsTag.
  • Returns the view name "default" which maps to "default.html".

Key up event

@GetMapping("/set-color")
public ResponseEntity<String> PageLoad(@RequestParam(name = "set-text-color", required = false) String Color) {

    WebForms form = new WebForms();

    form.saveValue("BackgroundColor","*");
    form.setBackgroundColor("BackgroundColor", Fetch.saved("*"));
    form.saveValue("TextColor","*");
    form.setTextColor("TextColor", Fetch.saved("*"));
    form.setCache();

    return ResponseEntity.ok(form.response());
}
Enter fullscreen mode Exit fullscreen mode

PageLoad Method with GetMapping("/set-color")

  • Initializes a new WebForms instance.
  • Saves and fetches values for BackgroundColor and TextColor inputs.
  • Sets the background and text colors based on saved values.
  • Caches the changes and returns the response as a String.

Note: The general structure of WebForms Core technology is that the client makes a request to the server online and the server sends commands to the client. However, this technology also supports offline mode. In this method, the tag values ​​are first saved and then those values ​​are retrieved again without the need for server data. Calling the setCache method causes the client to make a request to the server only once. Offline mode in WebForms Core technology provides a high-level structure like React Server Components.

Form submit

@PostMapping("/")
@ResponseBody
public String PageLoad(@RequestParam("BackgroundColor") String backgroundColor, @RequestParam("TextColor") String textColor) {

    WebForms form = new WebForms();

    form.setBackgroundColor(InputPlace.tag("form"), backgroundColor);
    form.setTextColor(InputPlace.tag("form"), textColor);
    form.setDisabled(InputPlace.name("Button"), true);
    form.addTag(InputPlace.tag("form"), "h3", null);
    form.setText(InputPlace.tag("h3"), "The background color and the text color was set!");
    form.delete(InputPlace.tag("h3"));
    form.assignDelay(3, -1);

    return form.response(); 
}
Enter fullscreen mode Exit fullscreen mode

PageLoad Method with PostMapping("/")

  • Takes BackgroundColor and TextColor as parameters.
  • Sets the background and text colors of the form based on the inputs.
  • Disables the submit button ("Button").
  • Adds and deletes a temporary heading ("h3") with a message.
  • Sets a delay for the next action.
  • Returns the response as a String.

Note: Please note that when the HTML page is requested for the first time, if you are using the WebForms class, use the exportToWebFormsTag method. For secondary requests, use the response method.

You can get the sample code in link below.
https://elanat.net/content/31/WebForms.java Update to WebFormsJS 1.6.html

WebForms Core technology is also supported by all JVM-based programming languages!

In addition to the Java programming language, you can use the WebForms classes located in the WebFormsCore directory directly or after compilation in the following programming languages (or implementations):

  • Kotlin
  • Scala
  • Groovy
  • Clojure
  • JRuby
  • Jython
  • Fantom
  • Mirah
  • Ceylon
  • Fantom
  • JPHP

Conclusion

With this update, Java developers can leverage WebForms Core, which allows them not only create more dynamic applications but also simplify their workflow significantly since no front-end knowledge is required anymore! This technology supports all JVM-based languages including Kotlin!

Top comments (0)