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视图技术提供标签 ...
随机推荐
- appendHTML方法ajax加载更多评论实例页面
//在后添加 <script>var appendHTML = function(el, html) { var divTemp = document.createElement(&quo ...
- 使用maven的mybatis-generator代码生成器插件生成实体类、mapper配置文件和mapper接口(使用idea)
接着之前创建的ssmMaven项目 一: 在pom文件中加入mybatis-generator插件 <plugins> <plugin> <groupId>org. ...
- Python 绑定方法与非绑定方法
用到的: import uuid -------------- uuid是128位的全局唯一标识符, 通常用32位的一个字符串的形式来表现 uuid.uuid1() ------------- ...
- Python 面向对象 组合-多态与多态性-封装-property
面向对象-组合 1.什么是组合 组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象 class Foo: xxx = 111 class Bar: yyy = 222 obj = Foo( ...
- [Python3网络爬虫开发实战] 2.3-爬虫的基本原理
我们可以把互联网比作一张大网,而爬虫(即网络爬虫)便是在网上爬行的蜘蛛.把网的节点比作一个个网页,爬虫爬到这就相当于访问了该页面,获取了其信息.可以把节点间的连线比作网页与网页之间的链接关系,这样蜘蛛 ...
- assert.deepStrictEqual()
assert.deepStrictEqual(actual, expected[, message]) 一般情况下等同于 assert.deepEqual(),但有两个例外.首先,原始值是使用全等运算 ...
- 得到JavaWeb项目在Tomcat中的运行路径
获得绝对路径 ··· File file= new File(path); System.out.println(file.getAbsolutePath()); ··· 获得Tomcat路径 这是一 ...
- jQuery学习之------对标签属性的操作
jQuery学习之------标签的属性 <a href=””>链接</a>此处的href就是该a标签带有的属性 在js中对标签的属性的操作方法有 1.1getAttribut ...
- JavaEE JDBC 了解JNDI
了解JNDI @author ixenos Web与企业应用中的连接管理 1. 数据库连接方式: (1)使用配置文件 (2)使用JNDI 2. 在Web或企业环境中部署 JDBC应用时,数据库连接管理 ...
- hihoCoder#1048 状态压缩·二
原题地址 位运算的状态压缩太操蛋了,很容易出错...又是数组没开够导致诡异现象(明明某个值是1,莫名其妙就变成0了),害我debug一整天!fuck 代码: #include <iostream ...