SpringMVC填坑之路
不小心删除了文件
因为自己用来练手的电脑比较卡,所以把其中一个敲了一下午的配置文件删掉了。当时懵掉了,思考了一下,可能会有记录,就找了这个方法
Myeclipse文件不小心删除时 右键项目–Restore from local History 就可以看到删除修改过的文件了
Mepper.xml 文件没有提示词
在手打编辑xml文件时候,能记得的长单词非常少,而且有提示也方便。
出现这个问题可能是配置问题
首先打开eclipse的Window→preferences→XML→XML Catalog
我们一共要添加两个配置,一个是config,一个是mapper
先添加config ,点击add
在Location项中写入配置路径 “ http://mybatis.org/dtd/mybatis-3-config.dtd
-//mybatis.org//DTD Config 3.0//EN ”
key type选择URL,key写Config就可以,完成后点ok
mapper也是同样的方法。
路径写“ http://mybatis.org/dtd/mybatis-3-mapper.dtd
-//mybatis.org//DTD Mapper 3.0//EN “
最后重启eclipse。
找不到applicationContext.xml问题
一般是web.xml中没配置好,
<!-- Spring 配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
这个写在靠前的位置 这个错误应该就不会有了。
不要笃定你的代码没有问题,有时候会有大小写,字母写反等问题。因为是自己写的,检查的时候容易惯性思维忽略过去。
beans问题
报错 :
BeanFactory not initialized or already closed - call ‘refresh’ before accessing beans via the ApplicationContext
出现这个可能是Spring-mvc.xml aclicationContext.xml两个配置文件头部的路径项目访问不了,一个个点了试试看,哪个不能访问就百度一下 改哪个。
PS:博主在修改这个的时候遇到了一个哭笑不得的问题,最后一行的路径死活加载不出来。可是我把路径复制到浏览器却能访问,在eclipse中按Ctrl+左击却访问不了。搞得博主差点疯掉,最后发现,是最后的双引号和路径挨着了,在文件里访问时候吧双引号也当成了路径的一部分。
就是上图左下角的双引号,别的地方也没有出现这个问题,就这个地方有,不知道为什么,写出来希望遇到的朋友注意一下。
Spring mvc乱码问题
- json字符串乱码
先查看web.xml是否已经添加设置好编码方式
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>ture</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
- Request时可以加编码方式
request.setCharacterEncoding("utf8");
- 数据库数据乱码
在对数据库数据进行操作时出现中文乱码,可以修改数据库的配置文件解决
博主用的是mysql,就分享一下这个配置
找到你的mysql安装根路径,博主的是D:\Program Files\mysql-5.7.20
里面有my-default.ini 或者my.ini文件
用记事本方式打开
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = D:\Program Files\mysql-5.7.20
datadir = D:\Program Files\mysql-5.7.20\data
port = 3306
# server_id = .....
character-set-server=utf8
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# !o>UZ!e(h6(m 初始化的密码
这是博主的配置 ,重点是那几个utf-8的配置。这样配置完应该就可以了
后记:第一次写博客,之前一直想把自己遇到的日常问题做个记录。但是由于执行力不足,一拖再拖。导致现在碰见解决过的问题时候还是需要重新找方法。争取以后把每天遇到的问题记录下来,定时更新博客。也算是对自己的一种勉励吧