mvc java html页面_Spring MVC - 静态页面

环境搭建

以下示例显示如何使用Spring MVC Framework编写一个简单的基于Web的应用程序,它可以使用标记访问静态页面和动态页面。首先使用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,如下所述。

最后一步是创建所有源和配置文件的内容并运行应用程序,如下所述。

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

c113d24c0e3c0b3092972fd60f01fd70.png

配置文件

web.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

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">

contextConfigLocation

/WEB-INF/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

StaticPages

org.springframework.web.servlet.DispatcherServlet

1

StaticPages

/

48304ba5e6f9fe08f3fa1abda7d326ab.png

StaticPages-servlet.xml

48304ba5e6f9fe08f3fa1abda7d326ab.png

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">

48304ba5e6f9fe08f3fa1abda7d326ab.png

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

控制器

WebController.java

48304ba5e6f9fe08f3fa1abda7d326ab.png

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";

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

视图

index.jsp

48304ba5e6f9fe08f3fa1abda7d326ab.png

Created by IntelliJ IDEA.

User: ktao

Date: 17-12-2

Time: 上午8:52

To change this template use File | Settings | File Templates.

--%>

Spring Landing Page

Spring Landing Page

点击下面的按钮获得一个简单的HTML页面

48304ba5e6f9fe08f3fa1abda7d326ab.png

final.html

48304ba5e6f9fe08f3fa1abda7d326ab.png

Spring Static Page

A simple HTML page

48304ba5e6f9fe08f3fa1abda7d326ab.png

运行结果

0ccf00a4f22742b2075c0e22487241cd.png

fdad6b02d927f78bec50a3138c037f33.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值