建立項目:
File — New — Dynamic Web Project — 輸入項目名(這裡我命名為MySpringMVC-1) — Finish。
搭建環境:
右鍵項目 — MyEclipse — Project Facets[Capabilities] — Install spring Facet:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SOzAnZyNjNxYjMtdDNxFTO28WMmBDMrJDcwIWMfV2Zh1WavwVO1kjc0gGNycXc0cXd5U3NnhHdwUnd2l2LcdmblBnbhd3ZuFWa49CXt92Yu8WdsVnY5pnLjlGdhR3cvw1LcpDc0RHaiojIsJye.png)
點選Next:
取消所有勾選,點選Finish。
配置web.xml:
打開WebRoot/WEB-INF/web.xml,配置為如下形式:
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_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- 配置 DispatcherServlet -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 該servlet在項目加載時就被建立,而不是等第一次請求時才建立 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!-- 可以應答所有請求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
配置SpringMVC的配置檔案:
在WebRoot/WEB-INF目錄下建立一個Spring Bean Definition,命名為<servlet-name>-servlet.xml,在我項目的web.xml中,<servlet-name>為springDispatcherServlet,是以此處配置檔案名為springDispatcherServlet-servlet.xml:
點選Next:
勾選context和mvc,點選Finish,然後打開springDispatcherServlet-servlet.xml,配置如下:
springDispatcherServlet-servlet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 指定要自動掃描的包 -->
<context:component-scan base-package="com.MySpringMVC"></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>
其中指定com.MySpringMVC下的子包和類中的注解将會被掃描到。
添加控制器類:
建立包com.MySpringMVC.controller,并在包下建立類Hello.java,代碼如下:
Hello.java:
package com.MySpringMVC.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
/**
* 1. 使用 @RequestMapping 注解來映射請求的 URL
* 2. 傳回值會通過視圖解析器解析為實際的實體視圖, 對于 InternalResourceViewResolver 視圖解析器, 會做如下的解析:
* 通過 prefix + returnVal + 字尾 這樣的方式得到實際的實體視圖, 然會做轉發操作
*
* /WEB-INF/views/success.jsp
*
* @return
*/
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world!");
return "success";
}
}
建立頁面:
現在我們來建立頁面測試一下:
index.jsp: (其中超連結位址“helloworld”對應Hello.java中的“@RequestMapping(“/helloworld”)”)
<%@ 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%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<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">
</head>
<body>
<a href="helloworld" target="_blank" rel="external nofollow" >Hello World!</a>
</body>
</html>
/WEB-INF/views/success.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%>" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" >
<title>My JSP 'success.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">
</head>
<body>
<h4>Hello world!</h4>
</body>
</html>
運作測試:
運作項目:
點選,跳轉到了WEB-INF/views/success.jsp:
并且在控制台輸出了hello world!: