SpringMVC_2
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC_2</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<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> <!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <!-- 配置HiddenHttpMethodFilter: 把post 请求转为DELETE、PUT请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:component-scan base-package="org.springmvc"></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>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven> <!-- 配置multipartResolver用于表单文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="UTF-8"></property>
<property name="maxUploadSize" value="1024000"></property>
</bean>
</beans>
Dao层:
@Repository//注解于Dao层使得用于spring管理实现自动注入
public class EmployeeDao {
private static Map<Integer, Employee> employees;
private static Integer initId =1006;
@Autowired
private DepartmentDao departmentDao;
static {
employees = new HashMap<>();
employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));
employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));
employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));
employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));
employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));
}
public void save(Employee employee){
if (employee.getId()==null) {
employee.setId(initId++);
}
employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
employees.put(employee.getId(), employee);
}
public Collection<Employee> getAll(){
return employees.values();
}
public Employee get(Integer id) {
return employees.get(id);
}
public void delete(Integer id) {
employees.remove(id);
}
}
package org.springmvc.curd.entity; public class Department {
private int id;
private String departmentName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
@Override
public String toString() {
return "Department [id=" + id + ", departmentName=" + departmentName + "]";
}
public Department() {
}
public Department(int id, String departmentName) {
super();
this.id = id;
this.departmentName = departmentName;
} }
Controller层:
@Controller
public class EmployeeHandler {
@Autowired
private EmployeeDao employeeDao;
@Autowired
private DepartmentDao departmentDao; @RequestMapping("emps")
public String list(Map<String,Object>map) {
map.put("employees",employeeDao.getAll());
return "list";
}
@RequestMapping(value="addEmployee",method=RequestMethod.GET)
public String input(Map<String,Object> map) {
map.put("departments",departmentDao.getDepartments());
map.put("employee", new Employee());
return "input";
}
//@PathVariable可用于获取随url发送过来的变量 匹配到参数
@RequestMapping(value="save", method = RequestMethod.POST)
public String save(Employee employee) {
employeeDao.save(employee);
return "redirect:emps";
}
@RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
public String delete(@PathVariable("id")Integer id) {
employeeDao.delete(id);
return "redirect:emps";
} @RequestMapping("/testFileUpload")//@PathVariable可用于获取随url发送过来的变量 匹配到参数
public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException {
System.out.println("desc"+desc);
System.out.println("OriginalFileName:"+file.getName());
System.out.println("InputStream:"+file.getInputStream());
return "success";
} }
SpringMVC_2的更多相关文章
- Mybaits+SpringMVC项目(含代码生成工具源码)
大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果 工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...
- Spring MVC 基础笔记
spring mvc功能: 以Controller为中心完成对系统流程的控制管理 从请求中搜集数据 对传入的参数进行验证 将结果返回给视图 针对不同的视图提供不同的解决方案 针对jsp视图技术提供标签 ...
随机推荐
- linux如何正确设置静态ip
如果是新安装的CentOS7的用户,刚开始应该是没网的,ifconfig命令在CentOS7已经被淘汰了. 1.使用ip addr 即查看分配网卡情况. 2.激活网卡 [root@localhost ...
- phpstorm破解激活码
一.将“0.0.0.0 account.jetbrains.com”添加到hosts文件中 二.浏览器打开 http://idea.lanyus.com,点击页面中的“获得注册码”,然后在注册时切换至 ...
- c++基础_特殊回文数
#include <iostream> using namespace std; int main(){ int n; cin>>n; ;i<;i++){ int tem ...
- Django中配置自定义日志系统
将
- Jquery validate自定义验证
http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...
- ZOJ 3905 Cake
Cake Time Limit: 4 Seconds Memory Limit: 65536 KB Alice and Bob like eating cake very much. One ...
- BNUOJ 1268 PIGS
PIGS Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 11496 ...
- xtu read problem training 4 B - Multiplication Puzzle
Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...
- 博客搬迁至Gitcafe
原先的Github pages貌似在国内被墙了,导致搜索引擎一直没有索引到,今天一怒之下迁到Gitcafe 虽然之前的模板用不成,害我重新找了一套,改了好半天,不过总算弄完了
- hdu 4435 bfs+贪心
/* 题意:给定每个点在平面内的坐标,要求选出一些点,在这些点建立加油站,使得总花费最少(1号点必须建立加油站).在i点建立加油站需要花费2^i. 建立加油站要求能使得汽车从1点开始走遍全图所有的点并 ...