首先说明一下使用的版本: JDK:1.6 Tomcat:6.0 spring:3.2,使用到的Jar包截图:
工程结构截图:
接下来:web.xml文件配置: - <?xml version="1.0" encoding="UTF-8"?>
- {$ R6 ]5 Y; l; e8 P# M" s - <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
0 A) H9 p k: O) w - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance": F: i% q, U% E# L
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 2 c% J0 K5 h7 j/ j9 ]
- http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
q; L* a/ x3 S% H7 U: V6 H - 3 O2 ?) n7 \% D/ }0 M' j( K" `' H; ?
- <servlet>
7 }4 y. z7 J! ~ - <servlet-name>SpringMVC</servlet-name>3 o6 s5 i5 L+ A3 k& ^
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
9 s+ ?1 T/ l! V9 v - <init-param>
% t% b7 `6 K% A0 k' Z2 z7 o - <param-name>contextConfigLocation</param-name>. o. y4 K# D; L
- <param-value>classpath:SpringMVC-servlet.xml</param-value>+ `! e5 X" Z: m/ S; C- I
- </init-param>
6 q" B4 N4 E. ` - <load-on-startup>1</load-on-startup>0 c) r+ G x N9 U
- </servlet>0 x; K( a; | |
- <servlet-mapping>6 ]2 i; @4 t4 i0 b/ G
- <servlet-name>SpringMVC</servlet-name>
4 l/ {0 C: I1 Y- Q0 R4 |; z - <url-pattern>/</url-pattern>
4 b$ a4 i+ k! ?/ n/ E p - </servlet-mapping>9 t1 e7 `% j" E: @' N, V' [+ i
-
: p& ?- W; w7 r i* G: l6 Z8 J4 f - <!-- 编码过滤 --> 3 i* J2 p2 C" c4 e b
- <filter> : v! I3 G& t+ s
- <filter-name>encode</filter-name>
6 i4 `% b% V9 b8 i" ?5 | - <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> : y% f; p) |8 r; G- b9 j
- <init-param>
, q3 ^0 f6 ~+ \- d - <param-name>encoding</param-name>
) ~* Z+ D: l1 I7 _2 b - <param-value>UTF-8</param-value>
8 I# M- G! ]7 i6 S7 q4 r0 v! T - </init-param>
, r+ y& d; ~9 V9 r( x - </filter>
0 I8 P. P9 ?* `0 A7 M - <filter-mapping>
" J v; Q% U4 ]% J- i2 B9 ^ - <filter-name>encode</filter-name>
x: ?; o" y/ I' l% u5 O+ \ s6 i - <url-pattern>/*</url-pattern>
8 F2 \7 V# n* }& ^0 ]2 [8 ^# o5 C+ z - </filter-mapping>
9 g& C. F. q, J( _ - <welcome-file-list>; p1 x2 W# N7 P1 M Q9 v4 r
- <welcome-file>WEB-INF/index.jsp</welcome-file>0 U0 W. b2 }4 h
- </welcome-file-list>
; v8 G8 X5 x% s$ F2 M - </web-app>
复制代码 4 y- H8 y; b6 w2 S
log4j.properties文件配置:
- log4j.rootLogger=WARN,stdout
5 Q- O" Y& e% x# _& j - # H' F/ X$ v- H# I" G% R. x
- log4j.appender.stdout=org.apache.log4j.ConsoleAppender0 h9 |: ^: M$ m
! v. ~, M$ r3 a! ]. J {# p5 I- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout( Z( O! s4 w j; B* w) L
2 b9 d) [, u. E/ l$ w1 P$ Y- log4j.appender.stdout.layout.ConversionPattern=%n%-d{yyyy-MM-dd HH:mm:ss}%n[%p]-[Thread: %t]-[%C.%M()]: %m%n
复制代码
, W N8 ~- W! y( CSpringMVC-servlet.xml文件的配置:- <?xml version="1.0" encoding="UTF-8"?>6 d. D) \" B! k: H1 Z- F% X
- <beans default-lazy-init="true"
/ K( \: t% A2 ?' [' N" G) Q2 j - xmlns="http://www.springframework.org/schema/beans" 5 L" T2 \. v) E* I2 B+ r
- xmlns:p="http://www.springframework.org/schema/p"; K: D5 W3 [4 u! E" f* W
- xmlns:tx=""
+ T m: q/ G. g/ i - xmlns:aop="http://www.springframework.org/schema/aop"
. o% G1 l) C8 R0 A - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
7 r9 c' C8 ^ a# k/ S% G* @ - xmlns:context=
p8 |2 p$ o0 m1 W) J* m3 k8 n' n - xmlns:mvc=/ U; B9 F# ^. ^1 [ n7 ` s
- xsi:schemaLocation= $ b0 G% y: O/ r% k8 @9 ]* y- R
- " http://www.springframework.org/schema/context/spring-context-3.1.xsd">
, y! J: o& D2 { -
7 x" t9 _( m7 A" q - <!-- 注解驱动 -->3 \. U. W1 j. |3 `9 I9 ?& ?3 p
- <mvc:annotation-driven />- n/ I- C8 ~$ |7 R/ Q' n
- <!-- 扫描包 --># `& ]1 ~. j) O, z
- <context:component-scan base-package="com.project.controller" />
8 Q9 _7 }' m# F" ]! B6 z' S - <!-- ViewResolver -->
& A; n& n) B. d7 g - <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">: Y0 w0 X! L& Z# [; @. [
- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
# ~+ e, M: P3 e% F0 C - <property name="prefix" value="/WEB-INF/jsp/"/>
- J1 R" }5 f! X - <property name="suffix" value=".jsp"/>
$ V a& l6 o0 q5 u: Z* n& q) I - </bean>
: s4 v( u+ ~/ x$ r - <!-- HandlerAdapter -->4 |6 q# C* I3 z9 u" v- r
- <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
0 C" H& w& Y5 p" Y0 U# p. h& o/ E - <property name="mappings">. m b# p; H* r3 g, X
- <props>
9 I% Y; h& b/ ]* V- e - <prop key="/index">Hello</prop>
( S; T% |7 N( O `! J4 Q M P - </props>
~/ K6 F* Z/ P( ` - </property>
+ ~" k/ m' ?: u( s4 V: K - </bean> -->( d% M; s! j7 J2 L; }3 N
- <!-- HandlerMapping --># B B, t. H& h
- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
9 A; }0 {" j% ~1 |. H6 d2 P* D - <!-- HandlerMapping -->
6 t/ U1 S7 R& Z6 [( R - <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>1 L, A% K; W2 K/ o. H# w+ A
- <!-- <bean name="/index" class="com.project.controller.HelloController"/> -->. a% X! p- O& k, O0 K6 G
- <!-- <bean id="Hello" class="com.project.controller.HelloController" /> -->' h# `& B, I9 M! {
- </beans>
复制代码
' W$ v( _! M9 {+ vHelloController代码内容:- package com.project.controller;
/ ~8 i5 K/ S: ? - import javax.servlet.http.HttpServletRequest; M$ r- H. B9 F u: ?+ D( Q' `* x
- import javax.servlet.http.HttpServletResponse;. Q% b9 t/ Q) \ k; t
- import org.springframework.stereotype.Controller;: J, B# F( m1 Q* |# H6 z
- import org.springframework.web.bind.annotation.RequestMapping;
* b6 P/ O- t" v7 g" {' P - import org.springframework.web.servlet.ModelAndView;
0 k5 s1 b" P$ y; Q6 l - @Controller3 N5 J" P. z/ L+ T$ s
- @RequestMapping("/hello") G* o% o& c, I8 I
- public class HelloController {
; z7 `9 h0 e( Y' g! { - @RequestMapping("/index"); R; S7 d/ `: R% T" ~0 E2 E/ M5 ?
- public ModelAndView index(HttpServletRequest request,HttpServletResponse response) throws Exception {
" D' W8 W A1 P+ @/ N - ModelAndView modelView = new ModelAndView();* L, d' k- F) g' }) p) z; n r
- modelView.addObject("msg","Hello Index");. ]/ @* t) f( k" A' R
- modelView.setViewName("index");. ~: y0 i: D# B0 v9 t' ~( T
- System.err.println("执行index!");) B8 o" Z2 m" C$ |7 E, P' D: u
- return modelView;
5 f4 }! n! {* I5 d - }/ P2 N" H' |3 V' O' K
- @RequestMapping("/show")
" p% I* `) e: k) S3 e3 t w2 B$ I - public ModelAndView show(HttpServletRequest request,HttpServletResponse response) throws Exception {
. I# h+ P8 c. ]0 c/ G9 i2 s4 \ - ModelAndView modelView = new ModelAndView();4 [7 | j2 J0 b0 X0 t, C, I
- modelView.addObject("msg","Hello show");* ]' T) ~6 F9 ~% r7 O9 N* }- a
- modelView.setViewName("index");9 `9 R( G# L" d
- System.err.println("执行show!");
5 p3 q% V/ ~/ ^5 P - return modelView;! _4 l m8 Y' @% J9 m
- }
) {4 W" A2 ]0 w6 S3 a$ { - }
复制代码 3 B2 { j- q$ ~; i5 t
OK,运行效果如图:
- W- L( _, W' j1 J
! X% l5 h ]- \- R- X# P |