Spring4 Spring MVC实战(一)——读《Spring in action》搭建最简单的MVC

现在还在用Struts吗,是的,不说你在用Spring MVC,我都觉得不好意思了。

前面的时候所谓的MVC模式中,C的角色交由Struts控制。而Spring MVC这样叫了,字眼上大概知道这东西是干什么用的。


之前是在哪里看到有位大哥这样说的。技术差别大,一个一个追逐下来很累。要知道技术的出现解决了什么问题,用时不会偏离。流行为何,没落为何。这次是因为本身这个基本上都用这个,还有自己也想尝试一下。其实重复的工作当中,真的不想满足于此,见多了之后就想自己去了解更多,去提升自己更多。


这里算是翻译一些重要的知识点加自己动手实现。


第三版看的是中文版,第四版看的英文版,第三版把MVC一直讲一直讲,不是从基础的实现开始。反正一连起来看感觉不好。

第四版第五章就开始讲了MVC的内容,而且会简单一些。


首先是大体的流程。
1、请求从浏览器离开,包含了地址还有数据。
2、Spring的DispatcherServlet,Spring通过一个简单的前端控制器过滤请求。,将请求传给控制器。
3、具体给哪个控制器,根据Handler Mapping(处理器映射)。
4、在Controller(控制器)这里处理信息。打包模型数据还有视图名称给DispatcherServlet
5、DispatcherServlet咨询view resolver(视图解析器),找到具体的视图映射。
6、最后将数据返回展示给客户端。

理解大概的流程之后,开始搭建。第三版中,是要我们在web.xml中配置DispatcherServlet

  <servlet>
      <servlet-name>spring-mvc</servlet-name>
      <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>    

第四版里,因为Servlet3.0和Spring3.1的新优势,可以直接在java里面配置。
但是这个不用在web.xml配置取决你所用的服务器。Tomcat7及以上版本就支持Servlet3.0


这里蛋疼了一下。之前的Eclipse建的有各种各样的问题,又是启动慢。
现在重新弄了套全新的东西Eclipse Neon+Tomcat 8+Java 8+Maven+Spring 4新建Maven项目后能直接就在Tomcat上跑了。


创建maven项目 web工程。


POM.xml配置,不得不说,Maven真是个好东西。能帮你处理这些复杂的依赖关系。

<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>iaiti</groupId>
	<artifactId>springmvc</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>springmvc Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>

		<!--jstl和standard是是jsp页面中 jstl依赖的包-->
		<!-- https://mvnrepository.com/artifact/jstl/jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/taglibs/standard -->
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- Spring dependencies -->
		<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>

		<!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.4.RELEASE</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>springmvc</finalName>
	</build>
</project>


继承AbstractAnnotationConfigDispatcherServletInitializer的类自动的配置了DispatcherServlet和
spring的应用上下文。
DispatcherServlet 一启动,创建Spring 的应用上下文。这里通过WebConfig的配置加载bean。
但是ContextLoaderListener也会创建Spring 的应用上下文。
DispatcherServlet加载的是controllers, view resolvers,和handler mappings这些bean。
AbstractAnnotationConfigDispatcherServletInitializer创建了这两者。
getServletConfigClasses()返回的@Configuration类为Dispatcher-
Servlet的应用上下文定义beans ,getRootConfigClasses() 返回的@Configuration类为ContextLoaderListener
的应用上下文定义beans。

具体代码:

public class WebAppInitializer 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 };
	}
}

WebConfig.java

@Configuration
@EnableWebMvc
@ComponentScan("spitter.web")
public class WebConfig extends WebMvcConfigurerAdapter {
	@Bean
	public ViewResolver viewResolver() {
		//书这里就讲得很好了,这里不需要先知道这个是什么视图解析器,跟你分析好,哪些是这章先要知道的哪些是不需要的。
		//只需要知道用来根据view找jsp文件就好了
		//例如home 会找/WEB-INF/views/home.jsp
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		resolver.setExposeContextBeansAsAttributes(true);
		return resolver;
	}

	@Override
	//告诉DispatcherServlet转接静态资源请求给servlet容器本身默认的servlet而不是自己操作自己
	public void configureDefaultServletHandling(
			DefaultServletHandlerConfigurer configurer) {
		configurer.enable();
	}
}

Root.java

@Configuration
@ComponentScan(basePackages = { "spitter" }, excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) })
public class RootConfig {
}

简单的控制器——HomeController

@Controller
public class HomeController {
	//处理请求,返回具体的view名称  view名称。根据WebConfig中的视图解析器,跳转的是/WEB-INF/views/home.jsp
	@RequestMapping(value = "/home",method= {RequestMethod.GET})
	public String home() {
		return "home";
	}
}

最后,直接运行。http://localhost:8080/springmvc/home变做到了和Struts2一样的MVC模式。


猜你喜欢

转载自blog.csdn.net/iaiti/article/details/52423166
今日推荐