Spring Mvc第一個web程式
介紹
Spring Mvc
是一種輕量級的web開發架構,有了
Spring Mvc
我們隻需要關注在我們的邏輯代碼和業務代碼上就可以了,比傳統的重量級web開發,節省了大量的時間。
比較正規的介紹
Spring Web MVC是一種基于Java的實作了Web MVC設計模式的請求驅動類型的輕量級Web架構,即使用了MVC架構模式的思想,将web層進行職責解耦,基于請求驅動指的就是使用請求-響應模型,架構的目的就是幫助我們簡化開發,Spring Web MVC也是要簡化我們日常Web開發的。
SpringMVC處理流程
圖檔來自網際網路
入門程式
第一步:建立Maven Web項目
這裡就不示範了,不會的可以去看這篇文章:
https://www.cnblogs.com/wen1027/p/6709810.html
第二步:引入Spring MVC包
在
pom.xml
檔案中,添加一下代碼,引入Spring mvc依賴包
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
第三步:配置 web.xml
web.xml
添加自定義的
Servlet
和控制器中的xml檔案:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
添加
Spring
中的監聽器:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
添加
Spring
中的路由
Servlet
:
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
整個web.xml的内容
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
第四步:
在第三步,你聲明
Spring
中的
servlet
傳入的參數有個檔案路徑,當然你也可以指定其他路徑,但是必須確定該路徑可以被找到
上面的檔案路徑:
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
在聲明的路徑下建立該檔案
servlet-context.xml
,将下面模闆拷貝下去:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
</beans:beans>
先添加資源引用轉換聲明:
<resources mapping="/resources/**" location="/resources/" />
啟動注解驅動:
<annotation-driven />
聲明
View
試圖的檔案及路徑
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
然後建立
/WEB-INF/views/
路徑
聲明掃描的注解包:
<context:component-scan base-package="cn.wenhaha.huoju" />
第三步還指定了一個/WEB-INF/spring/root-context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
第四步:建立控制層
建立一個HomeController類:
public class HomeController {
public String home(Locale locale, Model model) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
然後用
@Controller
注解聲明這是一個
Controller
層,然後用
@RequestMapping
注解聲明
home
方法為路由。
@Controller
public class HomeController {
@RequestMapping("/hi")
public String home(Locale locale, Model model) {
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);
model.addAttribute("serverTime", formattedDate );
return "home";
}
}
最後在/WEB-INF/views/路徑下,建立
home.jsp
檔案
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>
Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>