package com.jing.action;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import com.jing.model.User;
import com.jing.service.UserService;
@Controller
@RequestMapping("/user")
public class UserAction {
@Resource(name = "userService")
private UserService userService;
// 数据校验
@RequestMapping(value = "/add.do")
public String add(@Valid @ModelAttribute("user") User user, BindingResult result) {
System.out.println(user);
if (result.hasErrors()) {
return "user/error";
}
// userService.addUser(user);
return "user/success";
}
@RequestMapping("/formTag.do")
public String formTag(Map<String, Object> map) {
// 添加user
User user = new User();
user.setId("2222");
user.setName("丽丽");
user.setMale(true);
user.setBirthday(new Date());
user.setSex(1);
List<String> roles = new ArrayList<String>();
roles.add("role1");
roles.add("role2");
user.setRoles(roles);
map.put("user", user);
// 添加roleList列表
List<String> roleList = new ArrayList<String>();
roleList.add("role1");
roleList.add("role2");
roleList.add("role3");
map.put("roleList", roleList);
// 添加roleMap集合
Map<String, String> roleMap = new HashMap<String, String>();
roleMap.put("role1", "角色1");
roleMap.put("role2", "角色2");
roleMap.put("role3", "角色3");
map.put("roleMap", roleMap);
// 加入ballmap
Map<String, String> radioMap = new HashMap<String, String>();
radioMap.put("1", "篮球");
radioMap.put("2", "足球");
radioMap.put("3", "羽毛球");
map.put("radioMap", radioMap);
return "user/formTag";
}
/*
* 文件上传有2种方式:
* 1: 自己获得request, 解析
* 2: 配置bean, spring自动帮助我们解析
*/
// 文件上传: 解析request方式
@RequestMapping("/upload_1.do")
public String upload_1(HttpServletRequest request) throws Exception {
// 将请求转换为multipart请求
MultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
// 解析
Iterator<String> fileNames = multipartRequest.getFileNames();
multipartRequest.setCharacterEncoding("utf-8");
while (fileNames.hasNext()) {
MultipartFile multipartFile = multipartRequest.getFile(fileNames.next());
if (null != multipartFile) {
String fileName = request.getSession().getServletContext().getRealPath("/upload") + File.separator
+ multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(fileName));
}
}
}
return "/user/uploadSuccess";
}
// 文件上传: 自动注入方式
@RequestMapping("/upload_2.do")
public String upload_2(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) throws Exception {
if (null != multipartFile) {
String fileName = request.getSession().getServletContext().getRealPath("/upload") + File.separator + multipartFile.getOriginalFilename();
multipartFile.transferTo(new File(fileName));
}
return "/user/uploadSuccess";
}
}