Spring学习实例1-Spring MVC hello world

虽然作java好几年了,可是用spring的时间并不长,如今准备基于Spring开发一个产品,在开发产品的同时,作一系列的实例,把Spring的使用过程记录下来。html

涉及到的技术会包括Spring和Spring MVC等,以实践为主,适合新手一块儿学习,高手请飘过。java

今天开始第一篇:Spring MVC Hello Worldgit

一、建立Web Projectgithub

我使用的IDE是MyEclipse 2014,其余版本的操做相似。web

Target runtime选择none,一路默认或者直接点击Finishspring

二、建立完成后,会报HttpServlet找不到的错误api

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

引入servlet-api.jar便可,若是安装了tomcat能够从tomcat里面拷贝这个jar包,若是在刚才第一步选择了Target runtime会自动添加这些jar包,可是还会添加些别的。tomcat

三、引入spring的jar包,这里用的版本是3.0.5,jar包的种类参考了《Spring 3.x企业应用开发实战》,后续的工程也会继续参考这本书。mvc

spring的jar包和源码下载地址在官网上不是很是好找,记录下,方便使用app

spring jar包下载地址:

http://repo.spring.io/release/org/springframework/spring/

spring源码下载地址:

https://github.com/spring-projects/spring-framework/tags

引入的方式:将jar报拷贝到WEB-INF/lib下便可

引入的jar包能够参看我在码云上分享的代码,地址:

http://git.oschina.net/smilease/spring-example

hello world 项目自己不须要引入这么多jar包,之后的功能会用到,先引入了。

四、修改web.xml,若是没有就新建一个,web.xml,是整个项目的基础配置,其余配置文件须要经过它来配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
	<display-name>spring example</display-name>
	<servlet>
		<servlet-name>tsingyu</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>tsingyu</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>

这里配置了spring要拦截的后缀名.html,配置了欢迎页的路径,这个欢迎页也是要被sping拦截的。

五、添加tsingyu-servlet.xml,tsingyu是web.xml里面servlet-name的值,spring默认加载 *-servlet.xml,默认路径是在web.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:p="http://www.springframework.org/schema/p"
	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/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       <!-- 扫描web包,应用Spring的注解 -->
	<context:component-scan base-package="cn.tsingyu.spring.example.controller"/>
	
	<!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		p:viewClass="org.springframework.web.servlet.view.JstlView" 
		p:prefix="/WEB-INF/jsp/"
		p:suffix=".jsp" />
</beans>

这里配置了controller类的路径和视图文件的路径及后缀。

六、新建controller文件,路径与上面的配置保持一致

package cn.tsingyu.spring.example.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class IndexController {
	@RequestMapping(value = "/index.html")
	public String loginPage(){
		return "hello";
	}
}

经过@Controller注解来声明这是一个controller,经过@RequestMapping来配置要匹配的请求路径,这里配置的是欢迎页的路径,这样访问项目跟地址的时候就会进入到这个方法处理。

七、新建视图文件,路径及后缀与tsingyu-servlet.xml的配置一致,这里只作一个简单的hello world的jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
Hello World
</body>
</html>

八、发布到tomcat上,启动tomcat,访问http://localhost:8080/spring1/,看到hello world,成功。

以上就是spirng mvc的hello world,后续会继续扩展。

完整代码见:

http://git.oschina.net/smilease/spring-example

相关文章
相关标签/搜索