maven java,Spring框架之SpringMVC(2.0)

 2023-10-31 阅读 30 评论 0

摘要:Spring框架之SpringMVC(2.0) 編寫第一個MVC程序: 將接受的參數可以直接定義在方法上: 1,建立關聯的vo類: emp.java: package cn.mldn.vo; import java.io.Serializable; import java.util.Date; @SuppressWarnings("serial

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;}@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 + "]";}
}

2,在SpringMVC之中也將處理的控制器稱為Action;

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);}
}

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);}@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都一定需要有一個父類存在,以上的代碼就需要設置在父類之中。

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;}@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;}}

在這里插入圖片描述

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/167763.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息