Jsp MultipartFile在spring mvc中不起作用
当我使用@RequestParam(“filename”)多部分文件时,以下代码不会打印任何内容 但当我使用@RequestParam(“Name”)字符串名称时,它会打印输出 这是我的控制器Jsp MultipartFile在spring mvc中不起作用,jsp,spring-mvc,Jsp,Spring Mvc,当我使用@RequestParam(“filename”)多部分文件时,以下代码不会打印任何内容 但当我使用@RequestParam(“Name”)字符串名称时,它会打印输出 这是我的控制器 @Controller public class DocumentController { @RequestMapping(value="/document", method=RequestMethod.POST) // second parameter doesn't work pub
@Controller
public class DocumentController {
@RequestMapping(value="/document", method=RequestMethod.POST)
// second parameter doesn't work
public @ResponseBody String saveDocument(@ModelAttribute("document")
DocumentBean formdata,
@RequestParam("filename") MultipartFile file)
{
System.out.println("hello");
System.out.println(formdata.getDescription());
// below code doesn't work
System.out.println(file.getOriginalFilename());
return null;
}
}
下面是jsp代码
<body>
<form action="#" id="documentForm" name="document" method="post"
enctype="multipart/form-data">
<div>Name <input type="Text" placeholder="enter name of file"
name="Name"></div>
<div>Description <input type="Text" placeholder="description"
name="description"></div>
<div>Document <input type="file" name="filename" id="file"></div>
</form>
<button onclick="SubmitForm()">submit</button>
</body>
名称
描述
文件
提交
下面是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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID"
version="3.0">
<display-name>DocumentProject</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>ContextConfigLocation</param-name>
<param-value>/WEB-INF/applicaitonContext</param-value>
</context-param>
</web-app>
文档项目
index.jsp
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/
org.springframework.web.context.ContextLoaderListener
上下文配置位置
/WEB-INF/ApplicationContext
下面是dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<mvc:resources mapping="/js/**" location="/js/" />
<context:component-scan base-package="com.master.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
</bean>
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- setting maximum upload size -->
<property name="maxUploadSize" value="100000000" />
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
信息
下面是applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="annotatedClasses">
<list>
// pojo list
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
</props>
</property>
</bean>
//pojo列表
org.hibernate.dialogue.mysqldialogue
真的
线
下面是SubmitForm函数
<script>
function SubmitForm(){
var formdata=$("#documentForm").serialize();
$.ajax({
type:"Post",
url:"document",
data:formdata,
success:function(res){
},
error:function(){
}
});
}
</script>
函数SubmitForm(){
var formdata=$(“#documentForm”).serialize();
$.ajax({
类型:“Post”,
网址:“文件”,
数据:formdata,
成功:功能(res){
},
错误:函数(){
}
});
}
您可以试试这个片段
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page isELIgnored="false" %>
<form:form method="POST" commandName="fileUploadCmd" enctype="multipart/form-data">
Select File: <input type="file" name="uploadFile"/> <br>
<input type="submit" value="upload"/>
</form:form>
选择文件:
您可以试试这个片段
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@page isELIgnored="false" %>
<form:form method="POST" commandName="fileUploadCmd" enctype="multipart/form-data">
Select File: <input type="file" name="uploadFile"/> <br>
<input type="submit" value="upload"/>
</form:form>
选择文件:
你应该为SubmitForm添加JS代码
你能发布你的spring配置文件吗?@RC,我已经添加了JS代码。除了multipart@AkshayKhopkar,检查配置文件?!?不,您没有,您在某个地方使用了一个名为“SubmitForm”的javascript函数:
,应该发布此函数的代码,因为这可能是问题所在。您应该为SubmitForm
添加JS代码。您可以发布spring配置文件吗?@RC,我已经添加了JS代码。除了multipart@AkshayKhopkar,检查配置文件?!?不,您没有,您在某个地方使用了一个名为“SubmitForm”的javascript函数:
,应该发布此函数的代码,因为它可能是问题所在。请在答案中添加有关您的解决方案的更多详细信息。请在答案中添加有关您的解决方案的更多详细信息。