1.采用Spring MVC的好处
Dispathcher Servlet必须做如下的事情:
1.根据URI调用相应的action
2.实例化正确的控制器类
3.根据请求参数值来构造表单bean
3.调用控制器对象的相应方法
4.转向到一个视图(JSP页面)
SpringMVC提供了Dispatcher Servlet
使用基于XML的配置文件,实例化控制器
自动绑定用户输入,正确转换数据类型
可以校验用户输入,支持国际化和本地化,
支持多种视图
2.SpringMVC的DispatcherServlet
相当于调度员Servlet
配置部署:
1 26 7 13 14springmvc 89 org.springframework.web.servlet.DispatcherServlet10 111 1215 18 19springmvc 16/ 17
load-on-startup 如存在,在程序启动时加载, 如不存在,在一个请求时候加载 springMVC的配置文件,可以放在WEB-INF下 也可以放在任何地方,用init-param加载该文件
1 26 7 17 18springmvc 89 org.springframework.web.servlet.DispatcherServlet10 1112 15contextConfigLocation 13/WEB-INF/config/springmvc-config.xml 141 1619 22springmvc 20*.action 21
3.Controller接口(传统风格控制器,要实现Controller接口) 2.5以前开发控制的唯一方法是org.springframework.web.servlet.mvc.Controller接口 只有一个方法handleRequest,而他的实现类只能处理一个单一动作Action 1 ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) 而基于注解的控制器可以控制多个动作,无需实现任何接口 4.SpringMVC第一个应用 1.目录结构 spring-webmvc-x.y.z.jar包括了 DispatcherServlet
2.部署描述符文件和SpringMVC配置文件
2.1 部署文件
没有init-param,所以放在WEB-INF下
<url-pattern>/</url-pattern> 将所有的URL映射到该Servlet上
1 26 7 13 14springmvc 89 org.springframework.web.servlet.DispatcherServlet10 111 1215 18 19springmvc 16/ 17
2.2 SpringMVC的配置文件
2个控制器类
1 26 7 8 9 10
3.Controller
1 package app03a.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.springframework.web.servlet.ModelAndView; 9 import org.springframework.web.servlet.mvc.Controller;10 11 public class InputProductController implements Controller {12 13 private static final Log logger = LogFactory14 .getLog(InputProductController.class);15 16 @Override17 public ModelAndView handleRequest(HttpServletRequest request,18 HttpServletResponse response) throws Exception {19 logger.info("InputProductController called");20 return new ModelAndView("/WEB-INF/jsp/ProductForm.jsp");21 }22 23 }
1 package app03a.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.apache.commons.logging.Log; 7 import org.apache.commons.logging.LogFactory; 8 import org.springframework.web.servlet.ModelAndView; 9 import org.springframework.web.servlet.mvc.Controller;10 11 import app03a.domain.Product;12 import app03a.form.ProductForm;13 14 public class SaveProductController implements Controller {15 16 private static final Log logger = LogFactory17 .getLog(SaveProductController.class);18 19 @Override20 public ModelAndView handleRequest(HttpServletRequest request,21 HttpServletResponse response) throws Exception {22 logger.info("SaveProductController called");23 ProductForm productForm = new ProductForm();24 // populate action properties25 productForm.setName(request.getParameter("name"));26 productForm.setDescription(request.getParameter("description"));27 productForm.setPrice(request.getParameter("price"));28 29 // create model30 Product product = new Product();31 product.setName(productForm.getName());32 product.setDescription(productForm.getDescription());33 try {34 product.setPrice(Float.parseFloat(productForm.getPrice()));35 } catch (NumberFormatException e) {36 }37 38 // insert code to save Product39 40 return new ModelAndView("/WEB-INF/jsp/ProductDetails.jsp", "product",41 product);//路径,模型名称,模型42 }43 44 }
1 package app03a.form; 2 3 public class ProductForm { 4 private String name; 5 private String description; 6 private String price; 7 8 public String getName() { 9 return name;10 }11 public void setName(String name) {12 this.name = name;13 }14 public String getDescription() {15 return description;16 }17 public void setDescription(String description) {18 this.description = description;19 }20 public String getPrice() {21 return price;22 }23 public void setPrice(String price) {24 this.price = price;25 }26 }
1 package app03a.domain; 2 import java.io.Serializable; 3 4 public class Product implements Serializable { 5 private static final long serialVersionUID = 748392348L; 6 private String name; 7 private String description; 8 private float price; 9 10 public String getName() {11 return name;12 }13 public void setName(String name) {14 this.name = name;15 }16 public String getDescription() {17 return description;18 }19 public void setDescription(String description) {20 this.description = description;21 }22 public float getPrice() {23 return price;24 }25 public void setPrice(float price) {26 this.price = price;27 }28 }
4.View
ProductForm.jsp
1 2 3 4Add Product Form 5 6 7 8 910 3536 37
ProductrDetails.jsp
1 2 3 4Save Product 5 6 7 8917 18The product has been saved.
1011
Details:
12 Product Name: ${product.name} 13 Description: ${product.description} 14 Price: $${product.price}15 16
5.测试应用
1 http://localhost:8080/app03a/product_input.action
5.View Resolver
View Resolver负责配置视图解析器
1 2 37 8 9 10 11 1412 13
视图解析器配置了前缀和后缀两个属性:
view路径可以缩短,仅需提供myPage,无需在写/WEB-INF/jsp/myPage.jsp