Small things: For Each in different languages

In web programming, a lot of times you will have to handle lists or other types of collections, passing through the collection and doing whatever you want to do with the collection’s elements. There are some ways to do that, so I will tell one way I use a lot and it’s very simple to use, besides being an elegant way of programming.

I use the forEach iteration.

Let’s see an example in Java:

Supposing we have a very simple class called Person.java

public class Person {

    private String name;

    private Integer age;

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

And we will iterate over a list of Persons (personList):

// supposing we have a list of Persons (List<Person>)
if(personList != null) {
    for(Person person : personList) {
        System.out.println("Name: " + person.getName());
        System.out.println("Age: " + person.getAge());
    }
}

It’s always a good practice checking if the list is not null, to avoid java.lang.NullPointerException.

Let’s see an example in jsp:

In jsp, let’s suppose that we will receive a list of Persons, just like the above example. The list is called personList (List<Person>).

In jsp, to iterate over a list loop we can use a core tag lib called forEach.

<c:forEach var="person" items="${personList}">
    <c:out value="${person.name}"/><br/>
    <c:out value="${person.age}"/><br/>
<c:forEach>

In the above code we show all Persons. We use the core tag lib out to print the data from each object in the list. The personList and its’ objects ate caught via JSP Expression Language.

Let’s see some examples in Javascript:

In javascript we can iterate over an array, using a Javascript’s own commands. This link is a StackOverflow question/answer which covers that.

Another way to do this is using jQuery, which has a command to iterate over arrays. See this link to learn more about jQuery .each().

That’s all folks!

I hope I helped somebody using forEach way to iterate over lists/collections. Any doubt, I’m ready to solve your problem! Thanks!

Starting with Maven!

This tutorial will teach you important things about Maven, a powerful program to help you to organize your project and handle your project’s dependencies.

Maven – What is

Basically Maven is a very useful program for medium/large projects written in Java that helps you in basically 2 things that are more interesting to us:

  • Project organization

  • Dependencies manipulation

We’ll talk about each of the above things soon.

Maven is practically required for big applications and big enterprises have to use that because it optimizes certain project processes.

Fortunately, today Maven comes already integrated with Eclipse, so you don’t even need to install it! Only use and be happy! =)

Project Organization

When you open Eclipse, you can create a lot of kinds of projects without using Maven. You can create a Java project, to create applications that run as Desktop programs. You can also create a Web project, which interests to us.

Web project

The Web project supports codes written in Java, HTML, CSS, Javascript… This project runs in a server, such as Tomcat and the interface appears in the browser. Ok. You can also save things in the database. Ok.

What happens is that Web project Eclipse offers to you is in fact very rudimentary. Ancient web applications written in Java were like that (called Java EE with Java Servlets). This project format left the its organization a mess. Its what we want to avoid, since we want to create a project that will be easy to understand in the future, even for your business’s new employees.

Therefore, we will create a Maven Web project, which will come with everything separated in your correct place, everything organized in its structure. We’ll see that soon.

Dependencies manipulation

Dependencies

A dependency is nothing more than a .jar file, which is like a Java project compressed and you can use in your own project, like a library. Let’s see an example:

Last month you created a project that raffled random numbers (any project…). Today you want to create another project, but you want to use the later platform that you implemented (about random numbers raffle) in your new code. You don’t need to put all the files from old to the new project, you can simply compress your old code in a .jar and add it to your new project. Your project will be able to instantiate your the old classes and call its methods.

And that is very important. Dependencies are nothing more than libraries. For example, to execute operations in an image, we can use some library that make images processing and for that we need to add its .jar in our project. The same thing happens with Hibernate for example, which helps handling database stuff: we need to put the Hibernate’s .jars in our project to can use that.

Where does Maven enter in this history?

Maven helps you to handle these dependencies.

These dependencies are often global and lots of people use them, like Hibernate, Spring, MySql… And those dependencies have differences about version. Downloading these dependencies often is well painful, since finding the .jars up to finding the .jars with compatible versions.

With Maven, you only need to add some lines of XML code in your pom.xml and it finds the library for you and leave it ready to use in your project.

PS.: Maven finds dependencies which are registered in its repository. So, the big enterprises put its dependencies in Maven repository. A lower library, not too known, probably won’t be in Maven repository. In that case you can’t use Maven to import that library. It has to be done manually, adding the .jar files.

Next

Next we will learn how to create a Maven Web project in the Eclipse!