首先说明一下使用的版本: JDK:1.6 Tomcat:6.0 spring:3.2,使用到的Jar包截图:
工程结构截图:
接下来:web.xml文件配置: - <?xml version="1.0" encoding="UTF-8"?>
& d5 Y% Y) k; u7 |) u: Y$ H - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"3 t; n2 b, Z1 A$ e
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
/ }) n: H$ n4 c" O/ |! H* X - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ( M& N" m7 O! b# @) X% k7 s- ^5 N: F
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
/ `- u5 h. z2 o3 H$ V# O1 p( p - % g5 P9 ~' B0 G- y+ d* D# q9 s
- <servlet>
+ H* ~$ E! b$ A! O - <servlet-name>SpringMVC</servlet-name>
, V' o1 m( g( z - <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5 g, \9 Y& [: _, n6 p7 J - <init-param>0 h, L* B" W" \9 w! Z
- <param-name>contextConfigLocation</param-name>
% M/ S+ O% {+ z. q0 |5 y$ y - <param-value>classpath:SpringMVC-servlet.xml</param-value>
( |( X% M: u# q. N2 b/ K; N# C: b - </init-param>
: | t* f; n1 q' \ - <load-on-startup>1</load-on-startup>
: d' S5 l9 ~) l$ a9 R# y' D+ \ h - </servlet>
$ q! Z F# d. Y* E8 ]3 x/ s& Y - <servlet-mapping>3 e# Q: @, u. C3 p2 J. s
- <servlet-name>SpringMVC</servlet-name>5 i q8 g7 I! {. Q3 D: T4 b
- <url-pattern>/</url-pattern>' o* R, Z: L6 ]- _0 w( O) u& X
- </servlet-mapping>
( j( r* i9 w- c) {. ]- m! ~ -
% o$ W% H# Q" m- x: { - <!-- 编码过滤 -->
5 W6 p5 s& r- @ - <filter>
7 O: Q2 N6 S" E# q - <filter-name>encode</filter-name>
+ s: N, a, ]0 H0 a - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> w* p& @6 O4 j# i& p5 D& S& K
- <init-param>
5 J7 @% b# E: b& M - <param-name>encoding</param-name> , N1 ]% C) B" W# ]% J V- K- h6 r! d
- <param-value>UTF-8</param-value>
& I; Z+ s- E9 j: |; q& V - </init-param>
0 t8 M% P; {6 O - </filter> . D9 @* w- H- s' ?/ I i1 K7 N
- <filter-mapping> ; ^, Z. H7 H6 P. \ Y' A% |3 @
- <filter-name>encode</filter-name> : y+ A4 q/ P6 Y$ Z1 B" M4 ]3 y, U
- <url-pattern>/*</url-pattern> 3 G, `' [9 {1 p. a7 S3 b
- </filter-mapping>* t2 v9 w+ m% B _+ ~+ f9 K8 U% t
- <welcome-file-list>, `; E( g7 d# X- G. J' B, D
- <welcome-file>WEB-INF/index.jsp</welcome-file>
6 v) `8 I. k8 T - </welcome-file-list>
* |+ O$ O" { s; V/ x8 | - </web-app>
复制代码
$ L- }! p9 ?" G4 A8 A3 a8 O- i6 Ilog4j.properties文件配置:
- log4j.rootLogger=WARN,stdout
& T; ^ u7 v9 B5 t# M2 Z: m
( }9 L7 n% A; ` C& }( E- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
' J) _/ Y R3 y4 T4 d9 r: m9 y+ t - ) ?, ~8 D4 E$ p5 s0 E
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
1 b* d, N$ j& B8 P4 b- z
2 ]& k- _# f U# x- log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
复制代码
- U* C& u( S1 a+ O1 X( J# x' t- |3 X( rSpringMVC-servlet.xml文件的配置:- <?xml version="1.0" encoding="UTF-8"?>9 u1 J% q- t) n6 f) a
- <beans default-lazy-init="true"
# q, q7 y: l+ N) x; o - xmlns="http://www.springframework.org/schema/beans" - L, p0 g) i& K1 `5 }4 F) Y; W; F( u
- xmlns:p="http://www.springframework.org/schema/p"
5 G# l, {5 g6 R: h1 i - xmlns:tx="" 6 ^. u. N3 ^( H* u5 C& _& e
- xmlns:aop="http://www.springframework.org/schema/aop"+ S- a3 K( C% X' S# n5 Q) h6 O5 \
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 {" z9 P6 r' _* C4 N
- xmlns:context=
# m8 s M. J, u - xmlns:mvc=! q' X. _, I: `6 d
- xsi:schemaLocation=
, ?* _2 _- L& c8 Q4 A - " http://www.springframework.org/schema/context/spring-context-3.1.xsd">/ F% S6 ~2 u! y K$ m* Z
-
+ g" `. ~* n" Q+ u0 k7 Y W" a - <!-- 注解驱动 -->3 Y" v3 w& W7 Q$ j) R# d
- <mvc:annotation-driven />
. M6 r6 {6 e- r* E( o& v& G7 y5 k - <!-- 扫描包 -->
( M4 q* J/ a/ e. S/ A# q. q - <context:component-scan base-package="com.project.controller" />
; ?$ b8 x, g, k. r- ` - <!-- ViewResolver -->
5 w9 G2 {& ~& K, b4 ^/ C% m6 F - <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">2 O d% Z9 E" g+ ]1 A. A
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
6 p+ \& R. A: `6 g$ g - <property name="prefix" value="/WEB-INF/jsp/"/>
! r! y) x+ A/ }4 s8 ]+ Q - <property name="suffix" value=".jsp"/>
2 `/ D4 S5 w) z" n4 m - </bean>
3 @0 N* b# W& F5 s* h+ d. K6 F - <!-- HandlerAdapter -->
+ k1 [8 N3 h9 l6 v9 e; W( m% H - <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
/ F9 U# D( v; @/ b- a4 j0 v - <property name="mappings">8 ~0 q1 T2 o7 F$ F9 y( _5 M; n
- <props>; P' v* K5 E1 v7 s, [
- <prop key="/index">Hello</prop>
* D3 S4 X4 B) \: Z, u z' T, n - </props>& q7 I7 k. w, N7 o
- </property>4 m+ ^/ V0 }7 z% t8 c
- </bean> -->
& ^( y' j c* ^ _$ H6 ~ - <!-- HandlerMapping -->
# w$ [7 Z7 X# @5 c( \ - <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
8 }- P0 O* F& G x- S5 r. R - <!-- HandlerMapping -->' e- l7 ]" @# K% @5 |
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>6 v# `( G5 T7 T' j& J+ C
- <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->; o& o2 y: H* d \
- <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->, |: a& P- W. a: I( |9 W
- </beans>
复制代码 + h- r! u3 f, e6 r1 T
HelloController代码内容:- package com.project.controller;
, Q1 A, V" }. W$ a9 m1 f9 }* u* M0 T - import javax.servlet.http.HttpServletRequest;$ C2 L1 K6 j# h/ {9 P$ v/ b
- import javax.servlet.http.HttpServletResponse;6 A+ L9 L% `/ ^- [7 o1 I
- import org.springframework.stereotype.Controller;
4 c& F; Y3 ~, q' a+ S2 V - import org.springframework.web.bind.annotation.RequestMapping;
/ H/ z+ L# w$ u, @ - import org.springframework.web.servlet.ModelAndView;
; L! E* ~' r4 h& K# [7 ~- w - @Controller
+ b! ?& C Z6 r# Z8 q$ N1 M - @RequestMapping("/hello")$ N* ]7 b3 T Y% G* W
- public class HelloController {0 f& ~2 X7 {! Y" T
- @RequestMapping("/index")
/ D/ O3 w7 }7 D - public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
! B* v" _- R5 I% ] - ModelAndView modelView = new ModelAndView();: Y0 e/ J0 x& b1 j4 u8 ~
- modelView.addObject("msg","Hello Index");
# c! _, X9 C) d1 M4 z- q) K - modelView.setViewName("index");
0 D* r$ k1 o* ^: w - System.err.println("执行index!");3 o6 m. o# o) A, m" @) Q
- return modelView;
, J3 x6 t) W/ Q( {8 j a! G1 Z - }0 U* I7 `+ K5 ^% e
- @RequestMapping("/show")8 L& ?7 Y- v e$ ?8 E! Y/ ?
- public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
6 H9 h2 _! O4 [1 `' b' f - ModelAndView modelView = new ModelAndView();! e- A5 r( ]0 L! t9 b/ ^
- modelView.addObject("msg","Hello show");
& [2 W! d( d/ n4 V R+ L; R% f2 ~ - modelView.setViewName("index");; r3 u! u7 f' o! `
- System.err.println("执行show!");
- S, ^1 m9 d( f( Y' e. o" ] - return modelView;
% F+ Q' ^+ V+ E, F - }
- {+ S8 e' Y! W" H& T - }
复制代码 + ^' t1 z$ b- e$ a# p& x
OK,运行效果如图:
" H2 K% S1 Q0 C- J; {! R
+ J* x$ a: l9 D* V6 |8 C( B# S |