1.采用Spring MVC的好处
Dispathcher Servlet必须做如下的事情:
1.根据URI调用相应的action
2.实例化正确的控制器类
3.根据请求参数值来构造表单bean
3.调用控制器对象的相应方法
4.转向到一个视图(JSP页面)
SpringMVC提供了Dispatcher Servlet
使用基于XML的配置文件,实例化控制器
自动绑定用户输入,正确转换数据类型
可以校验用户输入,支持国际化和本地化,
支持多种视图
2.SpringMVC的DispatcherServlet
相当于调度员Servlet
配置部署:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 <servlet> 7 <servlet-name>springmvc</servlet-name> 8 <servlet-class> 9 org.springframework.web.servlet.DispatcherServlet 10 </servlet-class> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 14 <servlet-mapping> 15 <servlet-name>springmvc</servlet-name> 16 <url-pattern> /</url-pattern> 17 </servlet-mapping> 18 19 </web-app>
load-on-startup 如存在,在程序启动时加载,
如不存在,在一个请求时候加载
springMVC的配置文件,可以放在WEB-INF下
也可以放在任何地方,用init-param加载该文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 <servlet> 7 <servlet-name>springmvc</servlet-name> 8 <servlet-class> 9 org.springframework.web.servlet.DispatcherServlet 10 </servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>/WEB-INF/config/springmvc-config.xml</param-value> 14 </init-param> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 18 <servlet-mapping> 19 <servlet-name>springmvc</servlet-name> 20 <url-pattern>*.action</url-pattern> 21 </servlet-mapping> 22 </web-app>
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 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="3.0" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> 6 <servlet> 7 <servlet-name>springmvc</servlet-name> 8 <servlet-class> 9 org.springframework.web.servlet.DispatcherServlet 10 </servlet-class> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 14 <servlet-mapping> 15 <servlet-name>springmvc</servlet-name> 16 <url-pattern>/</url-pattern> 17 </servlet-mapping> 18 19 </web-app>
2.2 SpringMVC的配置文件
2个控制器类
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 6 7 <bean name="/product_input.action" class="app03a.controller.InputProductController"/> 8 <bean name="/product_save.action" class="app03a.controller.SaveProductController"/> 9 10 </beans>
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 = LogFactory 14 .getLog(InputProductController.class); 15 16 @Override 17 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 = LogFactory 17 .getLog(SaveProductController.class); 18 19 @Override 20 public ModelAndView handleRequest(HttpServletRequest request, 21 HttpServletResponse response) throws Exception { 22 logger.info("SaveProductController called"); 23 ProductForm productForm = new ProductForm(); 24 // populate action properties 25 productForm.setName(request.getParameter("name")); 26 productForm.setDescription(request.getParameter("description")); 27 productForm.setPrice(request.getParameter("price")); 28 29 // create model 30 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 Product 39 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 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Add Product Form</title> 5 <style type="text/css">@import url(css/main.css);</style> 6 </head> 7 <body> 8 9 <div id="global"> 10 <form action="product_save.action" method="post"> 11 <fieldset> 12 <legend>Add a product</legend> 13 <p> 14 <label for="name">Product Name: </label> 15 <input type="text" id="name" name="name" 16 tabindex="1"> 17 </p> 18 <p> 19 <label for="description">Description: </label> 20 <input type="text" id="description" 21 name="description" tabindex="2"> 22 </p> 23 <p> 24 <label for="price">Price: </label> 25 <input type="text" id="price" name="price" 26 tabindex="3"> 27 </p> 28 <p id="buttons"> 29 <input id="reset" type="reset" tabindex="4"> 30 <input id="submit" type="submit" tabindex="5" 31 value="Add Product"> 32 </p> 33 </fieldset> 34 </form> 35 </div> 36 </body> 37 </html>
ProductrDetails.jsp
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Save Product</title> 5 <style type="text/css">@import url(css/main.css);</style> 6 </head> 7 <body> 8 <div id="global"> 9 <h4>The product has been saved.</h4> 10 <p> 11 <h5>Details:</h5> 12 Product Name: ${product.name}<br/> 13 Description: ${product.description}<br/> 14 Price: $${product.price} 15 </p> 16 </div> 17 </body> 18 </html>
5.测试应用
1 http://localhost:8080/app03a/product_input.action
5.View Resolver
View Resolver负责配置视图解析器
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 7 8 <bean name="/product_input.action" class="app03b.controller.InputProductController"/> 9 <bean name="/product_save.action" class="app03b.controller.SaveProductController"/> 10 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 11 <property name="prefix" value="/WEB-INF/jsp/"/> 12 <property name="suffix" value=".jsp"/> 13 </bean> 14 </beans>
视图解析器配置了前缀和后缀两个属性:
view路径可以缩短,仅需提供myPage,无需在写/WEB-INF/jsp/myPage.jsp