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的更多相关文章

  1. Mybaits+SpringMVC项目(含代码生成工具源码)

       大家下载下来修改数据库配置应该就能运行起来,里面有一个SM的简单案例了,还有说明文件. 运行效果    工具类可以生成Springmvc+mybatis的相关类和配置文件,并具有增删查改的功能, ...

  2. Spring MVC 基础笔记

    spring mvc功能: 以Controller为中心完成对系统流程的控制管理 从请求中搜集数据 对传入的参数进行验证 将结果返回给视图 针对不同的视图提供不同的解决方案 针对jsp视图技术提供标签 ...

随机推荐

  1. 零基础入门学习Python(14)--字符串:各种奇葩的内置方法

    前言 这节课我们回过头来,再谈一下字符串,或许我们现在再来谈字符串,有些朋友可能觉得没必要了,甚至有些朋友就会觉得,不就是字符串吗,哥闭着眼也能写出来,那其实关于字符串还有很多你不知道的秘密哦.由于字 ...

  2. 201621123079《Java程序设计》第1周学习总结

    第1周-Java基本概念 1.本周学习总结 第一次上课接触java,了解了java的由来和历史,还有JCP,JSP的概念,并学会如何建立一个java文件和运行过程.感觉java比之前学习的数据结构更高 ...

  3. 分布式集群环境下运行Wordcount程序

    1.分布式环境的Hadoop提交作业方式与本地安装的Hadoop作业提交方式相似,但有两点不同: 1)作业输入输出都存储在HDFS 2)本地Hadoop提交作业时将作业放在本地JVM执行,而分布式集群 ...

  4. 小DEMO之manifest初体验

    前言 补漏洞系列~今天来动手体验一下HTML5中的离线应用之mainifest缓存清单.实际上H5还提供了一个JavaScript接口来用于更新缓存文件的方法以及对缓存文件的操作.在Chrome中,输 ...

  5. java使用ant.jar解压缩文件

    ant.jar下载地址http://ant.apache.org/bindownload.cgi 压缩文件代码: import org.apache.tools.ant.Project; import ...

  6. 测试第一个Oracle存储过程

    存储过程语句 //简单存储过程的例子 //每调用一次打印一次hello world create or replace procedure sayhelloworld as begin dbms_ou ...

  7. 集训第四周(高效算法设计)J题 (中途相遇法)

    Description   The SUM problem can be formulated as follows: given four lists A, B, C, D<tex2html_ ...

  8. JS逻辑运算符&&与||的妙用

    JS逻辑运算符&&与||的妙用   /* 文章写的不错 就此分享 */ &&中第一个表达式为假就不会去处理第二个表达式,直接放回结果. || 中就刚很好相反.如果第一个 ...

  9. 扫描局域网内所有主机和MAC地址的Shell脚本

    #!/bin/bash #author: InBi #date: 2011-08-16 #website: http://www.itwhy.org/2011/08-20/939.html ##### ...

  10. CodeForces 159E

    题目大意: 给定一堆带颜色和高度的魔方 用两种颜色的魔方,一种颜色接一种颜色向上拼接搭建成一个高塔,求高塔的最长高度,以及将拼接的过程中对应的编号顺序输出 多种情况成立输出任意一种即可 这里首先要对颜 ...