spring MVC

#千锋逆战#

1.导入依赖

使用maven方式创建web项目不需要导入Tomcat的包,只需要Tomcat插件,由于缺少了一些包,所以需要导入一些依赖:javax.servlet.jspjsp-api2.2providedjavax.servletjavax.servlet-api3.0.1providedjstljstl1.2

第一个依赖是jsp的依赖,第二个是servlet的依赖,第三个是jstl的依赖。

2.转发器DispatcherServlet.javapackagecom.qianfeng.controller;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.io.IOException;@WebServlet(urlPatterns={"/ProductInput","/ProductDetail"})publicclassDispatcherServletextendsHttpServlet{@OverrideprotectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{doPost(req,resp);}@OverrideprotectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)throwsServletException,IOException{Stringuri=req.getRequestURI();uri=uri.substring(uri.lastIndexOf("/")+1);Stringurl=null;Controllercontroller=null;if("ProductInput".equalsIgnoreCase(uri)){controller=newProductInput();}elseif("ProductDetail".equalsIgnoreCase(uri)){controller=newProductDetail();}if(controller!=null){url=controller.handleRequest(req,resp);req.getRequestDispatcher(url).forward(req,resp);}}}这个转发器的作用相当于银行的大堂经理,它不负责处理具体的业务,只负责转发到响应的页面,这样做可以将一些重复的工作提取出来,减少工作量。

3.控制器Controller.javapackagecom.qianfeng.controller;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicinterfaceController{publicStringhandleRequest(HttpServletRequestrequest,HttpServletResponseresponse);}

这个接口的作用的指定规则,让所有的控制器的业务类都实现这个接口,重写处理业务的具体方法ProductDetail.javapackagecom.qianfeng.controller;importcom.qianfeng.bean.Product;importcom.qianfeng.form.ProductForm;importcom.qianfeng.validate.ProductValidate;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjava.util.List;publicclassProductDetailimplementsController{@OverridepublicStringhandleRequest(HttpServletRequestrequest,HttpServletResponseresponse){Stringpname=request.getParameter("pname");Stringpid=request.getParameter("pid");Stringsprice=request.getParameter("price");Stringimage=request.getParameter("image");doubleprice=sprice==null||sprice.length()==0?0.0:Double.parseDouble(sprice);ProductFormpf=newProductForm();pf.setPname(pname);pf.setPrice(price);pf.setImage(image);ProductValidatepv=newProductValidate();Listerrors=pv.validate(pf);Stringurl=null;if(errors!=null&&!errors.isEmpty()){request.setAttribute("errors",errors);url="/WEB-INF/view/index.jsp";}else{Productp=newProduct();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";}returnurl;}}ProductInput.javapackagecom.qianfeng.controller;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;publicclassProductInputimplementsController{@OverridepublicStringhandleRequest(HttpServletRequestrequest,HttpServletResponseresponse){return"/WEB-INF/view/index.jsp";}}

这两个控制器实现类的作用就是处理具体的业务,并返回需要转发的页面。ProductForm.javapackagecom.qianfeng.form;publicclassProductForm{privateStringpname;privatedoubleprice;privateStringimage;publicStringgetPname(){returnpname;}publicvoidsetPname(Stringpname){this.pname=pname;}publicdoublegetPrice(){returnprice;}publicvoidsetPrice(doubleprice){this.price=price;}publicStringgetImage(){returnimage;}publicvoidsetImage(Stringimage){this.image=image;}}

FormBean的作用是将JavaBean中的一些字段抽取出来单独进行验证,因为有些字段可能不符合格式或者没有实际意义。ProductValidate.javapackagecom.qianfeng.validate;importcom.qianfeng.form.ProductForm;importjava.util.ArrayList;importjava.util.List;publicclassProductValidate{publicListvalidate(ProductFormpf){Listerrors=newArrayList<>();if(pf.getPname()==null||pf.getPname().length()==0){errors.add("商品名称不能为空!");}if(pf.getPrice()<0){errors.add("商品价格不能为负!");}if(pf.getImage()==null||pf.getImage().length()==0){errors.add("商品图片不能为空!");}returnerrors;}}

这个类的具体作用就是对FormBean中的字段进行验证

你可能感兴趣的:(spring MVC)