zoukankan      html  css  js  c++  java
  • Spring MVC笔记(四) 访问静态页面

    本例通过<mvc:resources>标签访问一个静态或动态页面。

    首先还是创建一个web工程,并引入相关jar包:

    创建控制器类,WebController.java

     1 package com.young.saticpage.controller;
     2 
     3 import org.springframework.stereotype.Controller;
     4 import org.springframework.web.bind.annotation.RequestMapping;
     5 import org.springframework.web.bind.annotation.RequestMethod;
     6 
     7 @Controller
     8 public class WebController {
     9   @RequestMapping(value = "/index", method = RequestMethod.GET)
    10   public String index() {
    11     return "index";
    12   }
    13 
    14   public String redirect() {
    15     return "redirect:/pages/final.html";
    16   }
    17 }

    staticPage-servelet.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="
       http://www.springframework.org/schema/beans     
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    
        <context:component-scan base-package="com.young" />
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
        <!-- <mvc:default-servlet-handler /> -->
        <mvc:annotation-driven/>
        <mvc:resources mapping="/pages/**" location="/WEB-INF/pages/" />
    </beans>

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

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

    <%@ 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="/staticPage/staticPages">
            <table>
                <tr>
                    <td><input type="submit" value="获取HTML页面" /></td>
                </tr>
            </table>
        </form:form>
    </body>
    </html>

    启动Tomcat服务器,访问http://localhost:8080/staticPage/index,如下所示:

  • 相关阅读:
    Mybaits-plus实战(三)
    Mybaits-plus实战(二)
    Mybaits-plus实战(一)
    面向对象的理解
    如何理解算法
    将yyyyMMdd HH:mm:ss格式的时间转换成时间类型
    泛型/dynamic/object作用
    成功之道
    ASP.NET注意事项
    Razor引擎总结
  • 原文地址:https://www.cnblogs.com/weyoung1987/p/7979067.html
Copyright © 2011-2022 走看看