使用Maven工程建立一個SpringMVC框架入門
阿新 • • 發佈:2018-12-17
前提:
首先建立一個maven工程的web專案,把最基本的web環境搭建好。包括包結構… 開始編寫SpringMVC入門案例
- 在pom.xml檔案中 匯入工程需要的座標
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.lxh</groupId> <artifactId>day09_springMVC_helloword</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <!--版本鎖定--> <properties> <maven.compile.source>1.8</maven.compile.source> <maven.compile.target>1.8</maven.compile.target> <spring.version>5.0.2.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.0</version> <scope>provided</scope> </dependency> </dependencies> </project>
- 在web.xml文加中配置SpringMVC所需的控制器元件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--配置springMVC前端控制器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置Servlet的初始化引數,讀取springmvc的配置檔案,建立spring容器 --> <init-param> <param-name>contextConfigLocation</param-name> <!--這裡為springmvc配置檔案的絕對路徑--> <param-value>classpath:springmvc.xml</param-value> </init-param> <!-- 配置servlet啟動時載入物件的時機 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--配置post請求方式下的中文亂碼過濾器(springMVC提供的過濾器類)--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
- 在sources目錄下建立SpringMVC的配置檔案,檔名可以隨便取。例如springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--開啟註解掃描,以及需要掃描的包--> <context:component-scan base-package="cn.lxh"></context:component-scan> <!--配置springMVC註解支援,當有自定義轉換器的時候,就需要加上conversion-service屬性--> <mvc:annotation-driven ></mvc:annotation-driven> <!--配置springMVC檢視解析器,也就是伺服器處理業務後跳轉的位置以及檢視的字尾名--> <!-- 字首prefix + 方法返回值 + 字尾suffix 組合為跳轉的路徑--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
- 在配置的跳轉路徑下WEB-INF/pages/下建立一個jsp頁面。這裡取名為success.jsp,用作請求成功之後跳轉的頁面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
入門成功!!!
</body>
</html>
- 編寫傳送請求的頁面 這裡以index.jsp為例,裡面包含一個a標籤,用於點選後傳送請求到伺服器
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h3>入門案例</h3>
<a href="${pageContext.request.contextPath}/hello">點選入門</a>
</body>
</html>
- 編寫SpringMVC的控制類
package cn.lxh.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller //指定該類為springMVC的控制器類
public class HelloController {
//訪問路徑,path/value可以省略
@RequestMapping(path = "/hello")
public String hello(){
System.out.println("Hello world !");
//返回值為跳轉路徑的頁面名(不要字尾),這裡相當於是跳轉到WEB-INF/pages/success.jsp頁面
return "success";
}
}
以上已經把程式碼全部編寫完畢,下面是釋出和訪問
- 將專案釋出到tomcat 然後點選Deployment配置
- 訪問 執行tomcat伺服器,顯示index.jsp頁面後點擊a標籤。如圖顯示則入門案例成功 以上就是SpringMVC的入門案例