Dynamic for with Spring MVC using a HashMap

Posted in IT StuffTagged , , ,

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….

Results pagination with MongoDB and Java

Posted in IT StuffTagged ,

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….

MongoDB query with logical and condition in Java

Posted in IT StuffTagged , ,

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"));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….

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

Posted in IT StuffTagged , ,

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>   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean>   <bean id="TemplateRequestDao" class="com.afm.admin.dao.mysql.TemplateDaoMySql"> <property name="dataSource" ref="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://${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> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/>….

Velocity generates NullPointerException while evaluating template

Posted in IT StuffTagged , ,

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….

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

Posted in IT StuffTagged ,

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’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….

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

Posted in IT StuffTagged , ,

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;   }public class UserProfile { List<Class1> classes1; List<Class2> classes2; Integer….

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 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://172.16.0.11:3306/test_vale</value></property> <property name="username"><value>root</value></property> <property name="password"><value>password</value></property> </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://172.16.0.11:3306/test_vale</value></property> <property name="username"><value>root</value></property> <property….

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

Posted in IT StuffTagged

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….

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

Posted in IT StuffTagged

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”….