<meta charset="utf-8">
Spring MVC
一胜榔、Spring AOP的第七種方式說(shuō)明
在使用Spring AOP的第七種方式過(guò)程中橄抹,我遇到了一個(gè)問(wèn)題:如下圖
自己的代碼中沒(méi)有EventListenerMethodProcessor误续、DefaultEventListenerFactory與public abstract void org.springframework.beans.factory.SmartInitializingSingleton.afterSingletonsInstantiated()這些方法董饰,為什么還會(huì)運(yùn)行奥喻。經(jīng)過(guò)實(shí)驗(yàn),這些方法是Spring bean在實(shí)例化與初始化的過(guò)程中會(huì)自動(dòng)執(zhí)行的侈贷,并且還和spring版本有關(guān),在4.3.16上會(huì)出現(xiàn),在5.2.2上就不會(huì)出現(xiàn)俏蛮,前兩個(gè)方法只會(huì)運(yùn)行一次撑蚌,最后一個(gè)方法有一個(gè)單例模式的bean初始化了就會(huì)執(zhí)行搏屑。
bean的生命周期:
在xml文件bean標(biāo)簽中可以添加init-method與destroy-method分別指定bean對(duì)象的初始化方法與銷毀方法争涌,屬性值對(duì)應(yīng)的是該類中的方法:
<bean id="st" class="com.qianfeng.aop05.Student" init-method="drink" destroy-method="eat" />
二、Spring MVC
m:model:模型辣恋,javabean
v:view:視圖亮垫,html/jsp
c:controller:控制器:servlet
為了保證數(shù)據(jù)的安全性,建議將一些比較重要的頁(yè)面放在WEB-INF目錄下伟骨,這樣外界就無(wú)法直接訪問(wèn)饮潦,服務(wù)器內(nèi)部可以通過(guò)轉(zhuǎn)發(fā)訪問(wèn)。
1携狭、需要的依賴
使用maven方式創(chuàng)建web項(xiàng)目不需要導(dǎo)入Tomcat的包继蜡,只需要Tomcat插件,由于缺少了一些包逛腿,所以需要導(dǎo)入一些依賴:
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
第一個(gè)依賴是jsp的依賴稀并,第二個(gè)是servlet的依賴,第三個(gè)是jstl的依賴单默。
3稻轨、轉(zhuǎn)發(fā)器
DispatcherServlet.java
package com.qianfeng.controller;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/ProductInput","/ProductDetail"})
public class DispatcherServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uri = req.getRequestURI();
uri = uri.substring(uri.lastIndexOf("/")+1);
String url = null;
Controller controller = null;
if("ProductInput".equalsIgnoreCase(uri)){
controller = new ProductInput();
}else if("ProductDetail".equalsIgnoreCase(uri)){
controller = new ProductDetail();
}
if(controller!=null){
url = controller.handleRequest(req,resp);
req.getRequestDispatcher(url).forward(req,resp);
}
}
}
這個(gè)轉(zhuǎn)發(fā)器的作用相當(dāng)于銀行的大堂經(jīng)理,它不負(fù)責(zé)處理具體的業(yè)務(wù)雕凹,只負(fù)責(zé)轉(zhuǎn)發(fā)到響應(yīng)的頁(yè)面殴俱,這樣做可以將一些重復(fù)的工作提取出來(lái),減少工作量枚抵。
4线欲、控制器
Controller.java
package com.qianfeng.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface Controller {
public String handleRequest(HttpServletRequest request, HttpServletResponse response);
}
這個(gè)接口的作用的指定規(guī)則,讓所有的控制器的業(yè)務(wù)類都實(shí)現(xiàn)這個(gè)接口汽摹,重寫處理業(yè)務(wù)的具體方法
ProductDetail.java
package com.qianfeng.controller;
import com.qianfeng.bean.Product;
import com.qianfeng.form.ProductForm;
import com.qianfeng.validate.ProductValidate;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
public class ProductDetail implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
String pname = request.getParameter("pname");
String pid = request.getParameter("pid");
String sprice = request.getParameter("price");
String image = request.getParameter("image");
double price = sprice == null||sprice.length()==0?0.0:Double.parseDouble(sprice);
ProductForm pf = new ProductForm();
pf.setPname(pname);
pf.setPrice(price);
pf.setImage(image);
ProductValidate pv = new ProductValidate();
List<String> errors = pv.validate(pf);
String url = null;
if(errors!=null && !errors.isEmpty()){
request.setAttribute("errors",errors);
url = "/WEB-INF/view/index.jsp";
}else {
Product p = new Product();
p.setPid(Integer.parseInt(pid));
p.setPname(pf.getPname());
p.setPrice(pf.getPrice());
p.setImage(pf.getImage());
request.setAttribute("product",p);
url = "/WEB-INF/view/product.jsp";
}
return url;
}
}
ProductInput.java
package com.qianfeng.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProductInput implements Controller {
@Override
public String handleRequest(HttpServletRequest request, HttpServletResponse response) {
return "/WEB-INF/view/index.jsp";
}
}
這兩個(gè)控制器實(shí)現(xiàn)類的作用就是處理具體的業(yè)務(wù)李丰,并返回需要轉(zhuǎn)發(fā)的頁(yè)面。
ProductForm.java
package com.qianfeng.form;
public class ProductForm {
private String pname;
private double price;
private String image;
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
FormBean的作用是將JavaBean中的一些字段抽取出來(lái)單獨(dú)進(jìn)行驗(yàn)證逼泣,因?yàn)橛行┳侄慰赡懿环细袷交蛘邲](méi)有實(shí)際意義趴泌。
ProductValidate.java
package com.qianfeng.validate;
import com.qianfeng.form.ProductForm;
import java.util.ArrayList;
import java.util.List;
public class ProductValidate {
public List<String> validate(ProductForm pf){
List<String> errors = new ArrayList<>();
if(pf.getPname()==null||pf.getPname().length()==0){
errors.add("商品名稱不能為空!");
}
if(pf.getPrice()<0){
errors.add("商品價(jià)格不能為負(fù)拉庶!");
}
if(pf.getImage()==null||pf.getImage().length()==0){
errors.add("商品圖片不能為空嗜憔!");
}
return errors;
}
}
這個(gè)類的具體作用就是對(duì)FormBean中的字段進(jìn)行驗(yàn)證