SpringMVC转发与重定向
在一个请求处理方法Action中如果返回结果为“index”字符则表示转发到视图index,有时候我们需要重定向,则可以在返回的结果前加上一个前缀“redirect:”,可以重定向到一个指定的页面,也可以是另一个action,示例代码如下:
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@RequestMapping("re")
public class ReController {
//转发
@RequestMapping("test1")
public String test1(Model model){
System.out.println("hei");
return "hi";
}
//转发
@RequestMapping("test2")
public String test2(Model model){
model.addAttribute("hai","hai");
return "forward:test1";
}
//重定向1
@RequestMapping("test3")
public String test3(Model model){
model.addAttribute("hai3","hai3");
return "redirect:test1";
}
//重定向2
@RequestMapping("test4")
public String test4(Model model, RedirectAttributes ra){
ra.addFlashAttribute("hai33","hai33");
return "redirect:test1";
}
}
在上述例子中,转发存在两种形式,如果转发方法则需要forward,重定向第一种不可带数据进行传递,重定向第二种则是使用Flash属性,是数据通过重定向实现传递
文件上传
配置
因为需要借助第三方上传组件commons-fileupload与commons-io,所以要修改pom.xml文件添加依赖,依赖的内容如下:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
springmvc-servlet.xml配置文件,增加如下配置内容:
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8" />
<property name="maxUploadSize" value="10485760000" />
<property name="maxInMemorySize" value="40960" />
</bean>
增加了一个类型为CommonsMultipartResolver类型的解析器,各属性的意义:
defaultEncoding:默认编码格式
maxUploadSize:上传文件最大限制(字节byte)
maxInMemorySize:缓冲区大小
方法
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
@Controller
@RequestMapping("up")
public class UpFileController {
@RequestMapping(value="/fileSave")
public String fileSave(Model model, @RequestParam MultipartFile[] files, HttpServletRequest request) throws Exception{
String username=request.getParameter("username");
System.out.println(username);
//文件存放的位置
String path=request.getServletContext().getRealPath("/files");
String fileName=null;
System.out.println(path);
System.out.println(files.length);
for (MultipartFile file : files) {
fileName=file.getOriginalFilename();
System.out.println(fileName);
File tempFile=new File(path, fileName);
System.out.println(tempFile.exists());
if(!tempFile.exists()) {
tempFile.mkdirs();
}
file.transferTo(tempFile);
}
System.out.println(path);
model.addAttribute("path",path);
model.addAttribute("fileName",fileName);
return "upload";
}
}