Spring MVC簡介
Spring MVC框架是一個MVC模式框架,通過實現Model-View-Controller模式很好地將數據、業務與展現相分離,類似於Struts框架。Spring MVC的設計是圍繞DispatcherServlet展開的,通過核心的DispatcherServlet將請求分發到指定的Handler。通過指定配置處理請求並實現邏輯視圖與實際視圖資源間的映射。
Spring MVC版HelloWorld
小弟新學Spring MVC,以下是學習中的一些筆記,大蝦請直接繞道。
開發環境:
- 系統:Windows7
- IDE:Myeclipse2014
- 服務器:Tomcat7.0
- Spring版本:4.1.6
開發步驟:
新建Web工程
Target runtime選擇已配置好的Tomcat服務器
選擇"Next"
勾選"Generate web.xml"選項,自動生成web.xml文件
添加Spring MVC所需jar包
配置web.xml文件
配置web.xml文件,添加DispatcherServlet請求分派器。添加Spring MVC的上下文配置路徑爲classpath下的spring-mvc.xml文件。配置Spring MVC處理所有*do結尾的請求資源。"load-on-startup”參數指定該servlet(SpringMVC)在容器啓動的時候是否被加載(實例化並調用其init()方法)。
它的值必須是一個整數,表示servlet應該被載入的順序。
- 當值爲0或者大於0時,表示在容器啓動的時候就被加載並初始化。
- 當值小於0或者沒有指定時,表示該servlet被選擇調用的時候纔回去加載。
- 正數的值越小,該servlet的優先級越高,應用啓動就越先加載。
- 當值相同時,容器會自主選擇相應servlet的加載順序。
<?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"
id="WebApp_ID" version="3.0">
<display-name>SpringMvc</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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:spring-mvc.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>
</web-app>
配置spring-mvc.xml文件
主要是配置一個視圖解析器,前綴爲/WEB-INF/jsp/,文件格式爲jsp,這樣servlet中如返回String爲"HelloWorld",則直接相當於返回/WEB-INF/jsp/路徑下的HelloWorld.jsp文件。再添加一個component-scan屬性,相當於識別該包下的所有註解。
<?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: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">
<context:component-scan base-package="com.controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
- 在WEB-INF路徑下新建上述步驟指定的路徑資源
編寫HelloWorldController控制器
新建一個Class
編寫具體實現
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorldController {
@RequestMapping("/helloWorld")
public String helloWorld(Model model){
model.addAttribute("message", "問候SpringMvc");
return "helloWorld";
}
}
使用@Controller註解的形式聲明一個控制器,使用@RequestMapping("/helloWorld")聲明該控制器下的"helloWorld"方法的請求路徑爲:"helloWorld.do",該方法中傳入了一個Model模型對象(model對象中設置了一個要返回的message屬性,其值爲"問候SpringMvc"),方法直接返回一個String字符串"helloWorld",表示返回上面步驟中的資源“/WEB-INF/jsp/helloWorld.jsp”。這樣可以直接在helloWorld.jsp文件中通過EL表達式取值的方式輸出該message屬性值。
**helloWorld.jsp文件如下,直接輸出message屬性值**
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'helloWorld.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
${message }
</body>
</html>
前臺通過index.jsp鏈接訪問,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="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="helloWorld.do" >問候SpringMvc</a>
</body>
</html>
通過直接鏈接的方式予以訪問,實際效果如下:
完整的步驟就是這樣,後續將會陸續上傳Spring MVC的後續筆記,共同勉勵,一起進步!
ps:新人寫博客,從javase到Spring完全自學,有錯誤還望老鳥及時指出,共同進步,小弟不勝感激!