SpringMVC-映射请求

SpringMVC-映射请求

一、概念:
Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求
在控制器的类定义及方法定义处都可标注
类定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录
方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于WEB 应用的根目录
DispatcherServlet 截获请求后,就通过控制器上@RequestMapping 提供的映射信息确定请求所对应的处理方法

二、在咋们上次学SpringMVC的环境(优化版)下开发的。这里没有基本的环境。

三、来实现我们平时开发的一个查询员工所有信息的一个操作,上代码

注意:我是面向接口编程,所以我的每一个Dao跟Service都有一个接口,它们的实现类都在DaoImpl、serviceImpl里,这里就不把接口展示出来了,你可以根据实现类,自己写。还有这里使用了注解开发,也就是我们在Spring里面使用的自动扫描技术,将Spring和SpringMVC结合在一起。
实体类;entity中有emp、dept
emp:

package com.RequestMapping.entity;

import java.util.Date;

public class Emp {

		/**雇员编号*/
		private int empno;
		/**雇员姓名*/
		private String ename;
		/**职位*/
		private String job;
		/**上级经理*/
		private int mgr;
		/**入职日期*/
		private Date hiredate;
		/**薪水*/
		private double salary;
		/**奖金*/
		private double comm;
		/**部门*/
		private Dept dept;

		/**构造函数*/
		public Emp() {
			super();
		}
		public Emp(int empno, String ename, String job, int mgr, Date hiredate, double salary, double comm, Dept dept) {
			super();
			this.empno = empno;
			this.ename = ename;
			this.job = job;
			this.mgr = mgr;
			this.hiredate = hiredate;
			this.salary = salary;
			this.comm = comm;
			this.dept = dept;
		}
		/**访问器*/
		public int getEmpno() {
			return empno;
		}
		public void setEmpno(int empno) {
			this.empno = empno;
		}
		public String getEname() {
			return ename;
		}
		public void setEname(String ename) {
			this.ename = ename;
		}
		public String getJob() {
			return job;
		}
		public void setJob(String job) {
			this.job = job;
		}
		public int getMgr() {
			return mgr;
		}
		public void setMgr(int mgr) {
			this.mgr = mgr;
		}
		public Date getHiredate() {
			return hiredate;
		}
		public void setHiredate(Date hiredate) {
			this.hiredate = hiredate;
		}
		public double getSalary() {
			return salary;
		}
		public void setSalary(double salary) {
			this.salary = salary;
		}
		public double getComm() {
			return comm;
		}
		public void setComm(double comm) {
			this.comm = comm;
		}
		public Dept getDept() {
			return dept;
		}
		public void setDept(Dept dept) {
			this.dept = dept;
		}
		
		@Override
		public String toString() {
			return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate
					+ ", salary=" + salary + ", comm=" + comm + ", dept=" + dept + "]";
		}
	}


dept:

package com.RequestMapping.entity;
/**
 * 映射Dept表的实体类
 */
public class Dept {

	/**部门编号*/
	private int deptno;
	/**部门名称*/
	private String dname;
	/**部门所在地址*/
	private String location;
	
	/**构造函数*/
	public Dept() {
		super();
	}
	public Dept(int deptno, String dname, String location) {
		super();
		this.deptno = deptno;
		this.dname = dname;
		this.location = location;
	}
	/**访问器*/
	public int getDeptno() {
		return deptno;
	}
	public void setDeptno(int deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + ", location=" + location + "]";
	}
}

EmpDaoImpl:

	package com.RequestMapping.Dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.RequestMapping.Dao.IEmpDao;
import com.RequestMapping.entity.Dept;
import com.RequestMapping.entity.Emp;
/**
 * 数据访问层-接口实现类
 */
@Repository("empDao")//相当于 IEmpDao empDao = new EmpDaoImpl();
public class EmpDaoImpl implements IEmpDao {

	private static List<Emp> empList = new ArrayList<Emp>();
	
