首先说明一下使用的版本: JDK:1.6 Tomcat:6.0 spring:3.2,使用到的Jar包截图:
工程结构截图:
接下来:web.xml文件配置: - <?xml version="1.0" encoding="UTF-8"?>& f& a5 ]3 ~/ h) J7 d7 g# G
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
$ o! U- \. s4 G- B6 f - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"" G: o. D1 Z1 t6 [# K* t
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
; T9 a/ C% U# x0 t) e5 S2 d2 B - http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">4 P) j. e' }$ L
-
, W) {, M; R6 Q( u8 U) z - <servlet>! L6 d, T' W( D) E2 |: j; V
- <servlet-name>SpringMVC</servlet-name>) ]! M7 D, E0 c# R2 a
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
! F7 Z9 r0 h( s3 M# E - <init-param>
" B) m/ Q- `: Q$ G$ a: s - <param-name>contextConfigLocation</param-name>
8 ^5 J3 ~2 A5 ^/ M3 p3 k2 [6 u - <param-value>classpath:SpringMVC-servlet.xml</param-value>3 S2 Y/ c5 F. h% R
- </init-param>
2 q% `4 o# P' }: p1 _) }% r - <load-on-startup>1</load-on-startup>3 _' `- t; E7 @# z1 n( V' r
- </servlet>
; R( r! B& \% Z' A+ ^2 V! y) v - <servlet-mapping>$ G3 {6 n+ L X" W; U+ K. T
- <servlet-name>SpringMVC</servlet-name>1 n" |! D: P T0 A4 v
- <url-pattern>/</url-pattern>
& W \7 y- l+ z5 e% z - </servlet-mapping>
2 m3 W: r: B0 y U6 B; w/ Z -
9 Z% T- @- [4 ^! l1 U5 y( H! c3 V - <!-- 编码过滤 -->
7 T% `# |8 ~" t, ], f% w - <filter>
4 N+ t5 |! K3 t: T4 a - <filter-name>encode</filter-name>
, d( ^. H4 [) D- z1 H - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
) C7 B4 V; w2 ?$ c+ Y - <init-param>
5 d+ x0 n4 n% ~, ^; k6 e7 Z - <param-name>encoding</param-name>
! m/ N( o [9 c0 v" |4 | - <param-value>UTF-8</param-value>
3 E7 u% ^' l. ?1 D - </init-param>
3 x4 L# Z; n. L - </filter>
' X) G# ^9 _! B# X& ~* T - <filter-mapping> & k( b ?3 u* }$ k0 A) O" X
- <filter-name>encode</filter-name>
, E, ]3 W- x, n$ t* `0 O - <url-pattern>/*</url-pattern>
4 E$ ~# ]; i0 s! u v) q - </filter-mapping>
3 l) g4 z4 w( I# e! ^' ~ - <welcome-file-list>
# D1 Z2 D/ i& Q% f2 X - <welcome-file>WEB-INF/index.jsp</welcome-file>) u& G' S! Q d8 U/ J
- </welcome-file-list>
3 g- O$ C3 Y; @1 V - </web-app>
复制代码
7 x4 a) ~8 E3 a" B( Ulog4j.properties文件配置:
- log4j.rootLogger=WARN,stdout
7 l- ]6 V& ~/ t1 d% T6 h - ( E% A5 r; O: L! ^+ e$ q
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender: g) u4 |9 f- S$ s& r# F
- 3 p) x* C6 {3 L R
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
2 ^0 z H- J8 H# Y: [. D
6 G+ `3 G2 t- z. K: p7 q6 D7 k- log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
复制代码 , Z9 A) F$ D5 }( Q
SpringMVC-servlet.xml文件的配置:- <?xml version="1.0" encoding="UTF-8"?>
& P# L8 d }$ O0 t1 P: B) b* w - <beans default-lazy-init="true"' C% D, m7 B9 F5 K4 x$ |' V
- xmlns="http://www.springframework.org/schema/beans"
) z; s: d* E- H# i" ?% Y - xmlns:p="http://www.springframework.org/schema/p". l+ t3 q4 U3 N" _, G
- xmlns:tx="" ' T) D; w0 Z( ]/ E/ f6 z! ^2 B
- xmlns:aop="http://www.springframework.org/schema/aop"
# Q! D8 r0 t/ h, K0 B6 `3 L( ] - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ( D% C$ \2 q" w/ b; N
- xmlns:context=
- U% f, }$ m5 K - xmlns:mvc=2 G! z: d+ L5 t1 b
- xsi:schemaLocation= % P# i6 ]+ z$ T: i, Q {
- " http://www.springframework.org/schema/context/spring-context-3.1.xsd">
7 b8 \3 B8 o( P* ]5 h1 [5 p0 u# _1 l9 { -
5 n; B- k z' j4 p l! Z' s1 c" m - <!-- 注解驱动 -->
' X& b0 t* h& U6 ] - <mvc:annotation-driven />
. y8 T# x% W6 E: p; }; V$ I - <!-- 扫描包 -->
" z8 j$ ^$ Q. S' y/ b1 c5 j# g - <context:component-scan base-package="com.project.controller" />
5 q, Y3 f0 t) N7 G7 s; N- @: C4 [ - <!-- ViewResolver -->$ Y* F# P, K4 ~# w" S# r( `
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
) C E9 M! r3 n1 j1 } ^ - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>6 ]+ r2 u% b& o# a
- <property name="prefix" value="/WEB-INF/jsp/"/>" I, B- d+ v! [/ e0 ~- Z- R- a# x
- <property name="suffix" value=".jsp"/>" x( O9 \! i# w7 l+ C
- </bean>* H6 h; @0 M: ~& L# V) \0 }, \
- <!-- HandlerAdapter -->
9 a) H @" y. l1 B. v - <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
" m7 z5 d5 s j$ a4 k9 _$ V - <property name="mappings">* {# g# f8 \% K
- <props>
0 { q1 [% [" k! U5 {) m7 A1 k4 c - <prop key="/index">Hello</prop>, X( Y8 L3 O# @! p% \
- </props>
) F) @4 n8 t' o0 e8 y4 O" W - </property>
- e6 R6 a# J; @+ z) Y - </bean> -->! Q) Q O f, \' T
- <!-- HandlerMapping -->2 m7 l/ Y! E# {2 S$ s
- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>5 g3 z$ o" I% T5 ]; Q& O8 C- V
- <!-- HandlerMapping -->
7 Y2 @/ n* x9 t$ t0 V! P" m4 U) p - <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>5 G! I9 T% }* z2 c2 ~" U& D7 L' Z. y
- <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->
, R4 ]% D4 S, I) U* I% t - <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->1 W- K! q" m! ~% G5 m* ~
- </beans>
复制代码 + i2 ~1 Y- Z. A& K7 z2 T
HelloController代码内容:- package com.project.controller;
$ Y6 |4 L1 m0 g# n6 j4 [ - import javax.servlet.http.HttpServletRequest;3 v+ i8 n0 x, _! j" d* B
- import javax.servlet.http.HttpServletResponse;' \ e: a: ~/ W3 [' f
- import org.springframework.stereotype.Controller;
+ @; E! [$ F8 Y9 k! i( g0 A - import org.springframework.web.bind.annotation.RequestMapping;$ j$ T/ e" N& O6 @" O( L
- import org.springframework.web.servlet.ModelAndView;
! A, _( ]1 @9 `# k9 a% K) h - @Controller
; v% T% p9 @; \$ m# q) n - @RequestMapping("/hello")! N+ C4 M5 J0 w8 E1 I- s
- public class HelloController {
7 c& O+ E! v. g - @RequestMapping("/index")
G. l% @' X8 U3 B - public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
1 v2 t4 p% h" C4 j! e - ModelAndView modelView = new ModelAndView();9 S! n: m; h9 Q: |) n8 e* y
- modelView.addObject("msg","Hello Index");: C+ M/ Y) ?+ C2 I' V( h5 M* `
- modelView.setViewName("index");
8 b* W2 ]* F1 X) v - System.err.println("执行index!");- T' _# k0 _! [. H% t- L9 M* r
- return modelView;+ z$ u, h0 Q# \4 v
- }+ P H2 U4 W% ~8 H" V& F
- @RequestMapping("/show")# `. d$ y) f2 L6 e) ~1 ?
- public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
- ]3 ?4 |. B" Q: Q - ModelAndView modelView = new ModelAndView();
* G8 ?4 r$ Z5 T - modelView.addObject("msg","Hello show");
8 m& q& a) M9 D# f0 j; u# a5 P0 v- c - modelView.setViewName("index");6 G9 o8 _6 I. [, q' ~! y
- System.err.println("执行show!");+ w# I4 A* q6 L9 I* U* M
- return modelView;
" y9 J+ { C' Q - }
$ C* `3 ]; A5 U& L - }
复制代码 ( t- y2 B/ B/ |/ |
OK,运行效果如图:
% o4 N* [/ I& r; D4 n* i* _, W: _/ I, i
& G; R3 e* X2 J" r# w |