SpringMVC 专题
您的位置:java > SpringMVC专题 > Spring MVC静态页面
Spring MVC静态页面
作者:--    发布时间:2019-11-19

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

  1. 创建一个简单的动态web项目:staticpages,并在 src 目录下创建一个 com.h3.springmvc 包。
  2. com.h3.springmvc包下创建一个java类webcontroller
  3. jsp子文件夹下创建一个静态文件final.html
  4. webcontent/web-inf文件夹下创建一个spring配置文件 staticpages-servlet.xml,如下所述。
  5. 最后一步是创建所有源和配置文件的内容并运行应用程序,如下所述。

完整的项目文件结构如下所示 -

webcontroller.java 的代码如下所示 -

package com.h3.springmvc;

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

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"
          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="springmvc"/>

      <mvc:resources mapping="/jsp/**" location="/web-inf/jsp/"/>
      <mvc:annotation-driven/>


</beans>

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

下面是spring视图文件web-inf/jsp/index.jsp的内容。这将是一个登录页面,此页面将发送一个请求访问staticpage服务方法,该方法将此请求重定向到web-inf/pages文件夹中的静态页面。

index.jsp 页面的代码如下 -

<%@ page contenttype="text/html; charset=utf-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>spring landing page</title>
</head>
<body>
<h2>spring landing page</h2>
<p>点击下面的按钮获得一个简单的html页面</p>
<form:form method="get" action="/staticpages/staticpage">
<table>
    <tr>
    <td>
    <input type="submit" value="获取html页面"/>
    </td>
    </tr>
</table>  
</form:form>
</body>
</html>

final.html 的完整代码如下 -

<html>
<head>
    <title>spring static page</title>
</head>
<body>

<h2>a simple html page</h2>

</body>
</html>

完成创建源和配置文件后,导出应用程序。右键单击应用程序,并使用导出> war文件选项,并将文件保存为helloweb.war 在tomcat的webapps文件夹中。

现在启动tomcat服务器,现在尝试访问url => http://localhost:8080/helloweb/index 。 如果spring web应用程序没有问题,应该看到以下结果:

单击“获取html页面”按钮访问staticpage服务方法中提到的静态页面。如果spring web应用程序没有问题,应该看到以下结果:


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册