Follow by Email

Friday, December 28, 2012

corejava: File Upload example using spring

corejava: File Upload example using spring: Below is a running example to upload a file using spring , please add the below lone in your application-context file to add CommonsMulti...

Spring Security Implement Logout Handler

In your applicationContext-security.xml file add the success-handler like below
 <logout logout-url="/resources/j_spring_security_logout" success-handler-ref="" />

Create the Class which will be implemneting "" interface and in it's logout method do all the stuff you want at the time of logout.


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SpringSecurityLogoutHandler implements LogoutHandler {
 public void logout(HttpServletRequest request, HttpServletResponse arg1,
   Authentication arg2) {
  // Write your logout logic here

Spring MVC file upload example

Below is a running example to upload a file using spring , please add the below lone in your application-context file to add CommonsMultipartResolver

   <bean id="multipartResolver"


Write your JSP page like below , Please do add HTML code if you want to run it as a seprate page

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<div  xmlns:c="" xmlns:jsp="" xmlns:page="urn:jsptagdir:/WEB-INF/tags/form" xmlns:table="urn:jsptagdir:/WEB-INF/tags/form/fields" version="2.0">

         <form id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" class="cleanform">
          <c:if test="${successfull==Y}">
              <c:out value="File Was Successfully uploaded"></c:out>
          <label for="file">File</label>
          <input id="file" type="file" name="file" />
          <p><button type="submit">Upload</button></p> 

Write the controller to handle requests related to file upload

//import org.springframework.mvc.extensions.ajax.AjaxUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

public class FileUploadController {
       public void ajaxAttribute(WebRequest request, Model model) {

//            model.addAttribute("ajaxRequest", AjaxUtils.isAjaxRequest(request));

       public void fileUploadForm() {

@RequestMapping(method=RequestMethod.POST,produces =
       public String processUpload(@RequestParam MultipartFile file, Model model) throws IOException {

              model.addAttribute(  "message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
              model.addAttribute("successfull", "Y");
              //**This byte array can then be sent to any content managment server  to save the file
              byte[] fileByteArray=file.getBytes();
//**Or you may want to save the file using java IO on a folder somewhere in you server             
              return "fileupload/showFileUpload";
//this will render to a page called showFileUpload.jspx
       public String showFileUploadPage(){
              return "fileupload/showFileUpload";


Saturday, December 15, 2012

Deploy JPA2.0 application on weblogic10.3.3

Please note that in order to run your JPA2.0 application on weblogic10.3.3 which is JPA1.0 compliant you will have to rename your persistence.xml  to something like foo.xml and mentione the name of this xml file in your applicationContext.xml as (I am using Spring here )

   <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
        <property value="classpath:META-INF/foo.xml" name="persistenceXmlLocation"/>
        <property name="persistenceUnitName" value="persistenceUnit"/>
        <property name="dataSource" ref="dataSource"/>

rename the persistenceUnit and dataSource according to the beans you have defined in your application
and you will have to define package exclusions in your weblogic.xml file as
<?xml version="1.0" encoding="UTF-8"?>
  <!-- prefer-application-packages> <package-name>javax.persistence.spi.*</package-name>
   </prefer-application-packages -->

I invested few days to resolve the problem , and sharing the solution hoping it might benefit someone someday.

Below are some related threads I created for this problem , there you will find the details of problems you can face and the recomended solutions , but the solution provided above is working for me now and is gurenteed :).

If you want to understand why we have package exclusions in our weblogic.xml file then below blog will help you alot understanding class loading in weblogic and why we need the package exclusions , I don't want to repeat the same story here , and this guy has written a great blog on it already