我是Spring MVC的新手,虽然不是Java的新手,并且在Struts2和Wicket上工作主要作为我的Web开发选择
我正在尝试使用spring MVC进行文件上传的POC,这里是我的jsp文件
<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>
我的控制器是
@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);
}
下面是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>
我也试过google但由于缺乏对spring MVC的了解而无法获得任何帮助可能无法找到好的资源 我在哪里击中我的上传按钮我得到这个URL的404错误
http://localhost:7777/FileUpload/fileupload
我确信我正在做一些配置错误但无法指出它,任何帮助都会非常有帮助
的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>
提前致谢
答案 0 :(得分:3)
有几点需要注意:
.htm
结尾的网址映射到您的Spring Dispatcher,因此Controller永远无法看到您的请求。FileUpload
是您的网络应用程序的名称,则需要将其从@RequestMapping
注释中删除。<bean name="/*" class="com.app.controller.FileUploadController"/>
应更改为<bean name="/fileupload.htm" class="com.app.controller.FileUploadController"/>
HTH
答案 1 :(得分:1)
我总是用正斜杠开始我的映射,所以试试:
@RequestMapping("/FileUpload/fileupload")