(未來會對每一個功能做更深入的了解)
一、開發準備
開發工具: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.lxweimin.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