Spring框架之SpringMVC(2.0)

Spring框架之SpringMVC(2.0)

编写第一个MVC程序:

在这里插入图片描述
将接受的参数可以直接定义在方法上:

1,建立关联的vo类:

emp.java:

package cn.mldn.vo;
import java.io.Serializable;
import java.util.Date;
@SuppressWarnings("serial")
public class Emp implements Serializable {
	private Integer empno ;
	private String ename ;
	private Date hiredate ;
	private Double sal ;
	private Dept dept ;
	public Integer getEmpno() {
		return empno;
	}
	public void setEmpno(Integer empno) {
		this.empno = empno;
	}
	public String getEname() {
		return ename;
	}
	public void setEname(String ename) {
		this.ename = ename;
	}
	public Date getHiredate() {
		return hiredate;
	}
	public void setHiredate(Date hiredate) {
		this.hiredate = hiredate;
	}
	public Double getSal() {
		return sal;
	}
	public void setSal(Double sal) {
		this.sal = sal;
	}
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "Emp [empno=" + empno + ", ename=" + ename + ", hiredate="
				+ hiredate + ", sal=" + sal + ", dept=" + dept + "]";
	}
}

Dept.java

package cn.mldn.vo;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Dept implements Serializable {
	private Integer deptno ;
	private String dname ;
	public Integer getDeptno() {
		return deptno;
	}
	public void setDeptno(Integer deptno) {
		this.deptno = deptno;
	}
	public String getDname() {
		return dname;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	@Override
	public String toString() {
		return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
	}
}
2,在SpringMVC之中也将处理的控制器称为Action;

定义EmpAction:

package cn.mldn.action;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.mldn.vo.Emp;
@Controller
@RequestMapping("/pages/back/emp/*")
// 此处描述的是定义父路径
public class EmpAction { // 完全是一个独立的类
	@RequestMapping("empAdd")  
	public void add(Emp emp){ //此处表示接受的参数就是Emp类型
		System.out.println(emp);
	}
}
3,编写具体的测试程序:采用地址重写的方式传递。
父路径:/pages/back/emp/
操作路径:empAdd
完整路径:http://localhost/SMVCProject/pages/back/emp/empAdd.action?
empno=7369&ename=smith&sal=800.0&dept.deptno=20&dept.dname=销售部

好处:Action类中所需要接收到参数不再以属性的形式出现,因为属性会占用堆内存。并且 进行单层VO对象传递的时候,发现不再需要传递对象名称。多层设置VO属性的时候,发现不再需要进行对象的实例化操作处理了。

4,实现时间日期的数据传递

在EmpAction类中追加转换器:

package cn.mldn.action;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.mldn.vo.Emp;
@Controller
@RequestMapping("/pages/back/emp/*")
// 此处描述的是定义父路径
public class EmpAction { // 完全是一个独立的类
	@RequestMapping("empAdd")  
	public void add(Emp emp){ //此处表示接受的参数就是Emp类型
		System.out.println(emp);
	}
	@InitBinder
	public void initBinder(WebDataBinder binder) { // 转换器
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 将自定义的转换器进行配置,表示以后如果发现有Date类型,就使用sdf对象进行转换,并且允许数据为null
binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(
				sdf, true));
	}
}

传入地址:

http://localhost/SMVCProject/pages/back/emp/empAdd.action?
empno=7369&ename=smith&sal=800.0&dept.deptno=20&dept.dname=销售部&hiredate=1998-10-10 11:11:11

在这里插入图片描述
任何的开发Action都一定需要有一个父类存在,以上的代码就需要设置在父类之中。

5,区分get与post请求:
@RequestMapping(value="empAdd",method=RequestMethod.POST)
@RequestMapping(value="empAdd",method=RequestMethod.GET)
6,在MVC开发之中,控制层的主要功能是进行请求处理及页面跳转操作的,而在页面跳转之后往往需要传递一堆的内容,因此提供了一个ModelAndView类。

观察org.springframework.web.servlet.ModelAndView程序类:

NO方法名称类型描述
1public ModelAndView()普通无参构造,不跳转(ajax)
2public ModelAndView(String viewName)普通设置跳转路径,需要写完整路径
3public ModelAndView addObject(String attributeName,Object attributeValue)普通设置传递的属性内容,相当于:request.setAttribute()
4public void setViewName(String viewName)普通设置路径
5public ModelAndView addAllObject(Map< String , ?> modelMap)普通自动处理Map集合的数据
ModelAndView 就相当于替代了之前的request.getRequestDispatcher(),forward(),request.setAttribute()。

范例:如果处理的Action方法不需要跳转,则也要使用ModelAndView作为返回值类型,返回null即可;

package cn.mldn.action;
import java.text.SimpleDateFormat;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import cn.mldn.vo.Emp;
@Controller
@RequestMapping("/pages/back/emp/*")
// 此处描述的是定义父路径
public class EmpAction { // 完全是一个独立的类
	@RequestMapping(value = "empAdd")
	public ModelAndView add(Emp emp) { // 此处表示接收的参数就是Emp类型
		System.out.println(emp);
		return null;
	}
	@InitBinder
	public void initBinder(WebDataBinder binder) { // 转换器
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		// 将自定义的转换器进行配置,表示以后如果发现有Date类型,就使用sdf对象进行转换,并且允许数据为null
		binder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));
	}
}

范例:设置跳转内容
1,定义一个forward.jsp页面:

  <body>
   	<h1>${url}</h1>
   	<h1>${msg}</h1>
   	<h1>${myemp}</h1>
  </body>

2,随后在Action之中跳转到此页面:

public class EmpAction { // 完全是一个独立的类
	@RequestMapping(value = "empAdd")
	public ModelAndView add(Emp emp) { // 此处表示接收的参数就是Emp类型
		System.out.println(emp);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/forward.jsp"); // 设置跳转路径
		mav.addObject("msg", "天要下雨啦,收衣服啦!");
		mav.addObject("myemp", emp);
		return null;
	}
	}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值