SpringMVC框架學習之框架初體驗(一)
目錄
前言
在之前Web項目開發過程中,大家往往會採用SSH結構來開發一套web項目,
但是,由於,struts漏洞問題的出現,這種架構顯得不是那麼的好了。
所以,目前,大多數人回採用,SpringMVC+MyBatis或SpringMVC+Hibernate
來開發項目以取代近乎被淘汰的SSH架構開發模式。
框架簡介
SpringMVC是Spring框架中的一個很重要的模塊。它實現了MVC結構
。使整個符合MVC結構的web項目的開發更加方便快捷。
Spring Web MVC的核心組件
Spring Web MVC提供了M、V和C相關的主要實現組件,具體如下:
- DispatcherServlet(控制器,請求入口)
- HandlerMapping(控制器,請求派發)
- Controller(控制器,請求處理流程)
- ModelAndView(模型,封裝業務處理結果和視圖)
- ViewResolver(視圖,視圖顯示器)
Spring Web MVC的處理流程
- 瀏覽器向Spring發出請求,請求交給前端控制器DispatcherServlet處理。
- 控制器通過HandlerMapping找到相應的Controller組件處理請求
- 執行Controller組件約定方法請求,在約定方法調用模型組件完成
業務處理。約定方法可以返回一個ModelAndView對象,封裝了處理結
果數據和視圖名稱信息。 - 控制器接受ModelAndView之後,調用ViewResolver組件,定位View(JSP)
並傳遞數據信息,生成響應界面結果。
創建一個第一個Springmvc框架。
步驟:
1創建web項目
2導入相應的jar包,其中核心jar包如下:
- spring-web、
- spring-webmvc、
- spring-core 、
- spring-context、
- spring-beans、
- commons-logging、
- spring-expression、
3創建Spring Web MVC 的XML配置文件,文件名設置爲springmvc.xml(其實就是spring的配置文件)。
springmvc.xml
<?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:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
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-3.1.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd"
default-autowire="byName">
</beans>
4將前端控制器組件DispatherServlet配置到web.xml文件中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>GIMS_SPRING1</display-name>
<servlet>
<!--配置關鍵代碼-->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5書寫Controller控制器
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
/**
* 控制器
* @author SHENGYUNT
*
*/
public class TestController extends AbstractController {
/**
*從控制窗體接受username和password並打印。
*/
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+":"+password);
//此處的index是試圖解析器InternalResourceViewResolver前綴後綴之間的值
return new ModelAndView("index");
}
}
6在springmvc.xml中配置TestController控制器
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 配置控制器 -->
<bean name="/gradController.do" class="com.gims.controller.GraduateController"/>
<!-- 配置視圖解析器,prefix爲前綴,suffix爲後綴 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/" />
<property name="suffix" value=".jsp"/>
</bean>
</beans>
7編寫index.jsp頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="Content-Type" content="text/http;charset=utf8">
</head>
<body>
This is my JSP page. <br>
<form action="gradController.do">
<table cellpadding="1" bgcolor="#aaaaaaaa" >
<tr >
<td width="100" >username</td>
<td width="100" ><input name="username" /> </td>
</tr>
<tr>
<td width="100" >password</td>
<td width="100" ><input name="password" /></td>
</tr>
<tr><td width="100"><input type="submit" name="login" value="submit" /></td></tr>
</table>
</form>
</body>
</html>
綜上,爲整個SpringMVC項目的環境搭建過程。