package com.demo.springbootweb.controller;
import java.util.Collection;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.demo.springbootweb.dao.DepartmentDao;
import com.demo.springbootweb.dao.EmployeeDao;
import com.demo.springbootweb.entity.Department;
import com.demo.springbootweb.entity.Employee;
import com.demo.springbootweb.exception.UserNotExistException;
@Controller
@RequestMapping(value="/user")
public class UserController {
@Autowired
EmployeeDao employeeDao;
@Autowired
DepartmentDao departmentDao;
// 在Rest风格中可以这么写
//@GetMapping
//@DeleteMapping
//@PutMapping
//@PostMapping(value="/user/login")
@RequestMapping(value="login", method = RequestMethod.POST)
public String login(String username, String password, Map<String, Object> map, HttpSession session){
if("admin".equals(username) && "123".equals(password)){
//return "dashboard";
session.setAttribute("loginUser", username);
/**
* 登录成功,防止表单重复提交,可以重定向某个路径信息,
* 这个路径是不存在的,通过路径映射现次设置相关的页面路径
*/
return "redirect:/main";
}
map.put("msg", "登录失败");
return "index";
}
/**
* 员工列表页面
* @param map
* @return
*/
@RequestMapping(value="emps", method = RequestMethod.GET)
public String list(Map<String, Object> map){
Collection<Employee> all = employeeDao.getAll();
map.put("emps", all);
return "emp/list";
}
/**
* 员工添加页面
* @param map
* @return
*/
@RequestMapping(value="emp", method = RequestMethod.GET)
public String add(Map<String, Object> map){
Collection<Department> departments = departmentDao.getDepartments();
map.put("deps", departments);
return "emp/add";
}
/**
* 提交添加员工
* SpringMVC自动将请求参数和入参对象的属性进行一一绑定
* @param employee
* @return
*/
@RequestMapping(value="emp", method = RequestMethod.POST)
//@PostMapping(value="emp")
public String save(Employee employee){
employeeDao.save(employee);
// 请求员工列表页面,"/"代表当前项目路径
return "redirect:/user/emps"; // 第一种方式:通过重定向一个地址,请求emps
//return "forward:/user/emps";// 第二种方式:通过转发一个地址
}
/**
* 保存修改员工
* @param employee
* @return
*/
//@PutMapping(value="emp")
@RequestMapping(value="uemp", method = {RequestMethod.PUT, RequestMethod.POST})
public String updateEmp(Employee employee){
System.out.println("---------------------- employee= " + employee);
employeeDao.save(employee);
return "redirect:/user/emps";
}
/**
* 删除员工信息
* @param id
* @return
*/
//@PostMapping(value="delemp/{id}")
//@DeleteMapping(value="emp/{id}")
@RequestMapping(value="emp/{id}", method = {RequestMethod.POST, RequestMethod.DELETE})
public String delEmp(@PathVariable("id") Integer id){
employeeDao.delete(id);
return "redirect:/user/emps";
}
/**
* 修改员工页面
* @param id 员工ID
* @return
*/
@RequestMapping(value="emp/{id}", method = RequestMethod.GET)
public String emp(@PathVariable("id") Integer id, Map<String, Object> map){
Employee employee = employeeDao.get(id);
Collection<Department> departments = departmentDao.getDepartments();
map.put("deps", departments);
map.put("emp", employee);
return "emp/add";
}
@ResponseBody
@RequestMapping(value="u", method = RequestMethod.GET)
public String getEmp(String name){
if("u".equals(name)){
throw new UserNotExistException();
}
return "getEmp";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
crud增删改查-定制异常信息
共83个文件
html:18个
java:14个
class:14个
需积分: 10 10 下载量 79 浏览量
2018-05-23
20:44:04
上传
评论
收藏 372KB ZIP 举报
温馨提示
SpringBoot web--错误处理、定制错误页面和错误数据,crud 增删改查
资源推荐
资源详情
资源评论
收起资源包目录
springbootwebcrud.zip (83个子文件)
springbootwebcrud
.project 777B
mvnw.cmd 5KB
mvnw 6KB
src
test
java
com
demo
springbootweb
SpringbootwebcrudApplicationTests.java 350B
main
resources
i18n
index_en_US.properties 121B
index.properties 151B
index_zh_CN.properties 151B
application.properties 605B
com
demo
springbootweb
templates
index.html 2KB
emp
list.html 4KB
add.html 5KB
commons
nav.html 8KB
error
4xx.html 3KB
5xx.html 3KB
404.html 3KB
dashboard.html 5KB
success.html 336B
static
asserts
img
bootstrap-solid.svg 1KB
js
Chart.min.js 154KB
feather.min.js 70KB
jquery-3.2.1.slim.min.js 68KB
popper.min.js 19KB
bootstrap.min.js 48KB
css
bootstrap.min.css 141KB
dashboard.css 2KB
signin.css 906B
java
com
demo
springbootweb
component
MyLocaleResolver.java 999B
LoginHandlerInterceptor.java 2KB
MyErrorAttributes.java 959B
controller
UserController.java 4KB
MyExceptionHandler.java 2KB
HelloController.java 889B
exception
UserNotExistException.java 368B
dao
DepartmentDao.java 853B
EmployeeDao.java 2KB
config
MyMvcConfig.java 3KB
entity
Department.java 813B
Employee.java 2KB
SpringbootwebcrudApplication.java 536B
target
classes
i18n
index_en_US.properties 121B
index.properties 151B
index_zh_CN.properties 151B
application.properties 605B
com
demo
springbootweb
component
MyErrorAttributes.class 1KB
LoginHandlerInterceptor.class 2KB
MyLocaleResolver.class 1KB
SpringbootwebcrudApplication.class 754B
controller
UserController.class 5KB
MyExceptionHandler.class 2KB
HelloController.class 1KB
exception
UserNotExistException.class 481B
dao
EmployeeDao.class 3KB
DepartmentDao.class 2KB
config
MyMvcConfig.class 3KB
entity
Employee.class 2KB
Department.class 1KB
templates
index.html 2KB
emp
list.html 4KB
add.html 5KB
commons
nav.html 8KB
error
4xx.html 3KB
5xx.html 3KB
404.html 3KB
dashboard.html 5KB
success.html 336B
static
asserts
img
bootstrap-solid.svg 1KB
js
Chart.min.js 154KB
feather.min.js 70KB
jquery-3.2.1.slim.min.js 68KB
popper.min.js 19KB
bootstrap.min.js 48KB
css
bootstrap.min.css 141KB
dashboard.css 2KB
signin.css 906B
test-classes
com
demo
springbootweb
SpringbootwebcrudApplicationTests.class 673B
.settings
org.eclipse.m2e.core.prefs 97B
org.eclipse.jdt.core.prefs 243B
org.eclipse.core.resources.prefs 459B
.mvn
wrapper
maven-wrapper.jar 46KB
maven-wrapper.properties 110B
pom.xml 2KB
.classpath 1KB
.gitignore 268B
共 83 条
- 1
资源评论
yufang131
- 粉丝: 35
- 资源: 62
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功