java ssm框架验证码_[手把手教程][JavaWeb]SSM框架验证、修复和实例

注意:我们需要先把WEB-IN\Fweb.xml下面的mvc-dispatcher更改为全局配置。

mvc-dispatcher

/

配置了web.xml后,我们重启重启应用,输入一个错误的地址(跳转到404页面),会发现提示错误信息,主要报错如下

Exception encountered during context initialization - cancelling refresh attempt:

org.springframework.beans.factory.BeanCreationException:

Error creating bean with name 'dataSource' defined in file [项目所在的物理地址\WEB-INF\classes\spring\spring-dao.xml]:

Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/slf4j/Logger

//上面这个错误主要是提示我们:NoClassDefFoundError: org/slf4j/这个类找不到。

解决办法:偷懒的把log4j2从maven配置文件中删除了,开启了logback,在maven的pom.xml中:

ch.qos.logback

logback-classic

1.1.1

org.slf4j

slf4j-api

${org.slf4j.version}

接着,我们重启Tomcat,等项目部署完成后,我们再次打开,再次输入错误地址,我们发现还是在报错,信息如下:

Exception encountered during context initialization - cancelling refresh attempt:

org.springframework.beans.factory.BeanCreationException:

Error creating bean with name 'sqlSessionFactory' defined in file [项目物理地址\WEB-INF\classes\spring\spring-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException:

Failed to convert property value of type [java.lang.String] to required type [org.springframework.core.io.Resource[]] for property 'mapperLocations';

nested exception is java.lang.IllegalArgumentException:

Could not resolve resource location pattern [classpath:mapper/*.xml]: class path resource [mapper/] cannot be resolved to URL because it does not exist

根据上面的提示信息,我们可以看到提示的是mapper下面没有文件,那么我们就给他制定一个空的配置文件(BaseDao.xml),内容如下:

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

同样的,我们再次重启服务器,并且输入错误的地址,现在能正常显示错误404的页面了,但是页面太过Low了,我们得重写一下web.xml的配置。404页面如下所示:

ssm%E6%A1%86%E6%9E%B6%E6%A3%80%E6%B5%8B404%E6%A8%A1%E7%89%88%E9%A1%B5%E9%9D%A2.png

ssm框架检测404模版页面

解决思路:错误404的页面是常用页面之一,所以我们在项目的资源目录(webapp)下创建一个static目录,专门用来存放静态资源,如js、css、错误提示页面、登录、注册页面等等。

通过网上查阅资料,我们看到大部分人都是把404页面提示信息提交给Servelt自己管理,我们也依样画瓢,在web.xml中配置,如下:

404

/static/view/404.html

同时,我们需要给Spring写一个web的配置,控制哪些资源被拦截。spring-web.xml文件配置如下:

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.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

以上文件配置好后,我们重启服务器,并输入错误地址,现在我们插入的404页面正常显示了。

通过上面的资料,我们可以大胆整理下web请求的思路:

用户发起请求→DNS解析发现服务器→建立链接发送请求→WEB服务器分发给应用服务器→MVC层框架处理请求过程→返回数据给用户

而上面的SpringMvc作为框架层,我们也可以通过网上资料和我们目前的配置做出一些构想:

Tomact分发请求→Servelt收到请求→Spring接管Servelt→DispatcherServlet处理请求分发→根据Spring配置找到对应的控制器(Controller)处理业务→返回对应数据。

而下面有一张更加突出的图片说明了这一切:

8260d5e2dcdf?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=qq

SpringMVC详细运行流程图

总结:目前项目中的静态资源我们已经处理完毕,加入了一部分Web的静态资源,增加了Spring的web处理配置,修改了web.xml,log4j2替换为LogBack,Junit版本提升为4.12,加入了一个BaseMapper.xml。

web.xml文件如下:

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee

http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"

version="3.1" metadata-complete="true">

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

SSM

mvc-dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-*.xml

mvc-dispatcher

/

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

forceEncoding

true

encodingFilter

/*

DruidStatView

com.alibaba.druid.support.http.StatViewServlet

DruidStatView

/druid/*

druidWebStatFilter

com.alibaba.druid.support.http.WebStatFilter

exclusions

/public/*,*.js,*.css,/druid*,*.jsp,*.swf

principalSessionName

sessionInfo

profileEnable

true

druidWebStatFilter

/*

404

/static/view/404.html

BaseMapper.xml如下:

/p>

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

其他的文件修改的信息都可以在上面看到就不再次贴出来了。


版权声明:本文为weixin_42648844原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。