SpringMVC(二) SpringMVC Hello World
阿新 • • 發佈:2018-12-01
準備條件:
STS(集成了Spring相關工具的Eclipse)
Spring軟體包 spring-framework-4.3.3.RELEASE-dist.zip。
步驟:
- 加入jar包。
Eclipse中新建一個動態的web工程。選擇Tomcat 7.0,在WebContent-->WEB-INF-->lib目錄下新增以下jar包。
spring-aop-4.3.3.RELEASE.jar
spring-beans-4.3.3.RELEASE.jar
spring-context-4.3.3.RELEASE.jar
spring-core-4.3.3.RELEASE.jar
pring-expression-4.3.3.RELEASE.jar
spring-web-4.3.3.RELEASE.jar
spring-webmvc-4.3.3.RELEASE.jar
另外還需要commons-logging-1.2.jar檔案。
2. 在web.xml中新增dispatcherServlet相關內容(使用快捷鍵Alt+/,在自動彈出的下拉條中,選擇#dispatcherservlet)。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>HelloWorld</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- The front controller of this Spring Web application, responsible for handling all application requests --> <servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置dispathcerservlet的一個初始化引數,設定配置檔案的名稱和位置 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3. 加入SpringMVC的配置檔案。在src目錄下,參照以下關鍵步驟建立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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <!-- 配置自動掃描包 --> <context:component-scan base-package="com.tiekui.springmvc.*"></context:component-scan> <!-- 配置檢視解析器:如何把handler方法返回給檢視 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.編寫請求的處理器。參考如下程式碼:
package com.tiekui.springmvc.handlers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { @RequestMapping("/helloworld") public String hello(){ System.out.println("hello world"); return "success"; } }
5. 編寫檢視。在WebContent目錄下建立index.jsp檔案,參考如下程式碼:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <a href="helloworld">Hello World</a> </body> </html>
在/WEB-INF/views目錄下建立success.jsp。內容參考如下程式碼:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h1>Hello SpringMVC View</h1> </body> </html>