Friday, 28 October 2011

Dependency tree in maven

To see dependency tree in maven use command:
mvn dependency:tree

Tuesday, 25 October 2011

How to create java object in Freemarker?

If you are using Freemarker integrated with struts, look at the class org.apache.struts2.views.freemarker.FreemarkerResult. In this class there is a method createModel(). There you have a list of objects inserted into model used in freemarker rendering.

  • Application - servlet context attributes hash model
  • JspTaglibs - jsp tag lib factory model
  • Request - request attributes hash model
  • Session - session attributes hash model
  • request - the HttpServletRequst object for direct access
  • response - the HttpServletResponse object for direct access
  • stack - the OgnLValueStack instance for direct access
  • ognl - the instance of the OgnlTool
  • action - the action itself
  • exception - optional : the JSP or Servlet exception as per the servlet spec (for JSP Exception pages)
  • struts - instance of the StrutsUtil class

In the ftl code you can use struts variable:

struts.bean("java.util.Date")
It will return object of this type, constructed with zero parameter constructor.

Format date in java with months' name changed

I was given the task to format dates. The requester needed months' name appropriate case endings.

It can be done be SimpleDateFormat class: (this example is for Polish language)

SimpleDateFormat sdf = new SimpleDateFormat("dd-MMMM-yyyy");
DateFormatSymbols dfs = new DateFormatSymbols();
dfs.setMonths(new String[]{"stycznia","lutego","marca","kwietnia","maja","czerwca","lipca","sierpnia","września","października","listopada","grudnia"});
sdf.setDateFormatSymbols(dfs);
String output = sdf.format(new Date());

Heap pollution

What is it?

Java Language Specification, Third Edition
§4.12.2.1 Heap Pollution
It is possible that a variable of a parameterized type refers to an object that is not of that parameterized type. This situation is known as heap pollution. This situation can only occur if the program performed some operation that would give rise to an unchecked warning at compile-time.

Here is an example:

List l = new ArrayList();
List l1 = l;
l.add("one");
Integer i = l1.get(0);

In java 7 the following additions help to deal with Heap Pollution:

  • A new mandatory compiler warning is generated on declaration sites of problematic varargs methods that are able to induce contributory heap pollution.
  • The ability to suppress those mandatory warnings at a declaration site using an @SuppressWarnings("varargs") annotation. The warnings may also be suppressing using the -Xlint:-varargs option to the compiler.
  • If the @SuppressWarnings("varargs") annotation is used on a problematic varargs method declaration, the unchecked warnings at call sites of that method are also suppressed.

Saturday, 22 October 2011

SpeakIt! - Chrome extension

Now you can mark a text in your Chrome browser and let the computer speak it!. Just install Chrome extension SpeakIt. You can find it here: SpeakIt.

Limit on Post Request - maxPostSize

Recently I was faced with a problem concerning a web page form. It stopped working without any change in the code. I have checked that due to database increase in this form I have over 50000 hidden inputs. So it looks like the server did not cope with such a big number of parameters in the post request.
I've found in the Tomcat and Jboss Connector configuration that we can define a constant maxPostSize that is the maximum size in bytes of the POST request that is handled by the container FORM parameter parsing. The limit can be disabled by setting this attribute to a value less than or equal to 0. If not specified, this attribute is set to 2097152 (2 megabytes).

Wednesday, 19 October 2011

STS 2.8.0 released. Supports Java 7

The new 2.8.0 release of the SpringSource Tool Suite (STS) is available. Now it has full IDE support for Java 7!
For more details browse to: http://www.springsource.org/node/3287