	static{
		empList.add(new Emp(1111, "张三", "需求工程师", 9999, new Date(), 8000, 500, new Dept(10, "需求部", "东软大厦125C")));
		empList.add(new Emp(2222, "李四", "前端工程师", 9999, new Date(), 8000, 500, new Dept(20, "前端部", "东软大厦125C")));
		empList.add(new Emp(3333, "王五", "开发工程师", 9999, new Date(), 8000, 500, new Dept(30, "开发部", "东软大厦125C")));
		empList.add(new Emp(4444, "赵六", "测试工程师", 9999, new Date(), 8000, 500, new Dept(40, "测试部", "东软大厦125C")));
		empList.add(new Emp(5555, "田七", "实施工程师", 9999, new Date(), 8000, 500, new Dept(50, "实施部", "东软大厦125C")));
	}
	
	@Override
	public List<Emp> selectEmp() {
		return empList;
	}
}

EmpServiceImpl:

package com.RequestMapping.Service.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.RequestMapping.Dao.IEmpDao;
import com.RequestMapping.entity.Emp;
import com.RequestMapping.Service.IEmpService;

/**
 * 业务逻辑层-接口实现类
 */
@Service("empService")//相当于IEmpService empService = new EmpServiceImpl();
public class EmpServiceImpl implements IEmpService {

	//@Autowired @Qualifier("empDao") //默认根据类型匹配,通常结合@Qualifier指定引用名称使用
	@Resource(name="empDao") //默认根据名称匹配
	private IEmpDao empDao;
	
	@Override
	public List<Emp> selectEmp() {
		List<Emp> empList = empDao.selectEmp();
		
		return empList;
	}
}

EmpController:

package com.RequestMapping.Controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.RequestMapping.Service.IEmpService;
import com.RequestMapping.entity.Emp;

/**
 * 控制器:处理器请求,响应结果
 */
@Controller
@RequestMapping("/emp")//修饰类,访问路径http://localhost:8088/RequestMapping/emp/get
public class EmpController {  
	@Resource(name="empService")
	private IEmpService empService;
	/*
	 * @RequestMapping()参数解释:
	 * 					value:表示访问路径,可以省略比如:@RequestMapping("/get")
	 * 					method:表示访问路径时,配置请求方式是GET或POST
	 * 					params = {"empno"}:发起请求时,必须带参数,且参数名称为empno
	 * 					params = {"!empno"}:发起请求时,不能带名称为empno的参数
	 * 					params = {"empno=7788"}:发起请求时,必须带参数名称为empno,以及参数值必须为7788:可以用于普通用户以及管理员的判断
	 * 					params = {"empno=7788","ename"}:发起请求时,必须带参数名称为(第一个)empno,以及参数值必须为7788,第二个参数必须为ename
	 * 					
	 */
	@RequestMapping(value="/get",method = {RequestMethod.GET,RequestMethod.POST},params = {"empno"})//修饰类,在无修饰类的前提下,访问路径http://localhost:8088/RequestMapping/get
	public ModelAndView getEmps()
	{
		ModelAndView mav = new ModelAndView();
		
		//1.调用service方法,查询所有雇员信息
		List<Emp> empList= empService.selectEmp();
		
		//2.保存到作用域
		mav.addObject("empList", empList);
		
		//3.指定跳转的路径
		mav.setViewName("/empQuery.jsp");
		
		return mav;
	}
}

测试的jsp有 index.jsp和empQuery.jsp

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>	
	<a href="emp/get.action?empno=7788">点击获取所有雇员信息-------get请求</a>
	<form action="emp/get.action">
		<input type="submit" value="点击获取雇员信息------Post请求">
	
	</form>
</body>	
</html>

empQuery.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="1px" width="80%" style="border-collapse: collapse;">
		<tr height="50px">
			<th>序号</th>
			<th>雇员编号</th>
			<th>雇员姓名</th>
			<th>职位</th>
			<th>上级经理</th>
			<th>入职日期</th>
			<th>薪水</th>
			<th>奖金</th>
			<th>部门</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${empList}" var="emp" varStatus="status">
			<tr height="35px">
				<td>${status.index+1}</td>
				<td>${emp.empno}</td>
				<td>${emp.ename}</td>
				<td>${emp.job}</td>
				<td>${emp.mgr}</td>
				<td> <fmt:formatDate value="${emp.hiredate}" pattern="yyyy-MM-dd"/></td>
				<td>${emp.salary}</td>
				<td>${emp.comm}</td>
				<td>${emp.dept.dname}</td>
				<td>
					<a href="#">编辑</a>
					<a href="#">修改</a>
				</td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值