SpringMVC实现的两种方式

SpringMVC基础

Spring MVC是基于Model 2实现的技术框架,Model 2是经典的MVC模型在web应用中的变体。model 1的开发模式是:jsp+javabean的模式,它的核心是jsp页面。model 2的开发模式是:jsp+servlet+javabean的模式。

一、运行原理

用户发起请求后,由前端控制器DispatcherServlet接受用户请求响应,然后前端可控制性请求查找Handler,处理器映射器HandleMapper收到请求后通过xml或者注解,根据url找到对应的Handler(也就是Controller),找到之后前端控制器请求处理器适配器去执行Handler,处理器适配器调用处理器Handler的方法执行,返回ModelAndView,处理器适配器拿到ModelAndView后返回给前端控制器,前端控制器拿到ModelAndView后请求视图解析器去解析,解析完成之后将View返回给前端控制器,前端控制器将View进行渲染之后展示给用户。具体流程如下图所示:

[图片上传失败...(image-6f568f-1584023191004)]

SpringMVC实现的两种方式

接口方法

1.在web.xml添加标签


aaa
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:spring-mvc.xml



aaa
/


2.添加spring-mvc.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">








3.会根据输入的地址查找servlet

SaveProductController.java

public class SaveProductController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {


String spid = request.getParameter("pid");
String pname = request.getParameter("pname");
String sprice = request.getParameter("price");

int pid = spid == null ? 0 : Integer.parseInt(spid);
double price = sprice==null ? 0.0 : Double.parseDouble(sprice);

Product p = new Product();//javaBean

p.setPid(pid);
p.setPname(pname);
p.setPrice(price);

/**

  • 创建模型视图对象
  • 该代码等同于以前的两行代码
  •      request.setAttribute("p", p);
    
  •      request.getRequestDispatcher("detail.jsp").forward(request, response);
    

*/
ModelAndView mv = new ModelAndView("detail.jsp", "p", p);


return mv;
}
}

注解方法

1.在web.xml添加标签


aaa
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:spring-mvc.xml



aaa
/


2.添加spring-mvc.xml


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">









在指定包下,扫描,在根据url指定方法

EmpController.java

@Controller
public class EmpController {

private IEmpService empService = new EmpServiceImpl();

/**

  • 如果有了users请求,那么该方法会被调用,返回值为将来要渲染的页面
  • @return
    */
    @RequestMapping("/users")
    public String getUsersPage(Model model, HttpSession session){

    List list = empService.getAllEmps();

    model.addAttribute("list", list);

    session.setAttribute("list", list);

    return "user.jsp";
    }

    @RequestMapping("/getEmpByEid")
    public String getEmpByEid(HttpServletRequest request, Model model){

    String seid = request.getParameter("eid");

    int eid = seid == null ? -1 : Integer.parseInt(seid);

    Emp emp = empService.getEmpByEid(eid);

    model.addAttribute("emp", emp);

    return "updateEmp.jsp";
    }
    }

你可能感兴趣的:(SpringMVC实现的两种方式)