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 | 方法名称 | 类型 | 描述 |
1 | public ModelAndView() | 普通 | 无参构造,不跳转(ajax) |
2 | public ModelAndView(String viewName) | 普通 | 设置跳转路径,需要写完整路径 |
3 | public ModelAndView addObject(String attributeName,Object attributeValue) | 普通 | 设置传递的属性内容,相当于:request.setAttribute() |
4 | public void setViewName(String viewName) | 普通 | 设置路径 |
5 | public ModelAndView addAllObject(Map< String , ?> modelMap) | 普通 | 自动处理Map集合的数据 |
范例:如果处理的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;
}
}