问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面
一、配置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/* 所有的请求都会进行拦截
问题之Spring MVC配置后,可以打开jsp页面,但打不开html页面的更多相关文章
- spring mvc 配置后,web中的html页面报404,该怎么处理
问题描述: 在根目录webapp下的jsp页面可以通过url直接访问,而html页面就会报404错误. 解决方案1: 在spring-mvc.xml中添加如下配置: <!--将静态文件指定到某个 ...
- spring MVC配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Spring mvc 配置详解
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- spring MVC配置详解(转)
现在主流的Web MVC框架除了Struts这个主力 外,其次就是Spring MVC了,因此这也是作为一名程序员需要掌握的主流框架,框架选择多了,应对多变的需求和业务时,可实行的方案自然就多了.不过 ...
- Tomcat配置和Spring MVC配置
Tomcat启动时,先找系统变量CATALINA_BASE,如果没有,则找CATALINA_HOME.然后找这个变量所指的目录下的conf文件夹,从中读取配置文件.最重要的配置文件:server.xm ...
- Spring MVC配置详解(3)
一.Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0) 1. jar包引入 Spring 2.5.6:spring.jar.spring-webmvc.ja ...
- MQTT 4 ——MQTT的Spring Mvc 配置接收字节流数据
本篇记录一下MQTT整合Spring Mvc配置直接收发字节流数据 设备方是纯C开发,并且为了交互数据的安全,将传送的数据用了AES CBC进行了加密. 接下来正常方便做法应该是 将加密后的字节流转换 ...
- Spring MVC配置静态资源和资源包
Spring MVC配置静态资源和资源包 本例映射:css目录: pom.xml <properties> <spring.version>4.3.5.RELEASE</ ...
- 最小可用 Spring MVC 配置
[最小可用 Spring MVC 配置] 1.导入有概率用到的JAR包, -> pom.xml 的更佳实践 - 1.0 <- <project xmlns="http:// ...
随机推荐
- js javascript 简易随机值穿插加解密【原】
适用场景 本方法适用于需要对敏感信息进行加密传输,但加解密要求又不高的场景,因为是前台的javascript加解密,所以其实还是能通过js代码分析出原始值来的. 如果您对信息极其敏感, 比例登录密码这 ...
- [C#] .NET4.0中使用4.5中的 async/await 功能实现异步
在.NET Framework 4.5中添加了新的异步操作库,但是在.NET Framework 4.0中却无法使用.这时不免面临着抉择,到底是升级整个解决方案还是不使用呢? 如果你的软件还没发布出去 ...
- Groovy里面闭包中变量符号的查找与变量定义的限制
class a { def v1 = "v1 in a" static def v2 = "v2 in a" def v4 = "v4 in a&qu ...
- PC端判断浏览器类型及移动端判断移动设备类型
浏览器代理检测,可以检测出来用户使用的浏览器类型,也可以检测浏览器所在的操作系统 navigator.userAgent (1).判断浏览器类型 var t = navigator.userAgent ...
- linux Centos7下安装python3及pip3
先去python官网下载python3安装包 执行命令:wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz 安装zlib-dev ...
- 开启IIS的WebGarden、WebFarm和StateServer之旅
前言 公司系统虽然配置有1台NLB后拖4台App Server最后搭一台强劲无比的DB Server,但每天下午4点左右总被投诉系统慢,报表下载不了等问题.究其原因,原来NLB采用锁定sessionI ...
- Theano.tensor.round函数学习,同时解决输出Elemwise{xxx,no_inplace}.0的问题
1. 出现Elemwise{xxx,no_inplace}.0 这是因为没有定义theano.function所致,参考下面错误示范: y = np.random.normal(size=(2,2 ...
- python 识别图片文字
今天群里有兄弟问如何把图片的文字给识别出来 对于python来说这不是小菜一碟吗,于是乎让pupilheart狠狠的吹了一波(哈哈,竟然没懂),下面将整个实现过程给大家实现下: 方法一:自己搞定ORC ...
- springboot接口访问权限AOP实现
场景 现在有个系统,很多接口只需要登录就可以访问,但是有些接口需要授予并验证权限.如果用注解controller的方式控制接口的权限呢? 1.注解声明代码 这个注解是要装饰在controller接口上 ...
- juypter4.4.0 自动补全
python -m pip install jupyter_contrib_nbextensions jupyter contrib nbextension install --user --skip ...