Spring MVC 环境搭建与配置

Spring MVC 环境搭建与配置

首先下载 Spring,我们使用的是 4.1.16 版本:

http://repo.spring.io/libs-release-local/org/springframework/spring/4.1.6.RELEASE/

然后还要下载日志包:

http://commons.apache.org/proper/commons-logging/download_logging.cgi

使用 Eclipse JavaEE 版本即可,相关依赖包已整理分享。

项目创建与依赖引入

打开 Eclipse,创建一个 Dynamic Web Project 项目,引入以下 jar 包:

  • commons-logging-1.2.jar
  • spring-aop-4.1.6.RELEASE.jar
  • spring-beans-4.1.6.RELEASE.jar
  • spring-context-4.1.6.RELEASE.jar
  • spring-core-4.1.6.RELEASE.jar
  • spring-expression-4.1.6.RELEASE.jar
  • spring-web-4.1.6.RELEASE.jar
  • spring-webmvc-4.1.6.RELEASE.jar

并将这些包添加到 Deployment 中。

web.xml 配置

修改 web.xml 文件,主要配置分发 Servlet,该 Servlet 会读取一个 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" id="WebApp_ID" version="3.0">
  <display-name>SpringDemo</display-name>
  <!-- configure the setting of springmvc DispatcherServlet and configure the mapping -->
  <servlet>
      <servlet-name>spm</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet.xml</param-value>
        </init-param>
        <!-- <load-on-startup>1</load-on-startup> -->
  </servlet>

  <servlet-mapping>
      <servlet-name>spm</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

  <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>
</web-app>Code language: HTML, XML (xml)

servlet.xml 配置

src 目录下添加 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/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.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

    <!-- scan the package and the sub package -->
    <context:component-scan base-package="cn.bamn.spring"/>

    <!-- don't handle the static resource -->
    <mvc:default-servlet-handler />

    <!-- if you use annotation you must configure following setting -->
    <mvc:annotation-driven />

    <!-- configure the InternalResourceViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
            id="internalResourceViewResolver">
        <!-- 前缀 -->
        <property name="prefix" value="/WEB-INF/jsp/" />
        <!-- 后缀 -->
        <property name="suffix" value=".jsp" />
    </bean>
</beans>Code language: HTML, XML (xml)

控制器与视图

添加控制器和 Action:

package cn.bamn.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/home")
public class HomeController {

    @RequestMapping("/index")
    public String hello(){
        return "index";
    }
}Code language: CSS (css)

return 后面的字符串即为视图的 JSP 文件名。

WEB-INF/jsp/ 目录下创建 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
   你好吗
</body>
</html>Code language: HTML, XML (xml)

访问地址:http://localhost:8080/SpringDemo/home/index

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注