第一步:發起請求到前端控制器(DispatcherServlet)
第二步:前端控制器請求HandlerMapping查找 Handler
可以根據xml配置、注解進行查找
第三步:處理器映射器HandlerMapping向前端控制器返回Handler
第四步:前端控制器調用處理器適配器去執行Handler
第五步:處理器適配器去執行Handler
第六步:Handler執行完成給適配器返回ModelAndView
第七步:處理器適配器向前端控制器返回ModelAndView
ModelAndView是springmvc框架的一個底層對象,包括 Model和view
第八步:前端控制器請求視圖解析器去進行視圖解析
根據邏輯視圖名解析成真正的視圖(jsp)
第九步:視圖解析器向前端控制器返回View
第十步:前端控制器進行視圖渲染
視圖渲染將模型數據(在ModelAndView對象中)填充到request域
第十一步:前端控制器向用戶響應結果
1、前端控制器DispatcherServlet(不需要程序員開發)
作用接收請求,響應結果,相當於轉發器,中央處理器。
有了DispatcherServlet減少了其它組件之間的耦合度。
2、處理器映射器HandlerMapping(不需要程序員開發)
作用:根據請求的url查找Handler
3、處理器適配器HandlerAdapter
作用:按照特定規則(HandlerAdapter要求的規則)去執行Handler
4、處理器Handler(需要程序員開發)
注意:編寫Handler時按照HandlerAdapter的要求去做,這樣適配器才可以去正確執行Handler
5、視圖解析器View resolver(不需要程序員開發)
作用:進行視圖解析,根據邏輯視圖名解析成真正的視圖(view)
6、視圖View(需要程序員開發jsp)
View是一個接口,實現類支持不同的View類型(jsp、freemarker、pdf...)
<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加載的配置文件(配置處理器映射器、適配器等等) 如果不配置contextConfigLocation,默認加載的
是/WEB-INF/servlet名稱-serlvet.xml(springmvc-servlet.xml) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
在springmvc.xml
<mvc:annotation-driven></mvc:annotation-driven>
在springmvc.xml
<!-- 視圖解析器
解析jsp解析,默認使用jstl標簽,classpath下的得有jstl的包
-->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 配置jsp路徑的前綴 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 配置jsp路徑的後綴 -->
<property name="suffix" value=".jsp"/>
</bean>
新建controller
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
//為了對url進行分類管理 ,可以在這裡定義根路徑,最終訪問url是根路徑+子路徑
//比如:商品列表:/items/queryItems.action
@RequestMapping("/items")
public class ItemsController {
@Autowired
private ItemsService itemsService;
// 商品查詢
@RequestMapping("/queryItems")
public ModelAndView queryItems(HttpServletRequest request) throws Exception {
//測試forward後request是否可以共享
System.out.println(request.getParameter("id"));
// 調用service查找 數據庫,查詢商品列表
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
// 返回ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 相當 於request的setAttribut,在jsp頁面中通過itemsList取數據
modelAndView.addObject("itemsList", itemsList);
// 指定視圖
// 下邊的路徑,如果在視圖解析器中配置jsp路徑的前綴和jsp路徑的後綴,修改為
// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
// 上邊的路徑配置可以不在程序中指定jsp路徑的前綴和jsp路徑的後綴
modelAndView.setViewName("items/itemsList");
return modelAndView;
}
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam裡邊指定request傳入參數名稱和形參進行綁定。
//通過required屬性指定參數是否必須要傳入
//通過defaultValue可以設置默認值,如果id參數沒有傳入,將默認值和形參綁定。
public String editItems(Model model,@RequestParam(value="id",required=true) Integer items_id)throws Exception {
//調用service根據商品id查詢商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
//通過形參中的model將model數據傳到頁面
//相當於modelAndView.addObject方法
model.addAttribute("itemsCustom", itemsCustom);
return "items/editItems";
}
//商品信息修改提交
@RequestMapping("/editItemsSubmit")
public String editItemsSubmit(HttpServletRequest request,Integer id,ItemsCustom itemsCustom)throws Exception {
//調用service更新商品信息,頁面需要將商品信息傳到此方法
itemsService.updateItems(id, itemsCustom);
//重定向到商品查詢列表
// return "redirect:queryItems.action";
//頁面轉發
//return "forward:queryItems.action";
return "success";
}
}
在springmvc.xml
<!-- 可以掃描controller、service、...
這裡讓掃描controller,指定controller的包
-->
<context:component-scan base-package="hstc.edu.controller"></context:component-scan>
mybatis自身配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 全局setting配置,根據需要添加 -->
<!-- 配置別名 -->
<typeAliases>
<!-- 批量掃描別名 -->
<package name="hstc.edu.po"/>
</typeAliases>
<!-- 配置mapper
由於使用spring和mybatis的整合包進行mapper掃描,這裡不需要配置了。
必須遵循:mapper.xml和mapper.java文件同名且在一個目錄
-->
<!-- <mappers>
</mappers> -->
</configuration>
applicationContext-dao.xml
配置:數據源、SqlSessionFactory、mapper掃描器
<!-- 加載db.properties文件中的內容,db.properties文件中key命名要有一定的特殊規則 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置數據源 ,dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 數據庫連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加載mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<!-- mapper掃描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 掃描包路徑,如果需要掃描多個包,中間使用半角逗號隔開 -->
<property name="basePackage" value="cn.itcast.ssm.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
在applicationContext-service.xml
<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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 商品管理的service -->
<bean id="itemsService" class="hstc.edu.service.impl.ItemsServiceImpl"/>
</beans>
<!-- 事務管理器
對mybatis操作數據庫事務控制,spring使用jdbc的事務控制類
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 數據源
dataSource在applicationContext-dao.xml中配置了
-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 傳播行為 -->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* hstc.edu.service.impl.*.*(..))"/>
</aop:config>
web.xml
<!-- 加載spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
頁面中input的name和controller的pojo形參中的屬性名稱一致,將頁面中數據綁定到pojo。
<table width="100%" border=1>
<tr>
<td>商品名稱</td>
<td><input type="text" name="name" value="${itemsCustom.name }"/></td>
</tr>
<tr>
<td>商品價格</td>
<td><input type="text" name="price" value="${itemsCustom.price }"/></td>
</tr>
<tr>
<td>商品生產日期</td>
<td><input type="text" name="createtime" value="<fmt:formatDate value="${itemsCustom.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
</tr>
<%-- <tr>
<td>商品圖片</td>
<td>
<c:if test="${item.pic !=null}">
<img src="/pic/${item.pic}" width=100 height=100/>
<br/>
</c:if>
<input type="file" name="pictureFile"/>
</td>
</tr> --%>
<tr>
<td>商品簡介</td>
<td>
<textarea rows="3" cols="30" name="detail">${itemsCustom.detail }</textarea>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="提交"/>
</td>
</tr>
</table>
public class Items {
private Integer id;
private String name;
private Float price;
private String pic;
private Date createtime;
private String detail;
1.自己寫converter
2.配置方式springmvc.xml
<!-- 自定義參數綁定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 轉換器 -->
<property name="converters">
<list>
<!-- 日期類型轉換 -->
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>