(未来会对每一个功能做更深入的了解)
一、开发准备
开发工具:eclipse Tomcat MySql
基于SpringMVC MyBatis框架
二、项目搭建
第一次尝试搭建一个服务端的项目,在搭建的时候遇到了许多的坑,在这里首先推荐我在这个阶段爬坑时借助到的资料
1.搭建Tomcat:
http://blog.csdn.net/dannor2010/article/details/8437998
2.安装MySql:
这里看了慕课网老师的视频,个人觉得讲解的很详细,推荐大家看一下
http://www.jb51.net/article/23876.htm
3.搭建SpringMVC项目,采用Maven Web Project搭建的方式
搭建项目的流程:
http://www.jianshu.com/p/58a55ad5b3c4
使用Intellj Idea搭建一个Maven项目。http://blog.csdn.net/myarrow/article/details/50824793
通过上述方式搭建一个MavenWebProject,会是2.3的版本,这里需要修改为2.5,在修改web版本为2.5时遇到了一些困难,该文章进行了详细的介绍: http://blog.csdn.net/steveguoshao/article/details/38414145
4.一套Spring+SpringMVC+MyBatis搭建的流程
http://blog.csdn.net/zhshulin/article/details/37956105
三、个人遇到的一些问题:
通过pom.xml导入一些包时,遇到了
Failure to transfer org.apache.maven.plugins:mavencompiler-plugin:pom:2.0.2 from http://repo1.maven.org/maven2
was cached in the local repository, resolution will not be reattempted until the
update interval of central has elapsed orupdates are forced. Original error:
Could not transfer artifact org.apache.maven.plugins:maven-compiler-plugin:pom:2.0.2 from/to central (http://repo1.maven.org/maven2):
No response received after 60000 ExampleProject Unknown Maven Problem
这是因为本地库缓存造成的 清理本地缓存指令:
window:
cd %userprofile%\.m2\repositoryfor /r %i in (*.lastUpdated) do del %i
The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误
<dependency> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope></dependency>
ClassLoaderListener没有找到错误:
1.首先检查org.springframework.web.context.ContextLoaderListene是否存在于MavenLibrary中,
2.如果存在是否通过build path方式 依赖了MavenLibrary 详细的方式见搭建SpringMVC项目
3.如果不存在导入org.springframework.spring-web包
406错误,当我想要返回json数据给客户端时,发生了该错误
he resource identified by this request is only capable of generating responses with characteristics
not acceptable according to the request "accept" headers.
" even though my *Accept*headers are
这里说的的是请求头部协议错误
1.在你的Controller类中加入@EnableWebMvc注解,在produces中加入协议application/json
@Controller
@EnableWebMvc
@RequestMapping(headers="Accept=*/*", produces="application/json")
public class LoginController {
@Resource
LoginService service;// 自動依賴注入
@Resource
LoginEntity userEntity;
@RequestMapping(value = "login", method = RequestMethod.GET,produces = "application/json",headers="Accept=*/*")
@ResponseBody//注意添加该注解
public ResultEntity login(HttpServletRequest request,HttpServletResponse response) {
//拿到服务器端的参数
String username = request.getParameter("username");
String password = request.getParameter("password");
if (service.isExitsPeople(username, password)==1) {
userEntity.setUsername(username);
userEntity.setPassword(password);
resultEntity.setIsSuccess(true);
resultEntity.setMessage("登录成功");
resultEntity.setResult(userEntity);
return resultEntity;
}else{
resultEntity.setIsSuccess(false);
resultEntity.setMessage("登录失败");
resultEntity.setResult("");
return resultEntity;
}
//System.out.println("测试login方法是否成功" + service.getUserCount(username, password));
// System.out.println("测试MyBatis使用是否成功" + service.isExitsPeople(username, password));
}
2.保证你引入了json相关jar包,这里我是用的maven来管理项目
<!-- JSON begin -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- JSON end -->
3.在你的springmvc.xml中加入
<mvc:annotation-driven />
4.请求后缀不要中.html,用.html后缀请求后,会默认会采用[text/html]编码
MyBatis返回一个自定义类型的集合时,直接resultType直接写你的类的路径就行。
<mapper namespace="com.tjpld.dao.ISmileDao">
<select id="findSmileContent" resultType="com.tjpld.entity.SmileEntity">
select * from smile
</select>
</mapper>
Mac下安装Tomcat
1.去官网下载Tomcat tar.gz格式的压缩包
http://www.cnblogs.com/xiaofeixiang/p/4299949.html
2.解压后运行startup.sh,打开localhost:8080,查看网页是否已经变成Apache的主页
/Users/apple/Desktop/tomcat/apache-tomcat-8.5.13/bin/startup.sh
3.配置tomcat的环境变量
4.在intellij idea中关联tomcat
http://blog.csdn.net/qiexingqieying/article/details/51810177
在preference中选择application servers