Article Directory
MVC framework of thought
1. What is MVC?
MVC full name is the Model View Controller, is a model (model) - view (view) - abbreviation controller (controller), and
it is a software design model, in a business logic, data, interface show a method of separating an organization code.
MVC core idea : their duties, that is, each doing.
2, MVC structure
M: Entity domain model (noun), the process domain model (verb)
V: JSP / iOS / Android
C: the servlet / Action
do distribute web browser requests a
service call processing dao project operations
dao database operations
Note 1: You can not cross-layer calls
Note 2: You can only call top-down appearance
In the MVC framework to understand the premise of thinking first to know about its working schematics [core]
central control dynamic invocation sub-controller calls to accomplish specific business logic
Summary:
The central controller: if there's a sub-controller to handle user requests, to process requests if you call the sub-controller; not on the error, it can not process the request.
The sub-controller: is treated with a user request.
case study
Using a main controller and sub-controller of the basic functions of a computer
entity class Cal
public class Cal {
private String num1;
private String num2;
public String getNum1() {
return num1;
}
public void setNum1(String num1) {
this.num1 = num1;
}
public String getNum2() {
return num2;
}
public void setNum2(String num2) {
this.num2 = num2;
}
public Cal(String num1, String num2) {
super();
this.num1 = num1;
this.num2 = num2;
}
public Cal() {
super();
}
}
main controller
/**
* 主控制器
* @author zhouyanpeng
*
*/
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = -7094025920085803724L;
private Map<String, Action> actionMap=new HashMap<>();
//初始化方法
public void init() {
//添加子控制器
actionMap.put("/addCal", new AddCalAction());
actionMap.put("/delCal", new DelCalAction());
actionMap.put("/chenCal", new ChenCalAction());
actionMap.put("/chuCal", new ChuCalAction());
}
@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 {
init();//初始化
String url = req.getRequestURI();//MVC/xxx.action
url = url.substring(url.lastIndexOf("/"), url.lastIndexOf("."));
// 相当于向上转型
Action action = actionMap.get(url);//根据url获取子控制器
action.execute(req, resp);
}
}
Sub-controller interface
/**
* 子控制器
* 专门用来处理业务逻辑
* @author zhouyanpeng
*
*/
public interface Action {
void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException ;
}
Write Math class
addition
public class AddCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)+Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
Subtraction
public class DelCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)-Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
multiplication
public class ChenCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)*Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
division
public class ChuCalAction implements Action {
@Override
public void execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String num1=req.getParameter("num1");
String num2=req.getParameter("num2");
req.setAttribute("res", Integer.valueOf(num1)/Integer.valueOf(num2));
req.getRequestDispatcher("calRes.jsp").forward(req, resp);
}
}
Configure the web
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>com.zyp.framework.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
test
<script type="text/javascript">
function doSub(value){
if(value==1){
calForm.action="${pageContext.request.contextPath}/addCal.action";
}
else if(value==2){
calForm.action="${pageContext.request.contextPath}/delCal.action";
}
else if(value==3){
calForm.action="${pageContext.request.contextPath}/chenCal.action";
}
else{
calForm.action="${pageContext.request.contextPath}/chuCal.action";
}
calForm.submit();
}
</script>
</head>
<body>
<form id="claForm" name="calForm" action="">
num1:<input type="text" name="num1" /><br/>
num2:<input type="text" name="num2" /><br/>
<input type="button" onclick="doSub(1)" value="+" />
<input type="button" onclick="doSub(2)" value="-" />
<input type="button" onclick="doSub(3)" value="x" />
<input type="button" onclick="doSub(4)" value="/" />
</form>
Enter billing numbers
Output