2019了,感觉经济越发不好,房价却还在持续高涨,最近公司砍了几个项目,打算让我们几个搞android开发的转到服务器端。由于以前有做过python服务端开发的经验,又有写了4年android的经验,接手服务端代码没什么难度,主要障碍还是对spring全家桶的学习。
框架模型
一、添加maven依赖 <properties > <spring.version > 3.2.10.RELEASE</spring.version > </properties > <dependencies > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-core</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-beans</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-context</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-web</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-webmvc</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aop</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > org.springframework</groupId > <artifactId > spring-aspects</artifactId > <version > ${spring.version}</version > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > jstl</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > javax.annotation</groupId > <artifactId > javax.annotation-api</artifactId > <version > 1.2</version > </dependency > <dependency > <groupId > javax.jms</groupId > <artifactId > jms</artifactId > <version > 1.1</version > </dependency > <dependency > <groupId > javax.mail</groupId > <artifactId > mail</artifactId > <version > 1.5.0-b01</version > <exclusions > <exclusion > <groupId > javax.activation</groupId > <artifactId > activation</artifactId > </exclusion > </exclusions > </dependency > <dependency > <groupId > javax.servlet</groupId > <artifactId > servlet-api</artifactId > <version > 2.5</version > <scope > provided</scope > </dependency > <dependency > <groupId > javax.servlet.jsp</groupId > <artifactId > jsp-api</artifactId > <version > 2.1</version > <scope > provided</scope > </dependency > </dependencies >
二、写jsp文件 在web
目录下编写*.jsp文件 为了使代码统一,我们可以在web目录下,创建一个modules
目录(名字可以随便),专门用于存放jsp文件 hi.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Spring MVC</title> </head> <body> hello spring mvc </body> </html>
三、编写控制器 HiController.java
@Controller @RequestMapping("/hi") public class HiController { @RequestMapping("/say") public ModelAndView say (HttpServletRequest req, HttpServletResponse resp) { String params = req.getParameter("p" ); resp.addHeader("header" , "header" ); Data data = new Data (); data.setName(params); data.setUrl("http://www.baidu.com" ); return new ModelAndView ("modules/activity/hi.jsp" , "data" , data); } }
这样:http://localhost:8080/hi/say 需要注意的是: 1、绑定的数据必须是java基础数据基结构(包括List、Map、Set等等) 2、或者是Bean 数据
bean配置方法: 1、xml配置方式 创建bean配置
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > <bean id ="data2" class ="com.lao.Data" init-method ="init" destroy-method ="destory" > <property name ="name" value ="123" /> </bean > </beans >
web.xml添加bean的配置文件路径
2、Bean注解配置方式 使用注解的方式,就不需要进行xml的配置了,但是需要另外的一个@Configuration
配合才可以 Config.java
@Configuration public class Config { @Bean(initMethod = "init", destroyMethod = "destroy", name = "beanName" ) @Scope("prototype") @DependsOn() @Lazy(false) GG getGG () { return new GG (); } }
GG.java
public class GG { private String name; private String url; public void init () { } public void destroy () { } .... }
@Autowired
说明: 一般在controller里,我们需要调用service,这时候就需要用到注解 @Autowired
@Autowired private IUserService userService;
@Autowired 这个annotation,它是为了告诉spring,这个对象没有实例化,需要注入一个IUserService的实例,可是问题是,IUserSerivce是一个接口,如果不指定就不知道你想用哪个实现类,Spring会首先看自己的容器里有没有一个叫做userService的对象,如果找不到就在 配置文件 里配置的路径下面寻找IUserService的实现类,找到了就把它的对象拿过来。
@Autowired 顾名思义,就是自动装配,其作用还可以是为了消除代码Java代码里面的getter/setter与bean属性中的property。当然,getter看个人需求,如果私有属性需要对外提供的话,应当予以保留。
四、创建spring的配置文件 在src\main\resource\spring
创建dispatcher-servlet.xml
文件,当然(spring)这个目录可以改为其它名字
<?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:aop ="http://www.springframework.org/schema/aop" xmlns:context ="http://www.springframework.org/schema/context" xmlns:task ="http://www.springframework.org/schema/task" xmlns:mvc ="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd" > <context:component-scan base-package ="com.lao" /> <context:annotation-config /> <mvc:annotation-driven /> <aop:aspectj-autoproxy /> <bean id ="annotationMethodHandlerAdapter" class ="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" /> <bean id ="internalResourceViewResolver" class ="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name ="prefix" value ="/" /> </bean > <mvc:default-servlet-handler /> </beans >
五、配置web.xml 配置web.xml文件
<?xml version="1.0" encoding="UTF-8" ?> <web-app xmlns ="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version ="4.0" > <context-param > <param-name > contextConfigLocation</param-name > <param-value > </param-value > </context-param > <listener > <listener-class > org.springframework.web.context.ContextLoaderListener</listener-class > </listener > <listener > <listener-class > org.springframework.web.util.IntrospectorCleanupListener</listener-class > </listener > <servlet > <servlet-name > dispatcher</servlet-name > <servlet-class > org.springframework.web.servlet.DispatcherServlet</servlet-class > <init-param > <param-name > contextConfigLocation</param-name > <param-value > classpath:spring/dispatcher-servlet.xml </param-value > </init-param > <load-on-startup > 1</load-on-startup > </servlet > <servlet-mapping > <servlet-name > dispatcher</servlet-name > <url-pattern > /</url-pattern > </servlet-mapping > <welcome-file-list > <welcome-file > index.jsp</welcome-file > </welcome-file-list > </web-app >
六、 配置tomcat 将pom.xml的打包方式改为war
<packaging > war</packaging >
1、创建tomcat
2、配置启动包
3、修改路径 添加前缀或不用前缀
4、检查资源文件路径(非常重要) 检查web资源文件路径是否映射正确,如果为红色,表示的是映射失败!!
最终效果