问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面
2024-10-18 10:23:45
一、配置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/* 所有的请求都会进行拦截
最新文章
- [zz]谱聚类
- 前端优化-Img与background
- 【转】虚拟机VMware3种网络模式(桥接、nat、Host-only)的工作原理
- OpenSessionInview
- mysql 设置可以外部访问
- web.config中配置页面出错后跳转指定错误页面
- 【leetcode】Minimum Window Substring (hard) ★
- 初识轻量级Java开源框架 --- Spring
- Ggoogle Protocol Buffer的使用 (基于C++语言)
- 利用PPT的WebBroswer控件助力系统汇报演示
- 文件IO一些注意的地方
- Docker基本命令
- LESS学习总结
- 初识EL表达式
- Vim默认保存文件路径的设置
- js两个判断&;&;的值与||的值
- MongoDB及Mongoose的记录
- ES6_入门(4)_数组的解构赋值
- FreeSWITCH与FreeSWITCH对接
- hdu1423LCIS zoj2432 必须掌握!