Tag Archives: java

Dynamic for with Spring MVC using a HashMap

Sometime you need to dynamically generate a form without knowing how many fields it will be required (i.e. when your form is driven by a configuration or by some properties). The problem is to draw the form and, return the values to the Controller, and recognize the couples Field name / Field value after the submit..

You can easily get solve this problem just adding HashMap which will hold the key-value pair data to the DataModel.

Supposing your configuration says: you have to draw two fields and these are the name, you UI will be something like*:

<c:forEach items="${newRequest.fields}" var="field">
	<f:input type="text" path="rawFields['${field.field_id}']" class="form-control validate[groupRequired[mandatoryField]]" /> (R)

When you submit the form, the values and the key for the dynamic fields will be filled.

* newRequest is the DataModel you are passing and fields is the list of Fields that user will fill with data, like that:

public class Request {
	/** Request type */
	private int templateRequest;
	// ***** Input field ***** 
	List<RequestField> fields = new ArrayList<RequestField>();
	private HashMap<String, Object> rawFields = new HashMap<String, Object>();
	[Setters and getters]
Tagged , , ,

Results pagination with MongoDB and Java

To implement the MongoDB results pagination in Java, there are few parameters that need to be collected:

1) the order of results (ascending / descending)
2) the field used to order the results
3) the number of element and the page selected
4) the field and the value used to filter the results

As well as the results of query, the method needs to return the total number of elements. All returned elements will be saved in a HashMap.

