Surfing With Code

Tricks for developers about Java, HTML5, CSS, JavaScript, XML, jSON, deployment, design …

Groovy: the Elvis Operator ?: — agosto 25, 2016

Groovy: the Elvis Operator ?:

Groovy is good at shortening normal Java statements. The Elvis operator is to shorten the ternary operator. If we have a sensible default when the value is null or false (following Groovy truth rules) we can use the Elvis operator. And why is it called the Elvis operator? Turn your head to the left and you will know.


def sampleText

// Normal ternary operator.
def ternaryOutput = (sampleText != null) ? sampleText : 'Hello Groovy!'

// The Elvis operator in action. We must read: 'If sampleText is not null assign
// sampleText to elvisOuput, otherwise assign 'Viva Las Vegas!' to elvisOutput.
def elvisOutput = sampleText ?: 'Viva Las Vegas!'
<div class="line number0 index0 alt1 break"></div>

Via: MRHAKI blog

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04 — julio 6, 2016

Keyboard locks in IntelliJ IDEA on Ubuntu 14.04

When you use Intellij IDEA 13 on Ubuntu 14.04 and from time to time the keyboard completely (yep, completely, it’s not about shortcuts) locks in IDEA. You still can do anything you want in IDEA with your mouse, also you can use the keyboard everywhere else except IDEA. And it can be fixed only by restarting IDEA so far… Howewer, you have a better solution:


ibus-daemon -rd

Enjoy!

Why enclose JavaScript code in a functions? — febrero 2, 2016

Why enclose JavaScript code in a functions?

Lately, specially with the last JavaScript frameworks, it is common to see the JS code (or files) enclosed in a function as follows:

(function() {
... all javascript code for the file ...
})();

Variables in Javascript have function scope. You’re wrapping your code in a function in order for it not to clobber the global namespace with tons of variables, which may lead to bugs later on when different code is added. E.g.:

// module 1
(function () {
var foo = 'bar';
...
})();

// module 2
(function () {
var foo = 'baz';
...
})();

By this way there is no problems, because both modules have their own variable scopes.

Integrate Spring and Webapp project — enero 26, 2016

Integrate Spring and Webapp project

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web.xml and use a contextConfigLocation to set which context files to load.

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>

	<listener>
	<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> 

 

Then, you can use the WebApplicationContext to get a handle on your beans or handle them using “@Autowired” annotation

 

 

 

Set different loglevel for different packages/classes —
What are the key differences between SQL Query and Hibernate Query Language (HQL)? — noviembre 19, 2015

What are the key differences between SQL Query and Hibernate Query Language (HQL)?

Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries which in turns perform action on database.

  • SQL is based on relational database model whereas HQL is a combination of OOPS with Relational database concept.
  • SQL manipulates data stored in tables and modifies its rows and columns while HQL is concerned about objects and its properties.
  • SQL is concerned about the relationship that exists between two tables while HQL is concerned about relation between objects.

Source: differencebetween.net

How to rename file in Java — noviembre 16, 2015

How to rename file in Java

Java comes with renameTo() method to rename a file. However , this method is platform-dependent. So, the return value (true if the file rename successful, false if failed) should always be checked to make sure the file is rename successful.

package com.surfingwithcode.file;

import java.io.File;

public class RenameFileExample 
{
    public static void main(String[] args)
    {	
    	
		File oldfile =new File("oldfile.txt");
		File newfile =new File("newfile.txt");
		
		if(oldfile.renameTo(newfile)){
			System.out.println("Rename succesful");
		}else{
			System.out.println("Rename failed");
		}
    	
    }
}

Source: Mkyong.com

Responsive Full Background Image Using CSS — noviembre 3, 2015

Responsive Full Background Image Using CSS

In this tutorial, we’ll go over the simplest technique for making a background image fully stretch out to cover the entire browser viewport. We’ll use the CSS background-size property to make it happen; no JavaScript needed.

HTML CODE

<!doctype html>
<html>
<body>
  <strong>...Your content goes here...</strong>
</body>
</html>

CSS CODE

body {
 /* Location of the image */
 background-image: url(images/background-photo.jpg);
 
 /* Background image is centered vertically and horizontally at all times */
 background-position: center center;
 
 /* Background image doesn't tile */
 background-repeat: no-repeat;
 
 /* Background image is fixed in the viewport so that it doesn't move when 
 the content's height is greater than the image's height */
 background-attachment: fixed;
 
 /* This is what makes the background image rescale based
 on the container's size */
 background-size: cover;
 
 /* Set a background color that will be displayed
 while the background image is loading */
 background-color: #464646;
}
<pre>

Keep that in mind as you choose which image you’re going to use

Android: Errors in layouts files in Eclipse — octubre 7, 2015

Android: Errors in layouts files in Eclipse

If you receive the following error when coding a layout xml file for an android app in Eclipse, don’t be panic, there is a solution!

“Incorrect line ending: found carriage return (\r) without corresponding newline (\n).

Follow the follogin steps:

  1. Open layout file
  2. Ctrl+Shift+F (Command+Shift+F in MacOSX)
  3. Save the layout file and close it.
  4. Delete any .out files the builder might have created.
  5. Clean project.

Enjoy!

Seguir

Recibe cada nueva publicación en tu buzón de correo electrónico.

Únete a otros 30 seguidores