首先说明一下使用的版本: JDK:1.6 Tomcat:6.0 spring:3.2,使用到的Jar包截图:
工程结构截图:
接下来:web.xml文件配置: - <?xml version="1.0" encoding="UTF-8"?> q* G. }; h, }* i3 q/ i
- <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"( @" ^. b J7 P* J1 z5 D0 Q! g
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
# R: S' m9 j2 ]3 w6 A6 h, T - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
: y! Y; Y+ _! ?% F - http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
/ N ` y, y+ t: X* n -
$ K, b) |9 B7 p' }# d - <servlet>
" t- J2 C+ C" Z - <servlet-name>SpringMVC</servlet-name>3 P q1 I$ _5 H
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
# I1 a/ K8 X6 J - <init-param>
& u( o$ v; I; F - <param-name>contextConfigLocation</param-name>" {, s/ w9 J8 F
- <param-value>classpath:SpringMVC-servlet.xml</param-value>% o( z( D& f: o6 c2 m ]2 \/ E1 P r) a
- </init-param>3 L# |! x% X+ H, K
- <load-on-startup>1</load-on-startup>
2 ?; y) B6 W3 |9 L S; N e - </servlet>/ Z4 @+ {+ w6 I9 x4 X" b" _
- <servlet-mapping>
* c% i+ |' B7 d' r9 ?1 p$ U U - <servlet-name>SpringMVC</servlet-name># n# j7 r6 t4 Y( c/ [; P
- <url-pattern>/</url-pattern>
% w+ `# N c( {$ M: F - </servlet-mapping>
$ J7 L& X+ A$ }* _ -
' |" d! L1 ^, ?3 }" T - <!-- 编码过滤 --> % t, m6 n3 D. B" }" \
- <filter>
( K8 |6 I* [2 x1 o* S - <filter-name>encode</filter-name> % s# D' E3 G* F1 J0 l$ ]
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
( [: r" v. @; k( f - <init-param> 0 b6 O l; k5 J) k- _ O. p
- <param-name>encoding</param-name> " b/ |; M+ h; G/ {& C) ^0 n
- <param-value>UTF-8</param-value>
# i. u# ?8 e. H7 D) M% _ - </init-param>
! w5 C M# F& f+ r8 v* b3 c - </filter> ; L! x: s# [6 H# I- e
- <filter-mapping> 8 O. s& T9 U! }* [9 i0 `4 o
- <filter-name>encode</filter-name> ( X F- f" ^, S, T9 J/ `- I, b9 B0 k
- <url-pattern>/*</url-pattern>
# a/ X: l$ q8 M$ p - </filter-mapping>4 s, \: T, E! o* \
- <welcome-file-list>3 _8 q5 r0 _, q2 B7 {) F7 X/ w
- <welcome-file>WEB-INF/index.jsp</welcome-file>
* v9 Z2 p' |( g' Y7 l% O+ x. ]0 G - </welcome-file-list>
4 s! p1 f3 @3 `5 W! a - </web-app>
复制代码
6 P* K1 y) e8 q: O# w# O3 O" E# nlog4j.properties文件配置:
- log4j.rootLogger=WARN,stdout$ M: ^8 d5 e4 S! O7 H
Z: ?5 n; b1 G+ @- log4j.appender.stdout=org.apache.log4j.ConsoleAppender4 s: j8 g/ u# h& l1 P
( ~# `- l7 ] d t1 y- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
* x% u0 t& P6 B R2 ` - . C3 V( k8 P3 u- ?/ g# `
- log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
复制代码 : s+ W. s: ~3 _2 a: V( d
SpringMVC-servlet.xml文件的配置:- <?xml version="1.0" encoding="UTF-8"?>. L2 a1 d" N: _; \
- <beans default-lazy-init="true"
( a, S) |7 f6 ?' @$ r4 q3 w6 { - xmlns="http://www.springframework.org/schema/beans"
4 H. U) p7 f0 m) }9 P2 W+ { - xmlns:p="http://www.springframework.org/schema/p"
& X& i, x2 c% |) s5 n3 e - xmlns:tx="" - C( r3 {1 W- Q' F: o
- xmlns:aop="http://www.springframework.org/schema/aop"
5 _9 T$ n& ]! u' j9 r5 Y - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 C% Z1 `3 t8 l. C" O - xmlns:context=
$ i1 c( X* E! i2 X( |: j% w - xmlns:mvc=" I# y3 ~* X/ Z/ u' |) ^5 [* g
- xsi:schemaLocation= $ L1 @* \ f' C
- " http://www.springframework.org/schema/context/spring-context-3.1.xsd">5 v1 r3 a- i# Y% u7 q. s
- 4 X* t4 M& M% D9 O5 Z
- <!-- 注解驱动 -->
- y' X; T, u) o+ L) n - <mvc:annotation-driven />
0 u" s' l4 ^0 u0 P+ K, U - <!-- 扫描包 -->
. Y3 p( P$ t; t$ f& x% V - <context:component-scan base-package="com.project.controller" />- W2 M/ S3 x0 l t5 d" U
- <!-- ViewResolver -->
' s8 _5 p5 s( N6 ` j! w# U - <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
% | N( u, g* p! o - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>' m: i4 p, `* \% J1 x" ^/ O) Q
- <property name="prefix" value="/WEB-INF/jsp/"/>
* o1 R" J: E0 G& K - <property name="suffix" value=".jsp"/>
# q! G+ O. J( D% n, y7 f W - </bean>+ F8 e5 V! |5 y3 m6 K+ o
- <!-- HandlerAdapter -->
) F' p. F: c6 P( q( x0 D - <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">+ h$ C8 B7 w- A: _
- <property name="mappings">! M% t* l W: V% d
- <props>
0 \* N4 L0 W4 ]) Z; C7 E7 D - <prop key="/index">Hello</prop>! }: |4 e8 a( m5 u$ k
- </props>0 u, [1 R# |; R5 G
- </property>
7 q6 p7 L: Q5 V2 M) f& q0 [ - </bean> -->
3 o2 H u, f. A5 k/ O6 J - <!-- HandlerMapping -->
! k+ I" v6 m' s* _/ ~ - <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
5 }& ^! `# g! ~ - <!-- HandlerMapping -->
( }3 ^3 N. G8 W1 O: B2 O0 C - <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
3 z) H. a4 y$ Z, T* @0 W - <!-- <bean name="/index" class="com.project.controller.HelloController"/> --> m5 A" Z/ F! o
- <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->
7 v* E H) G) n( Q6 Y - </beans>
复制代码 " R$ ~4 }7 P$ O6 l" ]5 a
HelloController代码内容:- package com.project.controller;
3 b5 ~ }! J! \7 K: r - import javax.servlet.http.HttpServletRequest;8 U$ R: M! ~$ M/ Q4 g9 i- U/ {
- import javax.servlet.http.HttpServletResponse;1 A$ _. @/ L8 y. B m
- import org.springframework.stereotype.Controller;7 `8 Q3 j# W, v9 S5 s7 p
- import org.springframework.web.bind.annotation.RequestMapping;
) w0 I: Z$ T, T- g3 T6 k - import org.springframework.web.servlet.ModelAndView;8 X4 N% Y4 I. H! h5 i" J: K3 f+ W
- @Controller0 s6 }& R, d( B, O @! B! x
- @RequestMapping("/hello") q/ N& x/ j! j2 D
- public class HelloController {
! }6 \! ~! H) E( s2 Z - @RequestMapping("/index")
# W# _% Y0 o; i - public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
$ q& [5 p! w% ?. i* ^- P: S& a - ModelAndView modelView = new ModelAndView();
, U0 y5 j I6 y1 l, J0 ]- x1 d - modelView.addObject("msg","Hello Index");* I& p1 f. D( Y- g
- modelView.setViewName("index");0 }' f( ?& L: h X3 x
- System.err.println("执行index!");
% P/ k, Q8 ~2 P% Z - return modelView;
8 [$ q" ]% L! c! o: O: H" G' u5 U - }
, v% B2 L. d& U/ J - @RequestMapping("/show")
2 w& \- H7 d+ s8 Q, [$ T6 f - public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
- g8 A; M7 |+ o% H - ModelAndView modelView = new ModelAndView();
0 e9 T2 V, X1 D - modelView.addObject("msg","Hello show");2 m) m$ i& F( O7 h8 H8 l: j( e8 Z
- modelView.setViewName("index");1 r1 A6 Q8 ]* }7 Z9 B) ]
- System.err.println("执行show!");* A# L5 `5 d9 _ l; A) I( E& f
- return modelView;/ w! ]5 @0 E& ]* _9 a4 g# m2 L
- }
J. i" i7 a% J3 M5 Q9 d - }
复制代码
( x" m/ n9 _" _) {: N5 u% ^OK,运行效果如图:
: ^: S: T* c; [( N
2 j$ p Z6 R2 p7 I
|