(未來(lái)會(huì)對(duì)每一個(gè)功能做更深入的了解)
一、開(kāi)發(fā)準(zhǔn)備
開(kāi)發(fā)工具:eclipse Tomcat MySql
基于SpringMVC MyBatis框架
二订咸、項(xiàng)目搭建
第一次嘗試搭建一個(gè)服務(wù)端的項(xiàng)目虎谢,在搭建的時(shí)候遇到了許多的坑盟榴,在這里首先推薦我在這個(gè)階段爬坑時(shí)借助到的資料
1.搭建Tomcat:
http://blog.csdn.net/dannor2010/article/details/8437998
2.安裝MySql:
這里看了慕課網(wǎng)老師的視頻,個(gè)人覺(jué)得講解的很詳細(xì)婴噩,推薦大家看一下
http://www.jb51.net/article/23876.htm
3.搭建SpringMVC項(xiàng)目擎场,采用Maven Web Project搭建的方式
搭建項(xiàng)目的流程:
http://www.reibang.com/p/58a55ad5b3c4
使用Intellj Idea搭建一個(gè)Maven項(xiàng)目。http://blog.csdn.net/myarrow/article/details/50824793
通過(guò)上述方式搭建一個(gè)MavenWebProject几莽,會(huì)是2.3的版本迅办,這里需要修改為2.5,在修改web版本為2.5時(shí)遇到了一些困難章蚣,該文章進(jìn)行了詳細(xì)的介紹: http://blog.csdn.net/steveguoshao/article/details/38414145
4.一套Spring+SpringMVC+MyBatis搭建的流程
http://blog.csdn.net/zhshulin/article/details/37956105
三站欺、個(gè)人遇到的一些問(wèn)題:
通過(guò)pom.xml導(dǎo)入一些包時(shí),遇到了
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
這是因?yàn)楸镜貛?kù)緩存造成的 清理本地緩存指令:
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錯(cuò)誤
<dependency> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope></dependency>
ClassLoaderListener沒(méi)有找到錯(cuò)誤:
1.首先檢查org.springframework.web.context.ContextLoaderListene是否存在于MavenLibrary中,
2.如果存在是否通過(guò)build path方式 依賴(lài)了MavenLibrary 詳細(xì)的方式見(jiàn)搭建SpringMVC項(xiàng)目
3.如果不存在導(dǎo)入org.springframework.spring-web包
406錯(cuò)誤矾策,當(dāng)我想要返回json數(shù)據(jù)給客戶(hù)端時(shí)磷账,發(fā)生了該錯(cuò)誤
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
這里說(shuō)的的是請(qǐng)求頭部協(xié)議錯(cuò)誤
1.在你的Controller類(lèi)中加入@EnableWebMvc注解,在produces中加入?yún)f(xié)議application/json
@Controller
@EnableWebMvc
@RequestMapping(headers="Accept=*/*", produces="application/json")
public class LoginController {
@Resource
LoginService service;// 自動(dòng)依賴(lài)注入
@Resource
LoginEntity userEntity;
@RequestMapping(value = "login", method = RequestMethod.GET,produces = "application/json",headers="Accept=*/*")
@ResponseBody//注意添加該注解
public ResultEntity login(HttpServletRequest request,HttpServletResponse response) {
//拿到服務(wù)器端的參數(shù)
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("測(cè)試login方法是否成功" + service.getUserCount(username, password));
// System.out.println("測(cè)試MyBatis使用是否成功" + service.isExitsPeople(username, password));
}
2.保證你引入了json相關(guān)jar包,這里我是用的maven來(lái)管理項(xiàng)目
<!-- 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.請(qǐng)求后綴不要中.html贾虽,用.html后綴請(qǐng)求后逃糟,會(huì)默認(rèn)會(huì)采用[text/html]編碼
MyBatis返回一個(gè)自定義類(lèi)型的集合時(shí),直接resultType直接寫(xiě)你的類(lèi)的路徑就行蓬豁。
<mapper namespace="com.tjpld.dao.ISmileDao">
<select id="findSmileContent" resultType="com.tjpld.entity.SmileEntity">
select * from smile
</select>
</mapper>
Mac下安裝Tomcat
1.去官網(wǎng)下載Tomcat tar.gz格式的壓縮包
http://www.cnblogs.com/xiaofeixiang/p/4299949.html
2.解壓后運(yùn)行startup.sh绰咽,打開(kāi)localhost:8080,查看網(wǎng)頁(yè)是否已經(jīng)變成Apache的主頁(yè)
/Users/apple/Desktop/tomcat/apache-tomcat-8.5.13/bin/startup.sh
3.配置tomcat的環(huán)境變量
4.在intellij idea中關(guān)聯(lián)tomcat
http://blog.csdn.net/qiexingqieying/article/details/51810177
在preference中選擇application servers