SpringMvc是在web應用開發中,用於web層的框架,與ssh中Struts2充當相同的職位。
-
搭建環境:
- 導包:SpringMvc與Spring渾然一體,SpringMvc的jar包也就是Spring的jar包。如果說獨屬於SpringMvc的jar包,我猜大概就兩個吧:
- 書寫SpringMvc配置文件(springmvc.xml):可以看到約束和Spring的配置文件約束一樣。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!-- 以註解開發方式,配置註解掃描路徑 --> <context:component-scan base-package="hh.controller"></context:component-scan> </beans>
- 配置SpringMvc前端控制器——(在項目的核心配置文件web.xml中):
<!-- 配置SpringMvc前端控制器 --> <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> <!-- 配置SpringMvc控制器可控制的請求 --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- /*:控制所有 *.adc:控制以.abc結尾的請求 /:控制所有(除.jsp) --> <url-pattern>*.action</url-pattern> </servlet-mapping>
基本配置就這些,接下來進行簡單測試。
- 頁面(hello.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> <h1>Hello</h1> </body> </html>
- 方法:
// 註解方式,將此類交給Spring容器 @Controller public class SpringMvcTest { // 註解方式,配置此方法的請求映射 @RequestMapping("/hello.action") public ModelAndView test() { ModelAndView mav=new ModelAndView(); mav.setViewName("/WEB-INF/jsp/hello.jsp"); return mav; } }
- 執行結果:
可以看到請求成功。
ModelAndView:是SpringMvc中用於設置回執參數和跳轉路徑的對象。相同作用的還有Model和ModelMap,這裏重點學習ModelAndView。
接下來寫一個傳參的方法來測試。
- 頁面(itemList.jsp):
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <!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>查詢商品列表</title> </head> <body> <form action="${pageContext.request.contextPath }/item/queryitem.action" method="post"> 查詢條件: <table width="100%" border=1> <tr> <td><input type="submit" value="查詢" /></td> </tr> </table> 商品列表: <table width="100%" border=1> <tr> <td>商品名稱</td> <td>商品價格</td> <td>生產日期</td> <td>商品描述</td> <td>操作</td> </tr> <c:forEach items="${itemList }" var="item"> <tr> <td>${item.name }</td> <td>${item.price }</td> <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss" /></td> <td>${item.detail }</td> <td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </form> </body> </html>
- 方法:
@RequestMapping("/itemlist.action") public ModelAndView testParameter() { List<Item> itemlist=new ArrayList<Item>(); itemlist.add(new Item(1,"筆記本",4399.00f,"好",new Date())); itemlist.add(new Item(2,"筆記本",4399.00f,"好",new Date())); itemlist.add(new Item(3,"筆記本",4399.00f,"好",new Date())); ModelAndView mav=new ModelAndView(); mav.addObject("itemList",itemlist); mav.setViewName("/WEB-INF/jsp/itemList.jsp"); return mav; }
- 執行結果:
-
以上,就是SpringMVC最基本的開發方式,可以看到比Struts2簡潔不少。