How To Automatically Download Generated XLS File on Client Side (Spring MVC + JXLS)

Sometimes we want to generate report to be downloaded by user without save it on the server side. So here is one of what I did for my web application. The process to create the document are do with the download process on client side. For this example, I create xls file using Jxls library.  But you will also need apache poi libs and apache common too for this process.

Download File Sample

xlsHasil

First of all, we need to prepare xls source file that we will use. Mine is like this

xlssource

Then I create simple form to send the parameter and call the controller. Here my code: Continue reading “How To Automatically Download Generated XLS File on Client Side (Spring MVC + JXLS)”

Advertisements

How To Upload Image And Display It On Spring MVC

Hi..

In this article I’ll give simple example how to upload image and display it on SpringMVC. We need to add some libs to support this feature. There are:

  • Apache common file upload
  • Apache common io

First, you need to add multipartresolver bean to your servlet configuration. As for me, here is what I added to my dispatcher-servlet.xml

<mvc:resources mapping="/js/**" location="/js/"/>
<mvc:resources mapping="/css/**" location="/css/"/>
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/font/**" location="/font/" />
	
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>

The key to display your uploaded image is put the image to folder that can be called from your view. From my dispatcher-servlet you can see that I will put the image to folder “img” and call it with url “[baseURL]/img/[nameOfImage]”. Here are some screenshot result of my code.

upload1

upload3 Continue reading “How To Upload Image And Display It On Spring MVC”

How To Show Data From Database Using SpringMVC + Hibernate + AngularJS

Hi.. in this article I will create simple web app using SpringMVC + Hibernate + AngularJS to show data from database. Some environment that I need are:

  1. SpringMVC libs.
  2. Hibernate libs.
  3. AngularJS.
  4. Another required libs like database connector, aopalliance, apache common.
  5. IDE. This time, I use netbeans.

I use mySQL as my database. The data that I want to show is from User table and has some data like this:

db

After create project that use SpringMVC and Hibernate framework, I setup application context, dispatcher servlet, and web.xml Continue reading “How To Show Data From Database Using SpringMVC + Hibernate + AngularJS”

How To Fix “Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]”

When I worked with my project wich using Hibernate + SpringMVC, I got this kind of error when I run my project:

SEVERE:   Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: 
start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sampleController': 
Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: 
private com.ariestania.sample.service.LogUserActionService com.ariestania.sample.controller.BaseController.logUserActionService; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'logUserActionService': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: 
private org.hibernate.SessionFactory com.ariestania.sampleservice.LogUserActionService.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: 
Invocation of init method failed; nested exception is org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

I though that error occured because Continue reading “How To Fix “Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]””

How To Upload and Read CSV File On SpringMVC Using OpenCSV

In this function, I use OpenCSV library to help me read csv file. You can read more about OpenCSV on their website.

The form on my jsp:

<form action="${baseURL}/uploadfile" method="POST" enctype="multipart/form-data" >
    <div class="form-group">
        <label>File input</label>
        <input type="file" name="file"/>
    </div>
    <br/><br/>
    <div class="form-group">
        <button type="submit" id="upload">Upload</button>
    </div>
</form>

My controller: Continue reading “How To Upload and Read CSV File On SpringMVC Using OpenCSV”

How To Use Multiple Database Connections On SpringMVC + Hibernate

In my experience, I met a rare case which is I should use more than one database connection on my web application. In that case, what I did was create some hibernate configuration file and call it whenever I need it. I have one main database which is use for my web application it self, and some hibernate configuration for another purpose.

Here are some snap of my code. I use oracle for my example.

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
      
    <context:component-scan base-package="com.ariestania.example" />
    <tx:annotation-driven /> 
 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
          p:driverClassName="oracle.jdbc.OracleDriver"
          p:url="jdbc:oracle:thin:@10.98.10.128:1521:examples"
          p:username="thisisuser"
          p:password="thisispass!"
          p:initialSize="5"
          p:maxActive="10"
          p:maxIdle="5"
          p:minIdle="2"
          p:maxWait="30000"
          p:removeAbandoned="true"
          p:removeAbandonedTimeout="30"
          p:validationQuery="SELECT 1 from dual"/>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
          p:dataSource-ref="dataSource"
          p:configLocation="classpath:hibernate.cfg.xml">
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
          p:sessionFactory-ref="sessionFactory"/>

</beans>

Continue reading “How To Use Multiple Database Connections On SpringMVC + Hibernate”

How To Fix Error 405 (Request method ‘POST’ not supported) On SpringMVC + SpringSecurity

I am new with springMVC technology. When I create my first form with post method, I got Error 405 (Request method ‘POST’ not supported).

error post method

After some googling, I found that the error was because I use csrf on my web application. I should add some code for token on my action form like “${_csrf.parameterName}=${_csrf.token}”. So, here are my code became: Continue reading “How To Fix Error 405 (Request method ‘POST’ not supported) On SpringMVC + SpringSecurity”