一、配置Spring MVC
1.導入jar
spring-framework-4.x.x.RELEASE-dist.zip壓縮文件
- commons-logging:http://commons.apache.org/
解壓之后將jar放入
2.在web.xml中配置DispatcherServlet
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <!-- 配置DispatcherServlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 如果值為正整數或者0時,表示容器在應用啟動時就加載並初始化這個servlet,值越小,servlet的優先級越高,就越先被加載 --> <load-on-startup>1</load-on-startup> </servlet> <!-- 配置會把所有的請求都會進行攔截,交給spring去處理。而spring所有請求的URL都是在controller中使用注解@RequestMapping標明,所以這樣的情況下訪問靜態資源是訪問不到的。 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.加入spring mvc 的配置文件spring-servlet.xml
注意:框架默認讀取 {servlet-name}-servlet.xml 是配置文件,所以我們在web.xml中寫了
<servlet-name>spring</servlet-name>
那么我們寫的配置文件就是 spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd" default-lazy-init="false"> <!-- 配置自動掃描包 --> <context:component-scan base-package="com.ttz.controller"></context:component-scan> <!-- 配置視圖解析器:如何把handler方法返回值解析為實際的物理視圖 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
4.編寫處理請求的處理器controller,並標記為處理器
package com.ttz.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class HelloWorld { @RequestMapping("/helloworld") public String hello() { System.out.println("hello world"); return "success"; } }
5.編寫視圖(頁面)
hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <a href="helloworld">helloworld</a> </body> </html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>成功!</h1> </body> </html>
二、問題
啟動tomcat可以成功打開hello.jsp,點擊后跳轉到success.jsp。
但是編寫index.html,和success.html,均無法打開
三月 24, 2019 10:02:17 下午 org.springframework.web.servlet.PageNotFound noHandlerFound
警告: No mapping found for HTTP request with URI [/Springmvc-01/index.html] in DispatcherServlet with name 'spring'
三、解決
1.原因分析
原因參考:https://blog.csdn.net/jdjdndhj/article/details/54907891
<!-- 配置會把所有的請求都會進行攔截,交給spring去處理。而spring所有請求的URL都是在controller中使用注解@RequestMapping標明,所以這樣的情況下訪問靜態資源是訪問不到的。 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
從配置上分析,如此配置會把所有的請求都會進行攔截,交給spring去處理。而spring所有請求的URL都是在controller中使用注解@RequestMapping標明,所以這樣的情況下訪問靜態資源是訪問不到的。
spring將index.html頁面攔截成請求,而在接口層HelloWorld中沒有該請求url對應的處理方法。
???不明白為什么jsp可以
2.解決辦法
- 修改web.xml
<!-- 配置會把所有的請求都會進行攔截,交給spring去處理。而spring所有請求的URL都是在controller中使用注解@RequestMapping標明,所以這樣的情況下訪問靜態資源是訪問不到的。 --> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/api/*</url-pattern> </servlet-mapping>
注意:不能寫 /api/ ,得寫 /api/*
- 對應修改頁面的請求
<a href="api/helloworld">helloworld</a>
配置會把 /api/* 所有的請求都會進行攔截