bug日志
如何从github上下拉maven项目?
流程如下:
references:Importing a Maven project into Eclipse from Git - Stack Overflow
eclipse的默认字符编码设置:
references:eclipse编码格式设置 - AlanLee(Java) - 博客园
前端环境部署的问题
报bug的语法:
No qualifying bean of type [com.molly.service.IStudentService] found for dependency
报这个错误是因为springmvc没有能够成功注入对象。我是在搭建SSM+Shiro环境的时候出的问题。最后找到的原因是。我将spring的配置文件按照不同的框架分成了几个xml配置文件,但是在web.xml中写contextConfigLocation的时候却是分开写的。正确写法如下:
配置applicationContext路径:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:config/applicationContext.xml,
classpath:config/spring-shiro.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
报错情况:使用postman测试后台功能。其他的所有handler的所有方法都能访问,只有一个handler里面的一个方法不能访问。经过排查,发现是我做了springMVC的校验拦截的原因。而我的校验拦截做错了。如图,int的数据类型却错误写成了
@NotBlank(message="{null}")
。因此使用hibernate-validate的时候要注意数据类型对应使用不同的注解。
报错:使用了注解却报错
The method must override a superclass method
。 这个报错的原因在于我使用github pull项目,因此在eclipse里面默认Java的版本低于1.5,需要这样去修改:
references:java - The method must override a superclass method - Stack Overflow
eclipse没有报错,却出现红叉。
出现这个错误的原因在于我用maven做依赖,却没有办法成功下载jar包。最终并没有主动解决掉这个问题,还是等待了两天就可以使用了。那么,我学到的这个问题是如何找到problem。
1.进入 eclipse ,按图示操作。
2.找到 General 下的 problems 。双击 problems 。就会在下边提示你项目报错的原因。根据错误提示去解决就好。
用maven做文件上传功能的流程:
- 导包:
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
- 前端做一个form表单。注意,id要在后面的js代码里面使用需要写上;第二,一定要加上enctype=”multipart/form-data”。
<form id="upload-form" name="upload-form" method="POST" enctype="multipart/form-data" action="#">
<input id="upfile" type="file" name="file">
<!-- 隐藏 div 用于存放 js 把对象转换成的 input 标签-->
<div style="display: none;" id="upfile-add-input"></div>
<div style="margin-top: 5%">
<input type="button" onclick="ajaxUpload();" value="提交">
<input type="reset" value="重置">
</div>
</form>
- 前端写一个提交js方法:
function ajaxUpload(e) {
var f = document.querySelector('#upload-form');
// 获取请求参数
var o = new FormData(f);
$.ajax({
'url': '/upload',
'type': 'POST',
'data': o,
'contentType': false, // 这两个字段不能省略,必须写上
'processData': false, // 这两个字段不能省略,必须写上
'success': function (result) {
alert(result);
},
'error': function() {
alert('error');
}
});
}
- 后台的handler接收数据:
@RequestMapping("/upload")
@ResponseBody
public String upload(Student student, @RequestParam("file") MultipartFile file) {
System.out.println(student);
System.out.println(file.getOriginalFilename());
return "success";
}
使用mybatis的update来操作数据库,明明有操作影响行数为1行,但是int值却为零。这个问题,我还没有解决掉。
eclipse开启自动刷新功能
上传图片,如果放到工程目录下则需要后台刷新refresh前端才能读取。如果放到服务器平台下,则重启服务器以后文件就会丢失。如果之后会学习其他的技术能够解决掉这个问题。我在这个项目里面采用的解决方案是打开eclipse的自动刷新功能。操作如图:
使用shiro登陆进入不了handler的登陆方法。
这个问题其实是因为我之前登陆了,但是忘记logout,因此它已经被authority了,因此它不会进入这个方法进行再次认证。
自定义shiro的logout目录路径
logout需要配置三个地方。一个是bean , 一个是 filterChainDefinitions , 一个是 filters。 下面的value改为需要跳转过去的资源路径
<bean id="logout" class="org.apache.shiro.web.filter.authc.LogoutFilter">
<property name="redirectUrl" value="html/login.html"/>
</bean>
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- shiroFilter进行路径过滤必须依赖安全管理器 -->
<property name="securityManager" ref="securityManager"></property>
<!-- 身份认证失败后要跳转的地址 -->
<property name="loginUrl" value="/html/login.html"></property>
<!-- 授权失败,配置失败的地址 -->
<property name="unauthorizedUrl" value="/html/error.html"></property>
<!-- 配置自定义过滤器 -->
<property name="filters">
<map>
<entry key="roleOr" value-ref="roleFilter"></entry>
<entry key="logout" value-ref="logout" />
</map>
</property>
<property name="filterChainDefinitions">
<value>
<!-- anon 是shiro提供的过滤器,代表匿名访问,无需认证直接访问 -->
<!-- shiro的退出 -->
/logout = logout
</value>
</property>
</bean>
references:shiro 自定义logout路径 - ITDragon博客 - CSDN博客
项目总结
这次项目使用的是SSM+Shiro+boostrap框架实现的需求。 和同学的对比,发现了自己有几个问题:
1、前端的基础技术不熟悉,框架也不会使用。因此浪费了很多时间在前端上,却也没有能够成功实现很多功能;
2、数据库建表并没有严格按照三范式去进行建表。用同学使用了中间表替代外键的方式;
3、值得夸奖的是严格按照dao→service→controller一层一层往上写的方式。并且一层写完就做一层测试,以至于前端出问题的时候能够快速排出后台的问题(基本上后台不会报红了)。controller层使用postman发请求,写API文档的方式也非常好用。我写前端的时候直接翻阅文档就可以看到前后台传参的方式,不用再在到eclipse一个个翻代码。也可以使用postman快速发请求定位controller层的错误。