学习参考自易百教程
1、程序目录如下:
2、web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>springMVC_hello</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
该配置中<url-pattern>/</url-pattern>拦截了所有路径请求,也包括了所有static静态资源,javascript、css、img等,当需要访问静态的资源文件时,需要在[servlet-name]-servlet.xml文件中添加配置<mvc: resources location="" mapping="">完成资源的获取。
<mvc:resources mapping="/javascript/**" location="/static_resources/javascript/"/>
<mvc:resources mapping="/styles/**" location="/static_resources/css/"/>
<mvc:resources mapping="/images/**" location="/static_resources/images/"/>
3、dispatcher-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">
<context:component-scan base-package="com.controller" />
<mvc:default-servlet-handler />
<mvc:annotation-driven />
<mvc:resources location="/html/" mapping="/html/**"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<!-- <property name="prefix" value="/jsp/" /> -->
<!-- 定义视图后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
4、controller
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class LinkHTMLController {
@RequestMapping(value="/index",method=RequestMethod.GET)
public String page(){
return "index"; //处理localhost:8080/springMVC_linkToHTML/index的路径请求,并返回index.jsp显示
}
@RequestMapping(value="/toHtml",method=RequestMethod.POST)
public String toHtml(){
System.out.println("toHtml");
return "redirect:/html/MyHtml.html";//当前return路径localhost:8080/springMVC_linkToHTML
}
}
5、index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>link to a html</title>
</head>
<body>
<h2>Loading a html page.</h2>
<form:form action="/springMVC_linkToHTML/toHtml">
<button type="submit">get a html</button>
</form:form>
</body>
</html>
6、MyHtml.html
<!DOCTYPE html>
<html>
<head>
<title>MyHtml.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<h2 style="text-align:center"> my HTML page. </h2>
</body>
</html>
结果: