这两天再看spring mvc,想再网上找一个demo,感觉国内的demo都太复杂了。后来在国外网站上发现了一个比较简单,应该说是最简单的spring mvc的demo了,在此做个记录,给需要的人了解一下。
第一步:准备包:
日志相关包
jcl-over-slf4j-1.6.1.jar
logback-classic-0.9.29.jar
logback-core-0.9.29.jar
slf4j-api-1.6.1.jar
jstl包
jstl-1.2.jar
spring 相关包
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
以前的版本好像叫springmvc包,现在改为servlet包
org.springframework.web.servlet-3.1.1.RELEASE.jar
第二步:
在eclipse工程中建Dynamic Web project,向导式的开发,一路next,最后得到一个web工程。
在WebContent目录下生成一个index.jsp文件
01
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
02
????
pageEncoding="ISO-8859-1"%>
03
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
04
<
html
>
05
<
head
>
06
?
<
title
>Spring 3.0 MVC demo</
title
>
07
</
head
>
08
<
body
>
09
?
<
a
href
=
"hello.html"
>Say Hello</
a
>
10
</
body
>
11
</
html
>
在WebContent\WEB-INF目录下生成一个jsp文件夹和两个配置文件:spring-servlet.xml、web.xml
web.xml内容为
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
web-app
version
=
"2.5"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
03
????
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04
????
xsi:schemaLocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
05
?
06
????
<
display-name
>Spring3MVC</
display-name
>
07
????
<
servlet
>
08
????????
<
servlet-name
>spring</
servlet-name
>
09
????????
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
10
????????
<
load-on-startup
>1</
load-on-startup
>
11
????
</
servlet
>
12
?
13
????
<
servlet-mapping
>
14
????????
<
servlet-name
>spring</
servlet-name
>
15
????????
<
url-pattern
>*.html</
url-pattern
>
16
?????
</
servlet-mapping
>
17
?
18
?????
<
welcome-file-list
>
19
????????
<
welcome-file
>index.jsp</
welcome-file
>
20
????
</
welcome-file-list
>
21
?
22
</
web-app
>
spring-servlet.xml内容为
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
02
<
beans
xmlns
=
"http://www.springframework.org/schema/beans"
03
?
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
04
?
xmlns:p
=
"http://www.springframework.org/schema/p"
05
?
xmlns:context
=
"http://www.springframework.org/schema/context"
06
?
xsi:schemaLocation="http://www.springframework.org/schema/beans
07
??
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08
??
http://www.springframework.org/schema/context
09
??
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
10
??
?
11
?
<
context:component-scan
base-package
=
"net.spring.controller"
/>
12
??
?
13
?
<
bean
id
=
"viewResolver"
14
??
class
=
"org.springframework.web.servlet.view.UrlBasedViewResolver"
>
15
??
<
property
name
=
"viewClass"
16
???
value
=
"org.springframework.web.servlet.view.JstlView"
/>
17
??
<
property
name
=
"prefix"
value
=
"/WEB-INF/jsp/"
/>
18
??
<
property
name
=
"suffix"
value
=
".jsp"
/>
19
?
</
bean
>
20
</
beans
>
然后生成java文件HelloWorldController.java包路径为net.spring.controller
01
package
net.spring.controller;
02
?
03
import
org.springframework.stereotype.Controller;
04
import
org.springframework.web.bind.annotation.RequestMapping;
05
import
org.springframework.web.servlet.ModelAndView;
06
?
07
@Controller
08
public
class
HelloWorldController {
09
?
10
?
@RequestMapping
(
"/hello"
)
11
?
public
ModelAndView helloWorld() {
12
?
13
??
String message =
"Hello World, Spring 3.0!"
;
14
??
System.out.println(message);
15
??
return
new
ModelAndView(
"hello"
,
"message"
, message);
16
?
}
17
?
18
}
到此就完成了所有的代码和配置,所有的文档和配置加在一起只有60行左右,比较简单把。
完成后就可以放在tomcat等容器中运行一下,然后在web中打开地址。
在index.jsp上点击Say Hello,就会转向http://localhost:8080/springmvcdemo/hello.html页面显示Hello World, Spring 3.0!,表示工作正常。
简单把。