Objective
When learning Springmvc encountered a problem, the background has been receiving the front desk passed over the parameters, delayed a good long time finally found the reason, write a blog record under this pit, Boingonium hindering--__--
Code Listing
? Using the Springmvc to accept the parameters passed by the foreground is very simple, as long as the parameter name and the name of the foreground form in the same can be, I made a file upload example, so look at my front page
<body> <!-- enctype="multipart/form-data"在文件上传时加入,编码类型,其值默认是application/x-www-form-urlencoded --> <form action="testFileUpload" method="post" enctype="multipart/form-data"> File: <input type="file" name="file" /> Desc: <input type="text" name="desc" /> <input type="submit" value="Submit" /> </form> <br><br> <a href="emps">List All Employees</a></body>
? The following is the SPRINGMVC controller
@Controllerpublic class springMVCTest { @RequestMapping("/testFileUpload") public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException { System.out.println("desc: " + desc); System.out.println("originalFilename: " + file.getOriginalFilename()); System.out.println("inputStream: " + file.getInputStream()); return "success"; }}
? then the Web. xml File
<!-- 配置DispatcherServlet --> <!-- SpringMvc会根据servlet-name配置,找到/WEB-INF/dispatcher-servlet.xml作为配置文件载入Web工程中 --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
? and then the SPRINGMVC configuration file.
<!-- 配置自动扫描的包 --> <context:component-scan base-package="com.zgz.springmvc.crud"></context:component-scan> <context:component-scan base-package="com.zgz.springmvc.test"></context:component-scan> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean>
? After the pit came, because it is file upload, so you need to add Multipartresolver in the Spring MVC configuration file, add to add chant, so I added the following section of code:
<!-- 配置 MultipartResolver --> <bean id="commonsMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="1024000"></property> </bean>
? And then the pit appeared, and struggled to find that the ID was written wrong,id= "Multipartresolver", modify the code to:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"></property> <property name="maxUploadSize" value="1024000"></property> </bean>
Written in the last
With a word I like
? I think someday you will become an old man like me and tell a young man how you bring life to your lemon-like sweetness, like a lemon soda .
SPRINGMVC parameter reason analysis for receiving no front end delivery