SpringMVC_2

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3   <display-name>SpringMVC_2</display-name> 
 4     <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
 5     <servlet>
 6         <servlet-name>springDispatcherServlet</servlet-name>
 7         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 8         <init-param>
 9             <param-name>contextConfigLocation</param-name>
10             <param-value>classpath:springmvc.xml</param-value>
11         </init-param>
12         <load-on-startup>1</load-on-startup>
13     </servlet>
14 
15     <!-- Map all requests to the DispatcherServlet for handling -->
16     <servlet-mapping>
17         <servlet-name>springDispatcherServlet</servlet-name>
18         <url-pattern>/</url-pattern>
19     </servlet-mapping>
20     
21      <!-- 配置HiddenHttpMethodFilter: 把post 请求转为DELETE、PUT请求-->
22      <filter>
23          <filter-name>HiddenHttpMethodFilter</filter-name>
24          <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
25      </filter>
26      <filter-mapping>
27          <filter-name>HiddenHttpMethodFilter</filter-name>
28          <url-pattern>/*</url-pattern>
29      </filter-mapping>
30 </web-app>

 

 

 springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:mvc="http://www.springframework.org/schema/mvc"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
 8         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
 9 <context:component-scan base-package="org.springmvc"></context:component-scan>
10 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
11     <property name="prefix" value="/WEB-INF/views/"></property>
12     <property name="suffix" value=".jsp"></property>
13 </bean>
14     <mvc:default-servlet-handler/>
15     <mvc:annotation-driven></mvc:annotation-driven>
16     
17 <!-- 配置multipartResolver用于表单文件上传 -->
18 <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
19 <property name="defaultEncoding" value="UTF-8"></property>
20 <property name="maxUploadSize" value="1024000"></property>
21 </bean>    
22 </beans>

 Dao层:

 1 @Repository//注解于Dao层使得用于spring管理实现自动注入
 2 public class EmployeeDao {
 3 private static Map<Integer, Employee> employees;
 4 private static Integer initId =1006;
 5 @Autowired
 6 private  DepartmentDao departmentDao;
 7 static {
 8     employees = new HashMap<>();
 9     employees.put(1001, new Employee(1001, "employee_1001","1001@163.com", 1,new Department()));
10     employees.put(1002, new Employee(1002, "employee_1002","1001@163.com", 0,new Department()));
11     employees.put(1003, new Employee(1003, "employee_1003","1001@163.com", 1,new Department()));
12     employees.put(1004, new Employee(1004, "employee_1004","1001@163.com", 0,new Department()));
13     employees.put(1005, new Employee(1005, "employee_1005","1001@163.com", 1,new Department()));
14 }
15 public void save(Employee employee){
16     if (employee.getId()==null) {
17         employee.setId(initId++);
18     }
19     employee.setDepartment(departmentDao.getDepartment(employee.getDepartment().getId()));
20     employees.put(employee.getId(), employee);
21 }
22 public Collection<Employee> getAll(){
23     return employees.values();
24 }
25 public  Employee get(Integer id) {
26     return employees.get(id);
27 }
28 public void delete(Integer id) {    
29     employees.remove(id);
30 }
31 }
32  

 

 1 package org.springmvc.curd.entity;
 2 
 3 public class Department {
 4 private int id;
 5 private String departmentName;
 6 public int getId() {
 7     return id;
 8 }
 9 public void setId(int id) {
10     this.id = id;
11 }
12 public String getDepartmentName() {
13     return departmentName;
14 }
15 public void setDepartmentName(String departmentName) {
16     this.departmentName = departmentName;
17 }
18 @Override
19 public String toString() {
20     return "Department [id=" + id + ", departmentName=" + departmentName + "]";
21 }
22 public Department() {
23 }
24 public Department(int id, String departmentName) {
25     super();
26     this.id = id;
27     this.departmentName = departmentName;
28 }
29 
30 }

Controller层:

 1 @Controller
 2 public class EmployeeHandler {
 3 @Autowired
 4 private EmployeeDao employeeDao;
 5 @Autowired
 6 private DepartmentDao departmentDao;
 7     
 8 @RequestMapping("emps")
 9 public String list(Map<String,Object>map) {
10     map.put("employees",employeeDao.getAll());
11 return "list";    
12 }
13 @RequestMapping(value="addEmployee",method=RequestMethod.GET)
14 public String input(Map<String,Object> map) {
15     map.put("departments",departmentDao.getDepartments());
16     map.put("employee", new Employee());
17     return "input";
18 }
19 //@PathVariable可用于获取随url发送过来的变量 匹配到参数
20 @RequestMapping(value="save", method = RequestMethod.POST)
21 public String save(Employee employee) {
22     employeeDao.save(employee);
23     return "redirect:emps";
24 }
25 @RequestMapping(value="/emp/{id}",method=RequestMethod.DELETE)
26 public String delete(@PathVariable("id")Integer id) {
27     employeeDao.delete(id);
28 return "redirect:emps";    
29 }
30 
31 @RequestMapping("/testFileUpload")//@PathVariable可用于获取随url发送过来的变量 匹配到参数
32 public String testFileUpload(@RequestParam("desc") String desc, @RequestParam("file") MultipartFile file) throws IOException {
33     System.out.println("desc"+desc);
34     System.out.println("OriginalFileName:"+file.getName());
35     System.out.println("InputStream:"+file.getInputStream());    
36     return "success";
37 }
38 
39 
40 
41 }

 

posted @ 2018-03-10 23:22  千彧  阅读(203)  评论(0编辑  收藏  举报