本系列博客内容为:做DRP系统中的常用功能。
该项目采用MVC架构
- C(Controller)控制器,主要职责;1、取得表单参数;2、调用业务逻辑;3、转向页面
- M(Model)模型,主要职责:1、业务逻辑;2、保存数据的状态
- V(View)视图,主要职责:显示
优点:职责清楚,较适合大型项目架构。分层主要是为了好管理,能更好的适应需求的变化,能够更好的人员分配。
缺点:不适合小型项目开发(主要是层次太多)
UML图如下所示:
界面如下:
web.xml:配置信息
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID"> <servlet> <servlet-name>ShowModifyItemServlet</servlet-name> <servlet-class>com.bjpowernode.drp.basedata.web.ShowModifyItemServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ShowModifyItemServlet</servlet-name> <url-pattern>/servlet/item/ShowModifyItemServlet</url-pattern> </servlet-mapping> </web-app>
V(View)视图:item_maint.jsp,负责显示所有的物料信息。并控制多选框只能选中一个
1 <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> 3 4 <% 5 String path = request.getContextPath(); 6 String basePath = request.getScheme() + "://" 7 + request.getServerName() + ":" + request.getServerPort() 8 + path + "/"; 9 %>
1 <html> 2 <head> 3 <base href="<%=basePath%>"> 4 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 5 <title>物料维护</title> 6 <script type="text/javascript"> 7 8 function modifyItem() { 9 var selectFlags = document.getElementsByName("selectFlag"); 10 var count = 0; 11 var j = 0; 12 for (var i=0; i<selectFlags.length; i++) { 13 if (selectFlags[i].checked) { 14 j = i; 15 count++; 16 } 17 } 18 if (count == 0) { 19 alert("请选择需要修改的物料!"); 20 return; 21 } 22 if (count > 1) { 23 alert("一次只能修改一个物料!"); 24 return; 25 }
//j页面转至需要提交的页面,并提交多选框选中的选项的id 26 window.self.location = "<%=basePath%>servlet/item/ShowModifyItemServlet?itemNo=" + selectFlags[j].value; 27 } 28 29 </script> 30 </head>
31 <body class="body1"> 32 <input type="checkbox" name="selectFlag" class="checkbox1" value="<%=item.getItemNo()%>"> 34 <input name="btnModify" class="button1" type="button" id="btnModify" value="修改" onClick="modifyItem()">36
C(Controller)控制器:ShowModifyItemServlet.java显示选中的物料详细信息。
package com.bjpowernode.drp.basedata.web; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 显示修改物料页面Servlet * @author Administrator * */ public class ShowModifyItemServlet extends HttpServlet { private ItemManager itemManager; private DataDictManager dataDictManager; @Override public void init() throws ServletException { //初始化时,自动new一个成员对象 itemManager = new ItemManagerImpl(); dataDictManager = DataDictManager.getInstance(); } @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //取得物料代码 String itemNo = request.getParameter("itemNo"); //如果获取的是字符串数组,则使用以下形式
//String itemNos=request.getParameterValues("selectFlag"); //根据物料代码查询 Item item = itemManager.findItemById(itemNo); //将物料信息设置到request里 request.setAttribute("item", item); //取得物料类别列表 List itemCategoryList = dataDictManager.findItemCategoryList(); //取得物料单位列表 List itemUnitList = dataDictManager.findItemUnitList(); request.setAttribute("itemCategoryList", itemCategoryList); request.setAttribute("itemUnitList", itemUnitList); //转发 request.getRequestDispatcher("/basedata/item_modify.jsp").forward(request, response); } }
V(View)视图:item_modify.jsp,修改物料详细信息。
1 <%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> 2 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" 6 + request.getServerName() + ":" + request.getServerPort() 7 + path + "/"; 8 9 Item item = (Item)request.getAttribute("item"); 10 List itemCategoryList = (List)request.getAttribute("itemCategoryList"); 11 List itemUnitList = (List)request.getAttribute("itemUnitList"); 12 %> 13 <html> 14 <head> 15 <base href="<%=basePath %>"> 16 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 17 <script type="text/javascript">
<!--利用submit提交表单时,限定物料名称不能为空--> 18 function validateForm(form) { 19 if (trim(form.itemName.value) == "") { 20 alert("物料名称不能为空!"); 21 form.itemName.focus(); 22 return false; 23 } 24 return true; 25 } 26 </script> 27 </head> 28 29 <body class="body1"> 30 <form name="itemForm" target="_self" id="itemForm" action="servlet/item/ModifyItemServlet" method="post" onsubmit="return validateForm(this)"> 31 <div align="center"> 32 //在对应的位置,显示item的详细信息 33 物料名称:<input name="itemName" type="text" class="text1" id="itemName" value="<%=item.getItemName() %>"> 34 35 <select name="category" class="select1" id="category"> 36 <% <!--下拉框中加载所有的物料类别,并选中对应的选项--> 37 for (int i=0; i<itemCategoryList.size(); i++) { 38 ItemCategory ic = (ItemCategory)itemCategoryList.get(i); 39 String selectedString = ""; 40 if (item.getItemCategory().getId().equals(ic.getId())) { 41 selectedString = "selected"; 42 } 43 %> 44 <option value="<%=ic.getId() %>" <%=selectedString %>><%=ic.getName() %></option> 45 <% 46 } 47 %> 48 </select> 49 50 </form> 51 </body> 52 </html>
C(Controller)控制器:ModifyItemServlet.java修改物料详细信息
package com.bjpowernode.drp.basedata.web; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 修改物料Servlet * @author Administrator * */ public class ModifyItemServlet extends HttpServlet { //定义成员变量 private ItemManager itemManager; //servlet不是线程安全的,但是本例中不做修改,因此是可以使用初始化成员变量的。如果涉及到了修改成员变量,而不能使用。 @Override public void init() throws ServletException {
//把itemManager放在初始化中 itemManager = new ItemManagerImpl(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //取得表单数据 String itemNo = request.getParameter("itemNo"); String itemName = request.getParameter("itemName"); String spec = request.getParameter("spec"); String pattern = request.getParameter("pattern"); String category = request.getParameter("category"); String unit = request.getParameter("unit"); //构造Item对象 Item item = new Item(); item.setItemNo(itemNo); item.setItemName(itemName); item.setSpec(spec); item.setPattern(pattern); //构造物料类别 ItemCategory itemCategory = new ItemCategory(); itemCategory.setId(category); item.setItemCategory(itemCategory); //构造物料单位 ItemUnit itemUnit = new ItemUnit(); itemUnit.setId(unit); item.setItemUnit(itemUnit); itemManager.modifyItem(item); //注意:此处使用页面重定向至servlet中,而不是jsp文件中,否则会出现500错误 //response.sendRedirect(request.getContextPath() + "/basedata/item_maint.jsp"); response.sendRedirect(request.getContextPath() + "/servlet/item/SearchItemServlet"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }