將接受的參數可以直接定義在方法上:
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;}@Overridepublic 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;}@Overridepublic String toString() {return "Dept [deptno=" + deptno + ", dname=" + dname + "]";}
}
maven java。定義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);}
}
父路徑:/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屬性的時候,發現不再需要進行對象的實例化操作處理了。
在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);}@InitBinderpublic 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
springmvc屬于什么框架。
任何的開發Action都一定需要有一個父類存在,以上的代碼就需要設置在父類之中。
@RequestMapping(value="empAdd",method=RequestMethod.POST)
@RequestMapping(value="empAdd",method=RequestMethod.GET)
觀察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;}@InitBinderpublic void initBinder(WebDataBinder binder) { // 轉換器SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 將自定義的轉換器進行配置,表示以后如果發現有Date類型,就使用sdf對象進行轉換,并且允許數據為nullbinder.registerCustomEditor(java.util.Date.class, new CustomDateEditor(sdf, true));}
}
范例:設置跳轉內容
1,定義一個forward.jsp頁面:
<body><h1>${url}</h1><h1>${msg}</h1><h1>${myemp}</h1></body>
Spring boot?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;}}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态