转载:https://www.jianshu.com/p/d403febaad83
前言
开发者都有跨界的情怀,比如Android开发学学iOS项目,iOS开发偶尔写写Html,JS之类的。而我原来是服务端开发,现在做了Android。后面可能写一系列的服务端开发文章,旨在帮大家了解服务端开发的世界。絮絮叨叨的话不想多说了,我们直接来搭建一个服务项目,然后能为自己的app提供服务才是最有成就感的事。对服务端开发没兴趣的开发者也可以直接看文章最后的召集小伙伴部分。
项目基本框架
开发框架主要是
- maven
- SpringMVC
- mybatis
开发的IDE推荐IntelliJ IDEA, 前几天下载了最新的eclipse折腾的够呛,果断换到IntelliJ IDEA。tomcat,maven请自行下载。建议先学习下servlet技术,这个是比较基础的东西。
开始
1
用idea新建一个maven项目
建完后大概的目录结构如下
大概说一下这三个部分
- src 目录下主要是工程代码。java目录下主要是java文件,resource目录下是些配置文件,webapp下是web相关的代码如js,css,jsp等。
- target 目录下主要是编译后的class文件,war包之类
- pom.xml maven的文件,很重要
2
配置web.xml文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
id="CHANGEME"
version="3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<!-- ==================================================== spring相关 ================================================= -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- ==================================================== session cookie ================================================= -->
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<!--<session-config>-->
<!--<session-timeout>15</session-timeout>-->
<!--</session-config>-->
<!-- ==================================================== 拦截器 ================================================= -->
<!--字符集拦截器-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- ==================================================== 默认页面 ================================================= -->
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
伸手党可以直接拿去用。web.xml文件有2.3、2.4、2.5、3.0版本, 这里是3.0。servlet基础的东西就不细说了,主要说下spring相关的。
- 监听器的配置是为了监听spring的bean初始化相关工作。
- <context-param>标签中的一组参数是为了映射/main/resource目录下的spring的配置文件,这里使用了一个通配符*。
- 第三个配置是为了实现springMVC的核心,他把所有servlet中的路由配置全都托管给了spring,由DispatcherServlet负责事件调度,流程控制,路由分发。
3
接着在resource目录下配置spring。本文先解决springmvc的问题,完成一个 http请求 -> 服务端应答的过程。mybatis其实就是个ORM框架,我们先不管数据库这块的东西。
红色的三块东西,我们先关注applicationContext.xml, spring-servlet.xml 2个文件。也就是说我们只要建了这个2个文件就能完成一个请求应答过程了。
** applicationContext.xml **
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描包 -->
<context:component-scan base-package="com.wolearn.service"/>
</beans>
这个文件很简单,意思就是扫描com.wolearn.service包下的注解,因为我们这里是用spring的注解来完成依赖注入的。service下放的都是业务组件。java下的文件分包如下
controller下的文件都是跟你访问的目标地址有关的信息。
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 注解的支持 -->
<mvc:annotation-driven />
<!-- 将webapp根目录映射到url: /resources/ -->
<mvc:resources location="views" mapping="/resources/**" />
<!-- 自动扫描包 -->
<context:component-scan base-package="com.wolearn.controller" />
<!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
这里的配置是为了将访问路径和视图关联起来。
4
配置到这里,已经可以基本完成一个请求的发送了。我们来尝试写个接收请求的controller。
@Controller
public class TestController {
@RequestMapping("index")
public ModelAndView index() {
ModelAndView view = new ModelAndView("test");
return view;
}
}
RequestMapping注解的意思是接收来自/index的请求。当你请求
http://localhost:8080/工程名/index
时,会返回给你工程目录下/webapp/views/test.jsp页面。当然你要先把工程部署到tomcat中。
后记
你是否成功完成一个http请求的应答呢。当然完成服务端开发,这些还是不够的,后面还有整合mybatis完成数据库,日志等等