在MVC之Servlet控制器(一)中,我们将业务逻辑的代码都写在了Servlet中,让Servlet显得非常的复杂,为了避免这个问题,我们将业务逻辑代码提取到
独立的controller类中。两个controller类都实现了Controller接口,Controller接口只有handleRequest一个方法,接口实现类通过这个方法访问当前请求的
HttpServletRequest/HttpServletResponse对象 。
一、整体思路
二、代码清单
- Controller接口
public interface Controller {
public String handleRequest(HttpServletRequest request,
HttpServletResponse response) ;
}
- 两个controller类
package app02b.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class InputProductController implements Controller{
@Override
public String handleRequest(HttpServletRequest request,
HttpServletResponse response) {
return "/WEB-INF/jsp/ProductForm.jsp";
}
}
另外一个
package app02b.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import app02b.domain.Product;
import app02b.form.ProductForm;
public class SaveProductController implements Controller {
@Override
public String handleRequest(HttpServletRequest request,
HttpServletResponse response) {
ProductForm productForm = new ProductForm();
productForm.setName(request.getParameter("name"));
productForm.setDescription(request.getParameter("description"));
productForm.setPrice(request.getParameter("price"));
//create model
Product product = new Product();
product.setName(productForm.getName());
product.setDescription(productForm.getDescription());
try {
product.setPrice(Float.parseFloat(productForm.getPrice()));
} catch (Exception e) {
e.printStackTrace();
}
//code to add product to the DB
request.setAttribute("product", product);
return "/WEB-INF/jsp/ProductDetails.jsp" ;
}
}
- DispatcherServlet
package app02b.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import app02b.controller.InputProductController;
import app02b.controller.SaveProductController;
/**
* 这里只分发请求,业务的处理转移到了controller中
* @author lsj
*/
public class DispatcherServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}
private void process(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uri = request.getRequestURI() ;
int lastIndex = uri.lastIndexOf("/") ;
String action = uri.substring(lastIndex+1);
//dispatch request to controllers according to action
//and return View name in dispatchUrl
String dispatchUrl = null ;
if (action.equals("product_input.action")){
System.out.println("product_input.action");
InputProductController controller = new InputProductController() ;
dispatchUrl = controller.handleRequest(request, response);
}else if (action.equals("product_save.action")) {
System.out.println("product_save.action");
SaveProductController controller = new SaveProductController() ;
dispatchUrl = controller.handleRequest(request, response) ;
}
if (dispatchUrl!= null){
RequestDispatcher rd = request.getRequestDispatcher(dispatchUrl);
rd.forward(request, response);
}
}
}
我们输入 http://localhost:8080/app02b/product_input.action后,
请求先到DispatcherServlet,然后按照请求的action名,分发请求到Controller中,controller返回View的name ,再跳转到view 。