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

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

Get a list of Object from jdbcTemplate()

Posted in IT StuffTagged

Sometime happens get a list of object from database and, to do that without call a RowMapper, use this: List<Object> strings = (List<Object>) jdbcTemplate.queryForList(query, Object.class);List<Object> strings = (List<Object>) jdbcTemplate.queryForList(query, Object.class); This is a sample to get a list of String: List<String> strings = (List<String>) jdbcTemplate.queryForList(query, String.class);List<String> strings = (List<String>) jdbcTemplate.queryForList(query, String.class);

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

JSP Error : Attribute qualified names must be unique within an element

Posted in IT StuffTagged ,

Today I updated an old Spring MVC application to Apache Tomcat 7 and some other newer jars and, when I started it, I get this error: SEVERE: Servlet.service() for servlet [mvc-dispatcher] in context with path [/CG] threw exception [/WEB-INF/view/main.jsp (line: 2, column: 0) /WEB-INF/view/include.jsp (line: 3, column: 75) Attribute qualified names must be unique within….

How to download PDF file from url on MVC controller

Posted in IT StuffTagged ,

To download a remote file (like a PDF) redirecting to response output, use these instructions to update a your Spring MVC controller: @RequestMapping(value="/viewAttach", method = RequestMethod.GET) public ModelAndView viewAttach(@RequestParam(value="article_id", required = true) String article_ref, HttpSession session, HttpServletResponse response) { /* *** Remember to check if Session still valid *** */ try {   URL url….

Route a message to MongoDB

The requirement is very simple. Route an XML message from rabbitMQ to MongoDB. MongoDB BSON as the data storage and network transfer format for “documents”. BSON is a binary-encoded serialization of JSON-like documents. So, the source message is in a XML format, after getting it from rabbitMQ is necessary to translate into a JSON format….

Camel and RabbitMQ : Finally, how to!

Define a RabbitMQ broker endpoint in Camel is possible with the Bluelock camel-spring-amqp (https://github.com/Bluelock/camel-spring-amqp) library. It’s an Apache Camel component that allows to natively communicate with a RabbitMQ broker and it’s implemented using Spring’s AMQP. For first, with Eclipse IDE create a new Maven Project with Artifact ID camel-arthetype-spring. This allows using Spring DSL to….

Put SQL result into a map

Posted in IT StuffTagged , ,

To extract two or more object (in this sample two Integer) from a DB and putting into a map, use this: final Map<Integer,Integer> topic = new HashMap<Integer,Integer>();   getJdbcTemplate().query(sqlCommand, new Object[] {query_parameter}, new RowMapper<Object>(){ public Object mapRow(ResultSet rs, int arg1) throws SQLException { topic.put(rs.getInt("integer1"), rs.getInt("integer2")); return null; } });final Map<Integer,Integer> topic = new HashMap<Integer,Integer>(); getJdbcTemplate().query(sqlCommand,….