不少應用程序的問題在於處理業務數據和顯示業務數據的視圖的對象之間存在緊密耦合。一般,更新業務對象的命令都是從視圖自己發起的,使視圖對任何業務對象更改都有高度敏感性。並且,當多個視圖依賴於同一個業務對象時是沒有靈活性的。html
Spring Web MVC是一種基於Java的實現了Web MVC設計模式的請求驅動類型的輕量級Web框架,即便用了MVC架構模式的思想,將web層進行職責解耦,基於請求驅動指的就是使用請求-響應模型,框架的目的就是幫助咱們簡化開發,Spring Web MVC也是要簡化咱們平常Web開發的。前端
一、清晰的角色劃分:前端控制器(DispatcherServlet)、請求處處理器映射(HandlerMapping)、處理器適配器(HandlerAdapter)、視圖解析器(ViewResolver)、處理器或頁面控制器(Controller)、驗證器( Validator)、命令對象(Command 請求參數綁定到的對象就叫命令對象)、表單對象(Form Object 提供給表單展現和提交到的對象就叫表單對象)。java
二、分工明確,並且擴展點至關靈活,能夠很容易擴展,雖然幾乎不須要;web
三、因爲命令對象就是一個POJO,無需繼承框架特定API,能夠使用命令對象直接做爲業務對象;spring
四、和Spring 其餘框架無縫集成,是其它Web框架所不具有的;編程
五、可適配,經過HandlerAdapter能夠支持任意的類做爲處理器;設計模式
六、可定製性,HandlerMapping、ViewResolver等可以很是簡單的定製;spring-mvc
七、功能強大的數據驗證、格式化、綁定機制;架構
八、利用Spring提供的Mock對象可以很是簡單的進行Web層單元測試;mvc
九、本地化、主題的解析的支持,使咱們更容易進行國際化和主題的切換。
十、強大的JSP標籤庫,使JSP編寫更容易。
………………還有好比RESTful風格的支持、簡單的文件上傳、約定大於配置的契約式編程支持、基於註解的零配置支持等等。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name> <filter> <filter-name>characterFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 加載springMVC的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置映射處理器 BeanNameUrlHandlerMapping 默認--> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/index.do">helloController</prop> <prop key="/qwe.do">helloController</prop> </props> </property> </bean> <bean id="helloController" name="/hello.do" class="cn.bdqn.controller.HelloController"></bean> <!-- 配置處理器適配器 默認--> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean> <!-- 視圖解析器 InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/jsp/"></property> <!-- 後綴 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
package cn.bdqn.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ //返回一個視圖 public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("mesg", "教育改變生活"); // application.setAtr... mv.setViewName("hello"); return mv; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <div>${mesg }</div> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" 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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="cn.bdqn"></context:component-scan> <!-- 註解一鍵配置 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 視圖解析器 InternalResourceViewResolver --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/jsp/"></property> <!-- 後綴 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
package cn.bdqn.pojo; public class User { private Integer age; //年齡 private String uname; //姓名 private String location; //地區 private String sex; //性別 public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "User [age=" + age + ", uname=" + uname + ", location=" + location + ", sex=" + sex + "]"; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/form/rePojo.do" method="post"> 姓名:<input name="uname"/><br/> 年齡:<input name="age" /><Br/> 地址:<select name="location"> <option>北京</option> <option>合肥</option> </select><br/> 性別:<input type="radio" name="sex" value="男"/>男 <input type="radio" name="sex" value="女"/>女<br/> <input type="submit" value="提交"/> </form> </body> </html>
package cn.bdqn.controller; import java.util.Map; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.bdqn.pojo.User; @Controller @Scope("prototype") @RequestMapping("/form") public class FormController { @RequestMapping(value="/rePojo.do") public String rePojo(User user,Model model){ //從頁面獲取user參數 , 添加Model參數 model.addAttribute(user); //使用此方法將參數傳入頁面 System.out.println(user); return "index"; //發送到index.jsp頁面
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> 姓名----${user.uname}<br/> 年齡----${user.age}<br/> 地區----${user.location}<br/> 性別----${user.sex}<br/> </body> </html>