1

I am new to Spring MVC though not new to Java and working primary on Struts2 and Wicket as my choice for web development

I am trying to do a POC of file upload using spring MVC here is my jsp file

<form  id="fileuploadForm" action="fileupload" method="POST" enctype="multipart/form-data" >
            <fieldset>
                <legend>Upload Fields</legend>

               <input id="file" type="file" name="file" />
                <p><button type="submit">Upload</button></p> 

            </fieldset>
        </form>

and my Controller is

@Controller
@RequestMapping("FileUpload/fileupload")
public class FileUploadController{



    public ModelAndView processUpload(@RequestParam MultipartFile file, WebRequest webRequest, Model model) {


        String orgFileName = file.getOriginalFilename();
        String filePath = "data/input" + orgFileName;
        ModelMap modelMap = new ModelMap(); 
        System.out.println("*******************************************");
        File dest = new File(filePath);
        try {
            file.transferTo(dest);
        } catch (IllegalStateException e) {
            e.printStackTrace();
            modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
            return new ModelAndView("results", modelMap);
                //return "File uploaded failed:" + orgFileName;
        } catch (IOException e) {
            e.printStackTrace();
            modelMap.addAttribute("result", "File uploaded failed:" + orgFileName);
            return new ModelAndView("results", modelMap);
        }


        modelMap.addAttribute("result", "File uploaded " + orgFileName);
        return new ModelAndView("results", modelMap);



    }

below is the entry for dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

     <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:suffix=".jsp" />

    <bean name="/*" class="com.app.controller.FileUploadController"/>
</beans>

i tried google also but not able to get any help may be not able to find good resource due to lack of knowledge of spring MVC where ever i am hitting my uplaod button i am getting 404 error for this URL

http://localhost:7777/FileUpload/fileupload

i am sure i am doing some configuration mistake but not able to point it out, any help in this will be very helpful

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>SpringExample17</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
        org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>    
</web-app>

Thanks in advance

6
  • Can you please also post your web.xml file. Commented Sep 2, 2011 at 7:16
  • any exception? Is the file uploaded? Commented Sep 2, 2011 at 7:20
  • @Bozho : no there is no exceptio in the console.moment i hot upload button after slecting a file it give me back 404 error with no exception on my eclipse server console Commented Sep 2, 2011 at 7:21
  • but is the file uploaded to the desired location? Commented Sep 2, 2011 at 7:30
  • no not at all the controller is not being called at all.this is what tomcat telling me "description The requested resource (/FileUpload/fileupload) is not available." Commented Sep 2, 2011 at 7:32

2 Answers 2

3

There are several things to note:

  • You have mapped only URLs ending on .htm to your Spring Dispatcher, so the Controller can never see your request.
  • If FileUpload is the name of your Web-App, you need to remove it from the @RequestMapping annotation.
  • <bean name="/*" class="com.app.controller.FileUploadController"/> should be changed to <bean name="/fileupload.htm" class="com.app.controller.FileUploadController"/>

HTH

Sign up to request clarification or add additional context in comments.

Comments

1

I'm always starting my mappings with a forward slash, so try:

 @RequestMapping("/FileUpload/fileupload")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.