首先说明一下使用的版本: JDK:1.6 Tomcat:6.0 spring:3.2,使用到的Jar包截图:
工程结构截图:
接下来:web.xml文件配置: - <?xml version="1.0" encoding="UTF-8"?>
- x+ i) G; Y8 e+ f - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"7 P5 I/ C5 Y0 @% ]
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 h4 I) T7 x" L7 B6 r0 B - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 h1 f/ ]0 U, I6 G; U - http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">& K2 s6 J+ {" G: p3 s
-
& ]) {7 N! E" B' G1 q$ k1 Z - <servlet>
# N/ G0 J# g9 V8 O' `8 x - <servlet-name>SpringMVC</servlet-name>0 `& H/ l0 i$ D8 L( Z) @( t
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>$ E7 ?2 V- q2 P
- <init-param>
, E, }; c6 j, c - <param-name>contextConfigLocation</param-name>
0 ~9 j2 s2 B- s# n4 M {) K* i7 } - <param-value>classpath:SpringMVC-servlet.xml</param-value>
1 W6 G# ]# I) S3 n6 X9 ~ - </init-param>2 u# ^- W& B' M, U6 w0 Z4 k W( P
- <load-on-startup>1</load-on-startup>$ S; q+ h% K* i
- </servlet>$ S v& S# e H. n
- <servlet-mapping>
( X+ W7 k5 u/ `1 O0 z - <servlet-name>SpringMVC</servlet-name>
1 a& K" v/ V8 r9 p - <url-pattern>/</url-pattern>; q M" }3 D2 w1 B
- </servlet-mapping>( p3 t( F5 J0 d9 I
- ( q9 \5 H8 k. M4 {
- <!-- 编码过滤 -->
9 P+ d, Q& y7 W( Q - <filter>
, s0 B2 v" y# s$ E) z m7 p; i) b - <filter-name>encode</filter-name> " V) S; l6 J* B2 X9 u8 s2 W6 I" G
- <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
" S. g& s5 ]& `: y1 P - <init-param> ( P) h8 z/ z& [* J, H, q
- <param-name>encoding</param-name> 3 |- U$ Z# Y5 B$ S" q2 a' w
- <param-value>UTF-8</param-value>
7 c- B- h7 u/ ]7 R - </init-param> % |6 s1 h& ~% ?
- </filter>
& U3 E% Z( r- H4 j2 e& k! S- g - <filter-mapping> 2 {6 |/ m+ i" q+ t( E$ O' R
- <filter-name>encode</filter-name>
% S v! W8 q5 r9 V0 [" ` - <url-pattern>/*</url-pattern> Q! B, }$ x% L
- </filter-mapping>
% o8 _/ X5 [- { - <welcome-file-list>0 k: z$ P8 z9 G3 W' t9 o. {
- <welcome-file>WEB-INF/index.jsp</welcome-file>
6 P$ P: ]2 H, V8 N5 I4 b* E M7 | - </welcome-file-list>
8 T+ e0 E1 l9 u5 H) r - </web-app>
复制代码 / h+ l6 {4 q; \1 x4 x
log4j.properties文件配置:
- log4j.rootLogger=WARN,stdout2 c% }; l% @" k. N
4 f+ B# @4 ^2 O) \2 u+ Q+ |0 e- log4j.appender.stdout=org.apache.log4j.ConsoleAppender t T: [. w( r5 `1 |
- 8 t1 @% p! T7 G# u: M. M& H0 Y
- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout. x" v& ~% ]* G3 H
- 0 r4 `7 t5 b* j2 A V
- log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
复制代码
! S) _# y E! Q# Y2 HSpringMVC-servlet.xml文件的配置:- <?xml version="1.0" encoding="UTF-8"?>
8 @# D1 u" @& m- I6 E* O, f - <beans default-lazy-init="true"6 S. X! D+ K3 n: k( s
- xmlns="http://www.springframework.org/schema/beans"
# e& B5 \3 L2 m5 e' q! _$ |0 P - xmlns:p="http://www.springframework.org/schema/p"
' Y, o% p# D9 I- r: a! j4 a. I5 x - xmlns:tx=""
9 ~' ^+ V9 J) q) ], s* b2 F7 _ - xmlns:aop="http://www.springframework.org/schema/aop"
x" ~ P3 p! y - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
( c9 [( D0 x. g& D - xmlns:context=3 w/ e0 F- o0 z1 ~0 W) Y
- xmlns:mvc=: ]7 `1 \4 Z" [0 ]% P! G
- xsi:schemaLocation=
) v7 c' u7 \. U+ v- [5 J J - " http://www.springframework.org/schema/context/spring-context-3.1.xsd">
$ e5 _7 Z/ a+ ~/ ?1 h - 1 ^# O5 V7 K- ^$ p
- <!-- 注解驱动 -->% ]0 |* b6 z' b3 p2 q5 X
- <mvc:annotation-driven />
. m0 ?# ]$ m* e6 N$ K* f7 w# ~0 _ - <!-- 扫描包 -->6 t5 H4 s9 A6 Z0 H H/ W/ f3 Z/ ^* T
- <context:component-scan base-package="com.project.controller" />6 l4 u# B7 l0 D% U. i$ v9 U' V
- <!-- ViewResolver -->
% X9 P/ S6 P) w* A" e& p5 ~( K - <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
4 h! _2 j& W; v) i - <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>8 i/ {8 o( M3 I- |5 m: H
- <property name="prefix" value="/WEB-INF/jsp/"/>
3 n" ~$ w! C( [; S- j4 z7 I - <property name="suffix" value=".jsp"/>
$ O1 k& e3 P* w5 F* g* z4 S - </bean>
% e0 @& T& H/ L+ V9 f- \ - <!-- HandlerAdapter -->
7 H( g f) Y2 D- _9 R5 I$ }$ ` - <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
) }3 y; ?% d( ~/ S+ P& C# ~ - <property name="mappings">$ L0 S7 o, h1 B$ l; k9 w* j/ e7 T
- <props>
6 I% t8 |; \3 ?4 U$ V - <prop key="/index">Hello</prop>8 E. T' i" I' G. r3 J* j
- </props>
! V- j' A V# _ - </property>
* }7 t$ u; ]" g. e$ f! s - </bean> -->7 D( w% `9 g; G
- <!-- HandlerMapping --># f' F! l. X2 }
- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
& t, u# B7 {# T. X& v& e$ ` - <!-- HandlerMapping -->. E7 n o4 u9 E
- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>* n @. n" i- i
- <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->
% Y/ f( @- b; L4 l4 h - <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->) n& W3 A2 Q2 z" `9 D
- </beans>
复制代码
9 ~' `* U, v0 |* O1 Q* ^ WHelloController代码内容:- package com.project.controller;/ T v$ R7 U$ L6 c9 o
- import javax.servlet.http.HttpServletRequest;2 T+ j* V6 I5 U! a0 h( R
- import javax.servlet.http.HttpServletResponse;& s4 J6 p: D0 ^; T% J4 V" B# X
- import org.springframework.stereotype.Controller;
9 V3 x1 |/ l- Z - import org.springframework.web.bind.annotation.RequestMapping;
) F+ r1 w& P/ ~, _* U- N- Y - import org.springframework.web.servlet.ModelAndView;( t, E7 c; X. |: W. p: b- n
- @Controller
7 N+ C6 l/ z" O, I - @RequestMapping("/hello")
* g+ s8 \1 K" `8 R) ~$ H - public class HelloController {
" \' p1 I, K2 _9 B2 T y3 I - @RequestMapping("/index")$ @# F9 G/ @6 [: m8 B
- public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
' {3 ?2 Q: L/ c) C+ x, i9 Z7 J E - ModelAndView modelView = new ModelAndView();
; ]" o) C& {# G+ | - modelView.addObject("msg","Hello Index");
( a6 e0 V2 a7 B1 E" ~) v0 a - modelView.setViewName("index");8 ~0 l6 L8 A/ j: t
- System.err.println("执行index!");
7 Z7 F( L% u$ T4 F8 M - return modelView;
; {% B) O' H; Y! k0 B2 G9 B# j/ `# A - }* X: W; ^ }; Y8 ]+ x
- @RequestMapping("/show")! q9 C& Q. v- s7 N- z" l# k' B
- public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception { J; l* j- ^, ~: `4 x4 M9 R4 f
- ModelAndView modelView = new ModelAndView();/ [6 D, O9 f3 w; L, \
- modelView.addObject("msg","Hello show");
; M5 |/ O7 l$ E4 s4 ~( q0 ?% f9 q2 R6 d - modelView.setViewName("index");
5 k7 u" I, `5 Z# u# {# G - System.err.println("执行show!");
- ^( o6 D: y' Z. m9 j1 m - return modelView;$ h& o9 V+ S! m# |0 V! X9 H; l# G2 n
- }9 g; r2 |! S6 [& J8 b; n
- }
复制代码
6 y0 ], U( r2 IOK,运行效果如图:
& J4 G8 D" l4 r- Y+ P2 [- f
+ L4 t2 r: B+ z* B' s+ | |