package cn.bainaonline.csm.cswarn.controller;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
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 org.springframework.web.servlet.ModelAndView;
import cn.bainaonline.csm.cswarn.model.User;
import cn.bainaonline.csm.cswarn.model.User1;
import cn.bainaonline.csm.cswarn.service.UserService;
import cn.bainaonline.csm.cswarn.util.Pages;
@Controller
@RequestMapping("/user/*")
public class UserController {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
private final String LIST="redirect:/user/list/1";
@RequestMapping(value="/add", method=RequestMethod.POST)
public String addUser(User1 user, @RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException{
//如果只是上传一个文件,则只需要MultipartFile类型接收文件即可,而且无需显式指定@RequestParam注解
//如果想上传多个文件,那么这里就要用MultipartFile[]类型来接收文件,并且还要指定@RequestParam注解
//并且上传多个文件时,前台表单中的所有<input type="file"/>的name都应该是myfiles,否则参数里的myfiles无法获取到所有上传的文件
for(MultipartFile myfile : myfiles){
if(myfile.isEmpty()){
System.out.println("文件未上传");
}else{
System.out.println("文件长度: " + myfile.getSize());
System.out.println("文件类型: " + myfile.getContentType());
System.out.println("文件名称: " + myfile.getName());
System.out.println("文件原名: " + myfile.getOriginalFilename());
System.out.println("========================================");
//如果用的是Tomcat服务器,则文件会上传到\\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\文件夹中
String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
//这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
// FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
}
}
// users.put(user.getUsername(), user);
return "redirect:/";
}
/**
* 登录
* @param user
* @param session
* @return
*/
@RequestMapping("/login")
public ModelAndView login(){
System.out.println(""+userService.getalleuser().get(0).getUser_name());
return new ModelAndView("/");
}
/**
* 跳转至添加页
* @return
*/
@RequestMapping(value="/new",method=RequestMethod.GET)
public ModelAndView toAdd(){
return new ModelAndView("user/add");
}
/**
* 保存
* @param user
* @return
*/
@RequestMapping(value="/new",method=RequestMethod.POST)
public ModelAndView add(@Valid User user,BindingResult result){
if(result.hasErrors()){
return new ModelAndView("user/add","error", result.getAllErrors());
}else{
userService.addUser(user);
return new ModelAndView(LIST);
}
}
/**
* 根据ID删除
* @param id
* @return
*/
@RequestMapping(value="/del/{id}")
public ModelAndView delete(@PathVariable int id){
userService.deleteUser(id);
return new ModelAndView(LIST);
}
/**
* 跳转至编辑页面
* @param id
* @return
*/
@RequestMapping(value="/edit/{id}")
public ModelAndView edit(@PathVariable int id){
User user=userService.getUserById(id);
return new ModelAndView("user/edit","user",user);
}
/**
* 编辑
* @param user
* @return
*/
@RequestMapping(value="/edit")
public ModelAndView update(@Valid User user,BindingResult result){
ModelAndView view=new ModelAndView();
if(result.hasErrors()){
view.addObject("error", result.getAllErrors());
view.setViewName("user/edit");
return view;
}else{
userService.updateUser(user);
return new ModelAndView(LIST);
}
}
/**
* 分页
* @param pageNo
* @return
*/
@RequestMapping(value="/list/{pageNo}")
public ModelAndView list(@PathVariable int pageNo){
Pages<User> pages=userService.getUserByPageNo(pageNo);
return new ModelAndView("user/main","pages",pages);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
springMVC+ibatis整合实例 (115个子文件)
all-wcprops 216B
UserController.class 5KB
UserDaoImpl.class 3KB
CommonInterceptor.class 2KB
Pages.class 2KB
UserServiceImpl.class 2KB
User.class 2KB
User1.class 1KB
UserService.class 780B
UserDao.class 731B
BaseDao.class 716B
.classpath 5KB
org.eclipse.wst.jsdt.ui.superType.container 49B
entries 382B
format 2B
hibernate3.jar 3.74MB
classes12.jar 1.52MB
aspectjweaver.jar 1.47MB
jsf-impl.jar 1.15MB
freemarker-2.3.15.jar 859KB
jaxb-impl-2.1.12.jar 847KB
mysql-connector-java-5.1.12-bin.jar 716KB
org.springframework.context-3.0.4.RELEASE.jar 650KB
google-collections-1.0.jar 625KB
c3p0-0.9.1.jar 594KB
commons-collections-3.2.1.jar 562KB
org.springframework.beans-3.0.4.RELEASE.jar 543KB
javassist-3.4.GA.jar 460KB
antlr-2.7.6.jar 433KB
xstream-1.3.1.jar 421KB
org.springframework.web.servlet-3.0.4.RELEASE.jar 407KB
jstl-1.2.jar 405KB
spring-webmvc.jar 393KB
org.springframework.web-3.0.4.RELEASE.jar 385KB
ibatis-2.3.4.726.jar 375KB
org.springframework.jdbc-3.0.4.RELEASE.jar 371KB
log4j-1.2.14.jar 359KB
org.springframework.core-3.0.4.RELEASE.jar 359KB
jackson-mapper-lgpl-1.2.1.jar 344KB
org.springframework.orm-3.0.4.RELEASE.jar 326KB
cglib-nodep-2.1_3.jar 317KB
jsf-api.jar 314KB
backport-util-concurrent.jar 314KB
org.springframework.aop-3.0.4.RELEASE.jar 313KB
dom4j-1.6.1.jar 307KB
hibernate-validator-4.1.0.Final.jar 281KB
ehcache-1.5.0.jar 258KB
commons-lang-2.4.jar 256KB
commons-beanutils-1.8.3.jar 227KB
org.springframework.transaction-3.0.4.RELEASE.jar 226KB
org.springframework.expression-3.0.4.RELEASE.jar 163KB
json-lib-2.3-jdk15.jar 148KB
jackson-core-lgpl-1.2.1.jar 142KB
commons-dbcp.jar 119KB
commons-io-1.4.jar 106KB
jaxb-api-2.2.jar 102KB
hibernate-jpa-2.0-api-1.0.0.Final.jar 99KB
ezmorph-1.0.6.jar 84KB
commons-pool.jar 61KB
commons-logging-1.1.1.jar 59KB
org.springframework.oxm-3.0.4.RELEASE.jar 59KB
commons-codec-1.4.jar 57KB
commons-fileupload-1.2.1.jar 56KB
org.springframework.asm-3.0.4.RELEASE.jar 52KB
commons-fileupload.jar 52KB
validation-api-1.0.0.GA.jar 46KB
org.springframework.aspects-3.0.4.RELEASE.jar 29KB
slf4j-api-1.6.1.jar 25KB
xstream-benchmark-1.3.1.jar 25KB
json_simple-1.1.jar 16KB
slf4j-nop-1.6.1.jar 5KB
aopalliance.jar 4KB
UserController.java 5KB
CommonInterceptor.java 2KB
UserDao.java 2KB
UserDaoImpl.java 2KB
Pages.java 2KB
UserServiceImpl.java 2KB
User.java 1KB
User1.java 1KB
UserService.java 892B
BaseDao.java 570B
jquery.js 71KB
.jsdtscope 500B
main.jsp 3KB
add.jsp 2KB
edit.jsp 1021B
fileerror.jsp 902B
index.jsp 834B
index.jsp 725B
list.jsp 641B
add.jsp 635B
404.jsp 628B
base.jsp 320B
MANIFEST.MF 36B
.mymetadata 316B
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.j2eedt.core.prefs 1KB
org.eclipse.jdt.core.prefs 330B
.project 1KB
共 115 条
- 1
- 2
独恋YOU
- 粉丝: 0
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页