MVC综合设计.docx
《MVC综合设计.docx》由会员分享,可在线阅读,更多相关《MVC综合设计.docx(42页珍藏版)》请在冰豆网上搜索。
![MVC综合设计.docx](https://file1.bdocx.com/fileroot1/2023-2/6/bce4db45-3617-4152-9112-0356710cfa47/bce4db45-3617-4152-9112-0356710cfa471.gif)
MVC综合设计
昆明理工大学信息工程与自动化学院学生实验报告
(2012—2013学年第一学期)
课程名称:
JAVAEE技术开课实验室:
信自楼4442012年12月25日
年级、专业、班
计科10级
学号
201010803116
姓名
李力
成绩
实验项目名称
实验四MVC综合设计
指导教师
杨云飞
教师评语
教师签名:
年月日
一、实验目的
通过上机,熟练掌握5层MVC设计模式。
二、内容及要求
上机内容:
用5层MVC分层模式完成用户管理的功能。
具有增加、删除、更新、显示、查询等功能。
三、步骤及具体实施
1、简要描述程序的开发流程、文件部署截图、及各种配置;
开发流程:
文件部署:
数据库:
2、程序代码
Com.city.oa.action中EmployeeMainAction:
packagecom.city.oa.action;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.city.oa.business.IEmployee;
importcom.city.oa.factory.BussinessFactory;
publicclassEmployeeAddActionextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;
publicEmployeeAddAction(){
super();
}
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("gb2312");
Stringid=request.getParameter("empid");
Stringname=request.getParameter("name");
Stringpassword=request.getParameter("password");
StringageStr=request.getParameter("age");
intage=Integer.parseInt(ageStr);
IEmployeeemp=BussinessFactory.getEmployee();
try
{
emp.add(id,name,password,age);
}
catch(Exceptione)
{
System.out.println("modifyEmployeeerror:
"+e.getMessage());
}
response.sendRedirect("employee/main.do");
}
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
Com.city.oa.action中EmployeeDeletAction.java:
packagecom.city.oa.action;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.city.oa.business.*;
importcom.city.oa.factory.*;
publicclassEmployeeDeletActionextendsHttpServlet{
/**
*
*/
privatestaticfinallongserialVersionUID=1L;
/**
*Constructoroftheobject.
*/
publicEmployeeDeletAction(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
/**
*ThedoGetmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
/**
*ThedoPostmethodoftheservlet.
*
*Thismethodiscalledwhenaformhasitstagvaluemethodequalstopost.
*
*@paramrequesttherequestsendbytheclienttotheserver
*@paramresponsetheresponsesendbytheservertotheclient
*@throwsServletExceptionifanerroroccurred
*@throwsIOExceptionifanerroroccurred
*/
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringuserid=request.getParameter("userid");
IEmployeeemp=BussinessFactory.getEmployee();
try
{
emp.delete(userid);
}catch(Exceptione)
{
System.out.println("DeletEmployeeerror:
"+e.getMessage());
}
response.sendRedirect("main.do");
}
/**
*Initializationoftheservlet.
*
*@throwsServletExceptionifanerroroccurs
*/
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
GetIdModify.java:
packagecom.city.oa.action;
importjava.io.IOException;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.city.oa.business.*;
importcom.city.oa.factory.*;
importcom.city.oa.value.*;
publicclassEmployeeGetIdModifyextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringid=request.getParameter("empid");
IEmployeeemp=BussinessFactory.getEmployee();
try
{
EmployeeValuegetEmpById=emp.getEmployee(id);
request.setAttribute("getEmpById",getEmpById);
RequestDispatcherrd=request.getRequestDispatcher("Modify.jsp");
rd.forward(request,response);
}
catch(Exceptione)
{
System.out.println("forwardvaluestomodifyerror:
"+e.getMessage());
}
}
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
LoginActionAction.java:
packagecom.city.oa.action;
importjava.io.IOException;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
importcom.city.oa.business.*;
importcom.city.oa.factory.*;
@SuppressWarnings("serial")
publicclassEmployeeLoginActionextendsHttpServlet{
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
Stringempid=request.getParameter("empid");
Stringpassword=request.getParameter("password");
booleancheck=false;
IEmployeeemp=BussinessFactory.getEmployee();
try{
check=emp.check(empid,password);
if(check==true)
{
HttpSessionsession=request.getSession(true);
session.setAttribute("empid",empid);
}
}
catch(Exceptione)
{
System.out.println("loginerror:
"+e.getMessage());
}
System.out.println("youhaveloginsuccess");
System.out.println("empid"+empid);
response.sendRedirect("employee/main.do");
}
}
EmployeeModifyAction.java:
packagecom.city.oa.action;
importjava.io.IOException;
importjava.util.*;
importcom.city.oa.business.*;
importcom.city.oa.factory.*;
importcom.city.oa.value.EmployeeValue;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;
@SuppressWarnings("serial")
publicclassEmployeeMainActionextendsHttpServlet{
/**
*Constructoroftheobject.
*/
publicEmployeeMainAction(){
super();
}
/**
*Destructionoftheservlet.
*/
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog
//Putyourcodehere
}
publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
//新加的;
intcountperpage=5;
HttpSessionsession=request.getSession();
StringpageIndex=request.getParameter("pageIndex");
if(pageIndex==null)
{
pageIndex="1";
}
//原来的;
try{
IEmployeeemp=BussinessFactory.getEmployee();
ListempList=emp.getlist();
request.setAttribute("empList",empList);
//新加的;
intpageCount;
if(empList.size()%countperpage==0)
{
pageCount=empList.size()/countperpage;
}
else
{
pageCount=empList.size()/countperpage+1;
}
session.setAttribute("pageCount",pageCount);
session.setAttribute("empListCount",empList.size());
//根据当前页数在查询集合中获取数据
intcurrentPageIndex=Integer.parseInt(pageIndex);
ArrayListpageempList=newArrayList();
for(inti=0;i{
if(i>=(currentPageIndex-1)*countperpage&&i<(currentPageIndex)*countperpage)
{
pageempList.add(empList.get(i));
System.out.println("i"+i);
}
}
session.setAttribute("pageempList",pageempList);
session.setAttribute("currentPageIndex",currentPageIndex);
//原来代码;
RequestDispatcherrd=request.getRequestDispatcher("main.jsp");
rd.forward(request,response);
System.out.println("currentPageIndex"+currentPageIndex);
System.out.println("pageempList"+pageempList);
System.out.println("pageCount"+pageCount);
}catch(Exceptione){
Stringmess=e.getMessage();
response.sendRedirect("../error.jsp?
mess="+mess);
}
}
publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doGet(request,response);
}
publicvoidinit()throwsServletException{
//Putyourcodehere
}
}
Com.city.oa.bussiness中IEmployee.java
packagecom.city.oa.business;
importjava.util.List;
importcom.city.oa.value.*;
publicinterfaceIEmployee{
//增加员工
publicvoidadd(Stringuserid,Stringname,Stringpassword,intage)throwsException;
//修改员工
publicvoidmodify(Stringuserid,Stringname,Stringpassword,intage)throwsException;
//删除员工
publicvoiddelete(Stringuserid)throwsException;
//取得所有员工列表
publicListgetlist()throwsException;
//取得一个员工
publicEmployeeValuegetEmployee(Stringuserid)throwsException;
//验证密码
p