Spring文件上传配置和控制器

时间:2012-03-07 16:43:43

标签: spring-mvc file-upload

我正在尝试使用Netbeans tutorial学习Spring文件上传。

当JSP文件位于根目录中时,我可以正常工作但是当我移动到新的更深的子目录时,我无法访问浏览器中的success.jsp

也就是说,fileUpload.jsp的网址为localhost:8080/NewSpring/accounts/causa/fileUpload.html,但success.jsp的网址为localhost:8080/accounts/causa/fileUpload.html

那就是我失去了背景。我不明白为什么会这样,因为我的代码和教程的代码对我来说都是一样的。

这是我的调度员

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!-- Message sources -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages"/>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="index.html">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!-- ======================  The index controller ====================== -->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="home" />

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!--
The file upload
-->
<bean id="multipartResolver"
      class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>

<bean name="/accounts/causa/fileUpload.html" class="controller.accounts.FileUploadController">
    <property name="commandName" value="fileUpload"/>
    <property name="commandClass" value="model.FileUploadBean"/>
    <property name="formView" value="/accounts/causa/fileUpload" />
    <property name="successView" value="redirect:/accounts/causa/success.html" />
</bean>

<bean name="/accounts/causa/success.html"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="success" />  

我的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.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>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>redirect.jsp</welcome-file>
</welcome-file-list>

我的appplicationContext.xml

<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

<!--
Most controllers will use the ControllerClassNameHandlerMapping above, but
for the index controller we are using ParameterizableViewController, so we must
define an explicit mapping for it.
-->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/index.html">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

最后我的控制器

package controller.accounts;

import java.io.File;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.FileUploadBean;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class FileUploadController extends SimpleFormController {

    private static final String destinationDir = "/tmp/";

@Override
protected ModelAndView onSubmit(HttpServletRequest req,
        HttpServletResponse res,
        Object command,
        BindException errors) throws Exception {
    res.setContentType("text/csv");
    if (!(req instanceof MultipartHttpServletRequest)) {
        res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Expected multipart request");
        return null;
    }
    FileUploadBean bean = (FileUploadBean) command;
    MultipartFile file = bean.getFile();

    if (file == null) {
        return new ModelAndView("failure");
    } else {
        File destination = new File(destinationDir + file.getOriginalFilename());
        file.transferTo(destination);
        return new ModelAndView(getSuccessView(), "destination", destination.getAbsolutePath());
        }

    }
}   

0 个答案:

没有答案