Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The first time you use GWT, especially if you have only developed front-end websites, it can be difficult. It's possible that you are a CSS and HTML ninja, but you can't get your head around Java. The purpose of this tutorial is to introduce you to the use of style sheets in GWT. This tutorial assumes that you have a running GWT application and won't explain how to create one. As a result, we can focus on style sheets in GWT.
The CSS File
We will create a CSS file called style.gss under src/main/resources/com/company/project/client/resources/css. That's right, .gss files instead of .css files, since GSS files are like CSS files, but more powerful.
To verify that the file works, we should add at least one line. Let’s use :
body { background-color: pink; }
Currently, it shouldn't be working.
The Resource File
Our next step will be to create the resource file that will connect your GSS file to your Java classes. On completion, GWT obfuscates class names, so this file will be used to link the obfuscated names to the corresponding classes in your GSS.
In this case, the ensureInjected() function is crucial, since it injects your CSS into the DOM.
It is important to bind the ClientModule to this file, which will likely be located under src/main/java/com/company/project/client/gin/ClientModule.java in your project.
package com.company.project.client.gin;
// [your imports]
public class ClientModule extends AbstractPresenterModule {
@Override
protected void configure() {
// [your code]
bind(ResourceLoader.class).asEagerSingleton();
}
// [you might have code here too]
}
Activate GSS
Since version 2.7, GSS has been included in GWT, but isn't enabled by default. Your GWT module needs to have a single line of code added to enable it. I have this file located at src/main/java/com/company/project/AppModule.gwt.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.7.0//EN"
"http://gwtproject.org/doctype/2.7.0/gwt-module.dtd">
<module rename-to="AppModule">
// [Inheriting some stuff]
// [Source some stuff]
// The line that you need to add
<set-configuration-property name="CssResource.enableGss" value="true">
// [Some more code extending ... thing?]
</module>
You can also try this code with Online Java Compiler
There you go! As long as everything went well, your application should build and run, loading your style.gss file. Don't worry if it fails, since your application might be structured differently than the tutorial shows. You should be able to fix compile errors if you take the time to read them. Those of you who don't, but have access to a backend dev near you can always ask for help.
Using Classes
When working with GSS files in GWT, one of the most confusing things is having to declare your classes inside of your resource file to access them.
In our style.gss file, let's add the following classes:
Congratulations!🎉 In addition to your views (files ending in .ui.xml), you can now use your Java classes as well
The first thing we need is a view. Our file will look like this, titled ApplicationView.ui.xml:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'>
<div>
<h1>Hello World!</h1>
<p>This is my first paragraph.</p>
<p>This is my second paragraph.</p>
</div>
</ui:UiBinder>
Our classes must be imported into the view before they can be used:
Using resources.style.name_of_my_class, we can now call the required classes
This would be the final code based on the precedent view:
<ui:UiBinder xmlns:ui='urn:ui:com.google.gwt.uibinder'>
<ui:with field="resources" type="com.company.project.client.resources.AppResources"/>
<div>
<h1>Hello World!</h1>
<p class="{resources.style.my_first_class}">This is my first paragraph.</p>
<p class="{resources.style.my_second_class}">This is my second paragraph.</p>
</div>
</ui:UiBinder>
It is important to understand that Java uses the CamelCase naming convention, which means that class names with hyphens will not compile. You need to use @ClassName within your CSSResource to resolve this issue:
@ClassName("my-class-name")
String myClassName();
You can also try this code with Online Java Compiler
In order to use images inside of your GSS file, you will need to declare them as ImageResources. As with your GSS files, it's always a good idea to maintain an organized folder structure for your image inside your resources folder.
It enhances the appearance of a website and creates a great user experience.
What are the 3 CSS principles?
CSS is built and maintained based on three principles: consistency, succinctness, and separation (or CSS).
Can CSS classes inherit?
There is no 'inheritance' in CSS, unfortunately.
Why CSS is called cascading?
When more than one style rule matches an element, cascading is used to determine which applies.
How many types of CSS are there?
CSS can be categorized as follows: Inline CSS, Internal or Embedded CSS, and External CSS.
Conclusion
CSS in GWT can be challenging at first, but once you master the basics, you'll be a front-end ninja in no time. Adapt your workflow to your needs, structure your files, and have fun! You can also visit this link if you want to learn more about other JAVA frameworks.