这两天再看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"> |
06 |
< title >Spring 3.0 MVC demo</ title >
|
09 |
< a href = "hello.html" >Say Hello</ a >
|
在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" >
|
06 |
< display-name >Spring3MVC</ display-name >
|
08 |
< servlet-name >spring</ servlet-name >
|
09 |
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
|
10 |
< load-on-startup >1</ load-on-startup >
|
14 |
< servlet-name >spring</ servlet-name >
|
15 |
< url-pattern >*.html</ url-pattern >
|
19 |
< welcome-file >index.jsp</ welcome-file >
|
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">
|
11 |
< context:component-scan base-package = "net.spring.controller" />
|
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" />
|
然后生成java文件HelloWorldController.java包路径为net.spring.controller
01 |
package net.spring.controller;
|
03 |
import org.springframework.stereotype.Controller;
|
04 |
import org.springframework.web.bind.annotation.RequestMapping;
|
05 |
import org.springframework.web.servlet.ModelAndView;
|
08 |
public class HelloWorldController {
|
10 |
@RequestMapping ( "/hello" )
|
11 |
public ModelAndView helloWorld() {
|
13 |
String message = "Hello World, Spring 3.0!" ;
|
14 |
System.out.println(message);
|
15 |
return new ModelAndView( "hello" , "message" , message);
|
到此就完成了所有的代码和配置,所有的文档和配置加在一起只有60行左右,比较简单把。
完成后就可以放在tomcat等容器中运行一下,然后在web中打开地址。
在index.jsp上点击Say Hello,就会转向http://localhost:8080/springmvcdemo/hello.html页面显示Hello World, Spring 3.0!,表示工作正常。
简单把。