Spring MVC

环境搭建

以下示例显示如何使用Spring MVC Framework编写一个简单的基于Web的应用程序,它可以使用<mvc:resources>标记访问静态页面和动态页面。首先使用Intellij IDEA创建一个动态WEB项目,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序:

  • 创建一个简单的动态Web项目:StaticPages,并在 src 目录下创建一个 com.ktao.controller 包。
  • com.ktao.controller包下创建一个Java类WebController
  • 在pages子文件夹下创建一个静态文件final.html
  • web/WEB-INF文件夹下创建一个Spring配置文件 StaticPages-servlet.xml,如下所述。
  • 最后一步是创建所有源和配置文件的内容并运行应用程序,如下所述。

完整的项目文件结构如下:

Spring MVC

配置文件

web.xml

<?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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>StaticPages</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>StaticPages</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

StaticPages-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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:beans="http://www.springframework.org/schema/c"
       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="com.ktao.controller" />
<!--
    <bean >
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
-->
    <mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
    <mvc:annotation-driven/>
</beans>

<mvc:resource ...../>标记来映射静态页面。映射属性必须是指定http请求的URL模式的Ant模式。location属性必须指定一个或多个有效的资源目录位置,其中包含静态页面,包括图片,样式表,JavaScript和其他静态内容。可以使用逗号分隔的值列表指定多个资源位置。

控制器

WebController.java

package com.ktao.controller;

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

@Controller
public class WebController {
    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/staticPage",method = RequestMethod.GET)
    public String redirect(){
        return "redirect:/pages/final.html";
    }

}

视图

index.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: ktao
  Date: 17-12-2
  Time: 上午8:52
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Spring Landing Page</title>
  </head>
  <body>
    <h2>Spring Landing Page</h2>
    <p>点击下面的按钮获得一个简单的HTML页面</p>
    <form:form method="get" action="staticPage">
    <table>
      <tr>
        <td>
          <input type="submit" value="获取HTML页面"/>
        </td>
      </tr>
    </table>

    </form:form>
  </body>
</html>

final.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Spring Static Page</title>
</head>
<body>
<h2>A simple HTML page</h2>
</body>
</html>

运行结果 

Spring MVC

Spring MVC