[Spring实战系列]
来源:互联网 发布:淘宝找不到自己的品牌 编辑:程序博客网 时间:2024/06/05 06:07
由于之前使用的《spring实战》第二版版本太老,所以我在接下来的文章会开始使用《spring实战》第四版。在第四版的书中,书从第五章就开始使用MVC进行web开发,和本系列的专题进度相仿,所以我会从第五章重新开始。另外,在第四版的书中,给了一个SpringMVC开发类似twitter的网站的例子,我也会根据这个例子,来展示如何使SpringMVC。
在这本书和我们之前构建SpringMVC的方式不同,作者使用的是纯Java配置的方式,极少的应用了XML。我觉得这是很好的一种方式,方便快捷。但是,我们之前XML的方式,条理也很清晰,不是么?
1.构建一个maven的项目,在maven的项目结构下新建包,完善项目结构:
2.配置你的pom.xml中的依赖:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>spitter</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>spitter Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <!-- Generic properties --> <java.version>1.6</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <!-- Web --> <jsp.version>2.2</jsp.version> <jstl.version>1.2</jstl.version> <servlet.version>3.1.0</servlet.version> <!-- Spring --> <spring-framework.version>3.2.3.RELEASE</spring-framework.version> <!-- Hibernate / JPA --> <hibernate.version>4.2.1.Final</hibernate.version> <!-- Logging --> <logback.version>1.0.13</logback.version> <slf4j.version>1.7.5</slf4j.version> <!-- Test --> <junit.version>4.11</junit.version> </properties> <dependencies> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Other Web dependencies --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${servlet.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>${jsp.version}</version> <scope>provided</scope> </dependency> <!-- Spring and Transactions --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-framework.version}</version> </dependency> <!-- Logging with SLF4J & LogBack --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> <scope>runtime</scope> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> </dependency> <!-- Test Artifacts --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Apache Commons Lang, a package of Java utility classes for the classes that are in java. --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- Mock objects library for java --> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.10.19</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.1.0.Final</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>3.0.5</version> </dependency> <!-- XML/XHTML/HTML5 template engine for Java --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring3</artifactId> <version>2.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>2.1.4.RELEASE</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>1.3.3.RELEASE</version> </dependency> --> <!-- MySQL JDBC Type 4 driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> </dependencies> <build> <finalName>spitter</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build></project>
3. 构建配置我们的DispatcherServlet
DispatcherServlet作为我们SpringMVC的核心,是我们需要首先配置组件。在我们使用Java配置时,要构建类似这样的一个类:
package spitter.config;/** * Created by YanMing on 2017/2/26. */import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected String[] getServletMappings() { return new String[] { "/" }; } @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { RootConfig.class }; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; }}
这个类继承了AbstractAnnotationConfigDispatcherServletInitializer,他是用来配置我们的DispatcherServlet,初始化我们Spring的容器的。getServletMappings()函数,类似于我们之前配置的,我们的URL起始是以"/"的。对比我们之前的配置方法,你是不是突然想到我们之前用过的一个很重要的东西?上下文载入器!对,就是我们之前使用过的ContextLoaderListener。这个是用来监听我们配置Spring的xml文件的。现在我们使用RootConfig和WebConfigWebConfig:
package spitter.config;/** * Created by YanMing on 2017/2/26. */import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.ViewResolver;import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver;@Configuration@EnableWebMvc@ComponentScan("spitter.web") public class WebConfig extends WebMvcConfigurerAdapter { @Bean public ViewResolver viewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); return resolver; } @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); }}
在WebConfig中,我们首先使用@configuration注解,告诉Spring这是一个配置类,同时,使用@ComponentScan注解,声明Controller存在的包。声明一个用于解析视图的Bean,告诉Servlet,jsp文件的存在路径。RootConfig:
package spitter.config;/** * Created by YanMing on 2017/2/26. */import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.FilterType;import org.springframework.web.servlet.config.annotation.EnableWebMvc;@Configuration@ComponentScan(basePackages = { "spitter" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })public class RootConfig {}
由于这个小例子是用于web开发的所以,在《Spring实战》也没有对RootConfig进行过多的解释。RootConfig配置了基础包的路径。4.编写你的控制器:
package spitter.web;/** * Created by YanMing on 2017/2/26. */import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller // 声明一个控制器public class HomeController { @RequestMapping(value = "/hello", method = RequestMethod.GET) // 处理GET请求 public String home() { return "home"; }}
<%-- Created by IntelliJ IDEA. User: YanMing Date: 2017/2/26 Time: 16:01 To change this template use File | Settings | File Templates.--%><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%><html><head> <meta charset="utf-8"> <title>Spittr</title> <link rel="stylesheet" type="text/css" href="<c:url value="/resources/style.css" />"></head><body><h1>Welcome to Spittr</h1><a href="<c:url value="/spittles" />">Spittles</a> |<a href="<c:url value="/spitter/register" />">Register</a></body></html>
在如果你把上述代码输入intellij,idea可能会在 两个链接处报错。不用理他,这是因为我们还没有完善这两个链接对应的controller。6.像之前那样配置tomcat:
如果你忘了tomcat配置,这里是传送门!
7.运行tomcat,输入 http://localhost:8080/hello
本文Github源码下载
1 0
- Spring实战系列
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring Boot实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring实战系列]
- [Spring Boot实战系列]
- [Spring Boot实战系列]
- [Spring Boot实战系列]
- Spring Boot实战系列教程
- 【Spring Security实战系列】Spring Security实战(一)
- 《CSS权威指南第三版》第五章的读书笔记
- Web基础之Ajax
- Implement Trie (Prefix Tree)
- MongoDB游标的基本操作
- leetcode3. Longest Substring Without Repeating Characters
- [Spring实战系列]
- UVA
- MongoDB基础学习一 ---- MongoDB的基本使用
- ANDROID应用规划及架构设计
- ……传奇
- sdutacm-活动选择
- 从C#到TypeScrip
- 代码重构梳理-实践篇(上)
- html文本输入框,密码输入框