HashMap&lt;String, Object&gt; resultMap = new HashMap&lt;String, Object&gt;();
Direction direction = Sort.DEFAULT_DIRECTION;
if (sortDirection &gt; 0) {
	direction = Sort.Direction.ASC;
} else { 
	direction = Sort.Direction.DESC;

If a pagination is required, skip and limit are used

if (pageSize &gt; 0) {
	query.skip((pageNum - 1 ) * pageSize);
if ( sortField != null &amp;&amp; !sortField.equals("") ) {				
	query.with(new Sort(direction, sortField));
results = mongoTemplate.find(query, Object.class);

If a pagination is required, queryCounter (basically a version of query without pagination an limit) is used to calculate the total number of results. Of course, if pagination is not required, is possible to use directly the size of results.

if ( pageSize &gt; 0 ) {
	resultMap.put("RESULT_SIZE", (int) mongoTemplate.count(queryCounter, Object.class));
} else {
	// If pagination is not required, the query is not re-executed
	resultMap.put("RESULT_SIZE", results.size());

mongoTemplate is a spring bean defined in this way on context configuration:

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"  c:mongo-ref="mongo" c:databaseName="${mongo.db.name}">
	<property name="writeConcern" value="SAFE" />
Tagged ,

MongoDB query with logical and condition in Java

Suppose you need to apply some filters to your MongoDB query, for example to extract some _ids that match a regex condition. This is the way to do that:

Query query;
query.addCriteria(Criteria.where("_id").in(IDs).and(query_field).regex(".*" + query_value + ".*", "i"));

In this example I used the Query (see here) and Criteria (see here) classes

And, this is the query you can use in mongoDB (Robomongo* or command line):

Query: { 
	"_id" : { "$in" : [ "ID1" , "ID2" , "ID3" ]} , 
	"detail.medicationBrandName" : { "$regex" : ".*x.*" , "$options" : "i"}}, 
	Fields: null, Sort: { "medicationGenericName" : -1}

*Robomongo: is a shell-centric cross-platform open source MongoDB management tool (i.e. Admin GUI). Robomongo embeds the same JavaScript engine that powers MongoDB’s mongo shell. You can download it here.

Tagged , ,

dataSource: The name of the property, following JavaBean naming conventions.

Suppose you defined TemplateDao Spring bean in this way:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">	
	<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>	
	<property name="url"><value>jdbc:mysql://${mysql.hostname}:${mysql.port}/${mysql.db}</value></property>
	<property name="username"><value>${mysql.user}</value></property>
	<property name="password"><value>${mysql.password}</value></property>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
<bean id="TemplateRequestDao" class="com.afm.admin.dao.mysql.TemplateDaoMySql">
	<property name="dataSource" ref="dataSource" />

and you get this error message: The name of the property, following JavaBean naming conventions. The reason is probably you forgot to extend your implementation class:

public class TemplateDaoMySql extends JdbcDaoSupport implements TemplateDao {
	public List<TemplateRequest> getTemplateRequest(UserProfile userProfile) {
		// TODO Auto-generated method stub
		return null;

Time lost to fix this issue: more or less 1 hour. Image the how many I was frustrated when I discovered what was the problem.

Tagged , ,

Velocity generates NullPointerException while evaluating template

While evaluating template with Velocity I am affecting a null point exception but happened only on production environment because it seems that the application is trying to write to tomcat/bin the velocity.log file but it failed due to the permission. I tried to add velocity.property file:

# Fri Dec  6 10:14:26 EST 2013 - Mandatory to prevent Velocity null point exception

but, after every startup I get the same error and, to fix that, it seems to be mandatory to remove the workdir tomcat folder. It’s not acceptable so, I tried to add this row before init the velocity:

Velocity.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");

Of course, my next step is to migrate to Freemarker because Velocity isn’t really under active development any more.

Tagged , ,

Cannot nest ‘PRJ/src/main/resource’ inside ‘PRJ/src’

I created a new Web application using Eclipse and, after Enabling Maven, I get this error updating the project:

Cannot nest 'PRJ/src/main/resource' inside 'PRJ/src'. To enable the nesting exclude 'main/' from 'PRJ/src'

Basically I removed the folder /src and create folder /src/main/java, /src/main/resources etc and this fight with the sourceDirectory tag in Build section of maven pom. To fix that, just remove this row and update the project:

Tagged ,

Spring-data: Cannot use a complex object as a key value

I was trying to figured out how to solve this issue. Basically I’m saving a user profile bean that contains multiple occurrences of other beans inside him. Something like date:

public class UserProfile {
	List<Class1> classes1;
	List<Class2> classes2;
	Integer int1;
	Map<String, Class3> classes3;

I was working fine until I changed a getter adding some business functionalities and surrounding all lines with a try / catch. Of course, I putted the exception message on the log using adding this definition inside the bean:

protected final Log logger = LogFactory.getLog(getClass());

Good point for me, everything worked fine until I updated the object in Mongo. Holy crap, I got the “Cannot use a complex object as a key value” exception and I spent two hours trying to change the beans (of course I did a lot of other changes after the fatal adding of business logic inside my bean). Exhausted, I contacted my colleague Max (@maxfarnea) and he told me, “when I got this error, I removed the log inside a bean”. Ipso facto, I removed the log entry from my bean, added a throws exception instead of a try / catch and tested! Well done @maxfarnea, it works!

I learned two lesson today: one, don’t put log into beans that need to be stored using spring-data and, two, if you are in pain, ask, talk, don’t be silly, don’t waste your time!!! Stackoverflow.com is a good point to start (and my blog either, of course) but never is more helpful than a quick chat with a colleague!

Tagged , ,

No suitable driver found for jdbc:mysql://localhost:3306/schema

If the exception “Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/schema” is raised probably you forgot to add the

<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>

property to your dataSource bean:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">		
	<property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
	<property name="url"><value>jdbc:mysql://</value></property>
	<property name="username"><value>root</value></property>
	<property name="password"><value>password</value></property>
Tagged , , ,

How to call a function whose name is stored in a string variable

The objective is simple, create a method which load a class dynamically, access its method and passing their parameters value and getting the return value at run-time. To do that is possible to use Java reflection.

try {
	Class<?> clazz = Class.forName("package.myclass");
	Method mth = clazz.getDeclaredMethod("method", String.class, String.class);
	Object source_class = clazz.newInstance(); 
catch (SecurityException ex) {				
	return null;
catch (NoSuchMethodException ex) {
	return null;
catch (IllegalArgumentException ex) {
	return null;
catch (IllegalAccessException ex) {
	return null;
catch (InvocationTargetException ex) {
	return null;
logger.debug((String) mth.invoke(source_class, "Param1", "Test ${medicationGenericName}"));

In this case the method needs two string as parameter and returns a string.

If the constructor of the class needs a parameter (i.e. the application context because the @Autowired doesn’t works), this is the code needed:

Class<?> clazz = Class.forName("package.myclass");
Constructor <?> constructor = clazz.getDeclaredConstructor(ApplicationContext.class);
Method mth = clazz.getDeclaredMethod("method", String.class, String.class);				
Object source_class = constructor.newInstance(applicationContext);

‘Must Override a Superclass Method’ Errors after importing a project into Eclipse

Eclipse is defaulting to Java 1.5, when you want it to use Java 1.6.

You have classes implementing interface methods, which in Java 1.6 can be annotated with @Override; however, in Java 1.5, @override could only be applied to methods overriding a superclass method.

Go to your project/ide preferences and set the “Java compiler level” to 1.6 and also make sure you select JRE 1.6 to execute your program from Eclipse. Check also the JRE System Library on Java Build Path properties of your project.