什么是MVC
- MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,它是一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码
- Model :? jsp+jdbc
- Model : ->MVC
- 核心思想:各司其职
MVC结构
- V view
- jsp/ios/android
- C controller
- M model
- servlet/action
- 实体域模型(名词)entity
- 过程域模型(动词)dao/biz
注1:不能跨层调用
注2:只能出现由上而下的调用
具体代码步骤
//写一个中央代码器ActionServlet管理所有servlet的传输内容
/**中央控制器
* @author Administrator
*
*/
public class ActionServlet 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 url = req.getRequestURI();
int start=url.lastIndexOf("/");
int end=url.lastIndexOf(".");
if(end!=-1) {//如果有点的话
url=url.substring(start, end);
}else {//如果吗没有点的话
url=url.substring(start);
}
//根据截取的url里面找到对应的configModel中找到ActionModel
try {
ConfigModel configModel = ConfigModel.createConfigModel("/config.xml");
ActionModel actionModel = configModel.get(url);
/**根据全限定名,得到对象,实例化对象
* ,调取对象的方法
*/
String type=actionModel.getType();
System.out.println(type);
Class c = Class.forName(type);
Object obj = c.newInstance();
String method = req.getParameter("method");
Method m = c.getMethod(method);
//给要传的属性赋值
//先定义一个接口,然后写一个得到模型的方法getModel()
//if(obj instanceof modelDriver) {//如果继承obj实现了modelDriver
StudentBiz ss=(StudentBiz)obj;
Student stu = ss.getModel();
BeanUtils.populate(stu, req.getParameterMap());//则给属性赋值
}
Object str = m.invoke(obj);
//根据方法进行页面跳转
ForwardModel forward = actionModel.get(str.toString());
System.out.println(forward);
if(forward.isRedirect()) {
resp.sendRedirect(forward.getPath());
}else {
req.getRequestDispatcher(forward.getPath()).forward(req, resp);
}
} catch (Exception e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
}
}
?
|