You are on page 1of 24

Spring MVC

September 2007  



   !"#$!%&
|   


    .

.

.

.

 .

  )ðispatcherServlet )Request flow in application )Meaning of Important terms )HandlerMapping  ** +.

 .

   .    .

 .

 * .

 .

 .

   -.

 . .

 . .

  .

 .   )Interceptors  .

 +.

.

  $/* .

 .

 .

  .

.

 0  +&1 .

    .

  ' |     .

(    .

.

.

 !"#$!%&  .

 .

   .

  . )Controllers  .    .

  3* .

" * .

)Spring tag library  Π 4   - 3   5.

 )ViewResolver )Spring I18n   .

  ' |     .

(    .

.

.

 !"#$!%& 2 .

#  .

3.

.

xml Œ. It is Front controller for Spring web mvc Configuration in web.

.

4 Œ.

.

*.

4 7.

.  Π.

.

*.

4 Œ.

.

*. 4  ( .

0 8(0.

(.

.

(#  .

3.

.

Π.

.

4 <init-param> <param-name>namespace</param-name> <param-value>XmlWebBeanFactory</param-value> </init-param> Π   4Π    4 Π.

.

4 Œ.

.

* 4 Œ.

.

*.

4 7.

.  Π.

.

*.

htm</url-pattern> Π.4 <url-pattern>*.

.

* 4   .

  ' |     .

(    .

.

.

 !"#$!%& 6 .

.

:.

 .0       .

  ' |     .

(    .

.

.

 !"#$!%& 9 .

.

*   . .

  * . * ð   .

 0 .

.

.

.

:.

  #  .

3.

.

.

 .

 1    .

0.

.

 +&   .

5.

      ð    .

.

      .

.

 = .

*.   ( .

0 8(0.

(.

.

(*( .

   .

.

  .    .

 * .

*.

    .

..

  .

  3   .

 .

   .

 . .

 <.

0 #.

 1 ..

( % 5.

   .

 .

. #  .

3.

.

 .

 .

0   .

  ' |     .

(    .

.

.

 !"#$!%&  .

.

*   . .

*  .

   <.

0.

.

.

  #  .

3.

.

 . .

  .

<. .

0  0 .

 .

.

 .0  .

. <.

0.

.

  *  .

0 *.

0  .

0 ð     ð .

             % 5.

  .

 .

 . .

 .

 .

0  ( . *.

0 8(0.

(.

.

(<.

0    .

. .

.

** .

<.

0 * .

*.

   . 3.  ( . *.

0 8(0.

(.

.

(.

0( <.

0   .

  ' |     .

(    .

.

.

 !"#$!%& = .

1 .

  ? ** +.

1 .

  Commonly used HandlerMappings ï ð  .

 BeanNameUrlHandlerMapping    .

 +& based - .

 #  ..

3.

.

.

on bean name  * .

*.

   SimpleUrlHandlerMapping    .

 +& based on Property   .

  ' |     .

(    .

.

.

 !"#$!%& > .

1 .

  ? .   For BeanNameUrlHandlerMapping Œ.

 name="/security/login.htm" CD*( (**(.

  (0.

( 7.

 .

 .

 .

:.

       .

(& .

D4  ? 000( (*?>>  A*/s Π .

 *.

CD. *<.

htm .0D ecurity/login.

CDD 4 #  .

.

.

0    .

:.

  Π .

 *.

CD.

<.

0D & .

.

CD*.

D 4 Π .

 *.

CD   D Imp note .

CD<   D 4 3..

-.

!*.

+ 1 .

   .

.          Π.

4 .

.

   .

..

 B& .

 .   7.

    +&1 .

 .

      .

  ' |     .

(    .

.

.

 !"#$!%& @ .

1 .

   For SimpleUrlHandlerMapping URL HandlerMapping Œ.   ? .

  CD .

  .

+  D %  .

*. *  *  +&  CD ( .

0 8(0.

(.

.

 .

.

 .  .

 ( .

(3* .

+ 1 .

 D4 <property name="mappings"> +&     8.

 0.

.

    <props> <.

<prop key="/logout.htm">logoutController</prop> 1.

.

 ( * E(.

( .

F  * .

 </props> .

 0     .

E(.

( .

F </property> Π.

4 Controller definition Œ.

 id="logoutController" CD*( (**(.

  (0.

( .

(&   .

D4 Π .

 *.

CD <.

0D .

CD.

   ( *D 4 Π.

4   .

  ' |     .

(    .

.

.

 !"#$!%&  .

1 .

BeanN "  .   ? Registering More than one HandlerMapping Need <bean id="publicUrlMapping" class="org.springframework.servlet.handler.web.

   0 .

.

*  ameUrlHandlerMapping">  .  .

<property name="order"> 1.

.

0.

.

..

  .

*  <value>0</value> </property> 7   .

  .

  G.

Π.

servlet.h andler.springframework.web.4 <bean id="restrictedUrlMapping" Value of order property Ļ priority Ĺ class="org.SimpleUrlHandlerMappingD4 Œ  .

 *.

CD* D4 .

.

.

 .

..

0   +   Π 4  .

    Π 8.

CD  ( *D4  .

Π 4 Π 4 Π .

4 <property name="order"> <value>1</value> </property> Π.

4   .

  ' |     .

(    .

.

.

 !"#$!%&  .

1 .

  ? -.

 .  .

 H "  .

    .

    * * .

  . .

 * .

.

..

.

 .

. *  .

H %     -.

!*.

+ 1 .

  .

  .

  +& *    *  .

. /* .

 2H #.

..

  .

*  . 0.

    .

    .

  ' |     .

(    .

.

.

 !"#$!%&  .

 .

.

 .   ? .

3  .  .

 .

/ .

*.

 .

.   0. .

 0.

0    *.

 .

.  . .

  .

 .

:.

  1 .

 .

.

 .

 .

:.

 .

 ..

 . .

   .

 .

.

 .

:.

   .

  ' |     .

(    .

.

.

 !"#$!%& 2 .

 .

.

  ? +.

.

 H  +&  .

. .

   .

.

 +& .     .

((  .

  0 . .

.

 .

  .

+& 0 .

.

.

  . .

   .

((( &   .

.

0.

.

.

 .

.8 .

   .

.

   .

  .

 H 7.

.

.

   .

*.

*   . *  .

 .

 .

 0.

 .

 .

 .

:.

  .

.

.

((( .

 .  . .

  .

&.

 .

.

..

 .

 *   .  % .

.

.

 .

.  .

 .

 .

  .

2H  .

 .

.

/ .

.

.

% 8 . .

.

  *   .

0.

    *.

    * .

 0.

.

.

 .

.8 .

:.

  .

.

  .

   .

  ' |     .

(    .

.

.

 !"#$!%& 6 .

 .

.

  ? $/* .

If you want to create your own interceptor   .

 .

/ .

    .

 .

1 .

E1 3.

.

.

:.

 .

:.

  1 .

 .

.

   .

 * .

*.

 1 3.

.

.

 .

.

 .

 5.

 1 .

 .

.

  .

F  0 $/.

 I When Interceptor handle request? 1 3.

 .

 C .

:.

 (.

3.

E..

FJ   .

    .  .

 . E.

 CC F I .

:.

 .

 ..

.  .

   .

 .

 .

.

 .

 .

(.

 .

 .

 E .

:.

 (.

 .

/ . EF K D D K (.

0& "  + .

FJ .

.   .

J L .

.

I .

  .

$/  .

 .

 .

  .

J L L   .

  ' |     .

(    .

.

.

 !"#$!%& 9 .

 .

.

  ? .

 .

  .

.

 0  +&1 .

  Œ.

*. id="localeChangeInterceptor" CD ( .

0 8(0.

(.

.

(>(&.

.

 .

.

 D4 Π.

4 Œ.

*.  CD +  D CD ( .

0 8(0.

(.

.

( .

(-.

!*.

+ 1 .

 D4 <property name="interceptors"> <list> <ref bean="localeChangeInterceptor" /> </list> </property> Π .

 *.

CD .

D4 Œ.

4Π.

4 Π .

4 Π.

4   .

  ' |     .

(    .

.

.

 !"#$!%&  .

 .

    .  ? .

  ' |     .

(    .

.

.

 !"#$!%& = .

3* .

" * .

When to use SimpleFormController 7.

 0.

.

.

 .

 * .

. * Mandatory attributes SimpleFormController **  ?   .

   ** 5.

 *<. .

0 ?  *.

. * . .

0 Some other Imp attributes .

<.

0?  *.

. .

0 . .

 * *    ? . .

    .

.. * .

  Features .  .

3.

<    . *  .    .

. -  .

5.

     8   Limitations  .

  .   ..

 * .

*.

 . ( 3   .

     .

  ' |     .

(    .

.

.

 !"#$!%& > .

3      Π 4  Purpose +.

 .

 ** 5.

  .

  .

0  ** 5.

 Attributes    .

  .

*.

    .

 .

  .

  .

 .

 .

.

((  .0   0.

 .

.

 .

*.

  .

 * 5.

 <spring: bind path="command. username"> Π  .

CD .

/ D *.

CD.

!*.

D G.

CDD .

CDŒCstatus(.

# <.

EF4D4 Œ" !%  CD .

D4 Œ-4ŒCstatus(.

$  .

.

EF4 Π-4 Π" !%4 Π ? 4   .

  ' |     .

(    .

.

.

 !"#$!%& @ .

3      ? - 3   5.

 3   .

 org.servlet.support.BindStatus 5.springframework.web.

 .

 .

 .

0  *.

   Imp methods in BindStatus object available .

# <.

EF % *.

 .

   .

**   . .

.

$  .

.

EF % *.

 .

 .

  .

0  **   .

   . .

  ' |     .

(    .

.

.

 !"#$!%&  .

<.

0.

.

? .

 .

- .

<.

0.

.

"  &  0 .

 .

  Œ..

 *.

CD .

   ( *D " & .

      . CD*( (**(.

  (0.

( .

(& 3* .

" * .

 .

D4 <property name="formView" value="login" /> 7.

.

   0 .

<property name="successView" value="home" /> <.  ..

0.

.

 B& .

   Π . .

 *.

CD   D .

CD<   D 4 7..

  .

 0  .

.

& .

Π.

4  0 .

 .

:.

   ?  ?>>  A* .

   ( * Œ.

  CD.

0.

.

D CD ( . *.

0 8(0.

(.

.

(.

0(.

  .

- .

<.

0.

.

D4 2 &  0 .

.  <.

0 5.

   .

   .

0   Π 0   .

 .

0(  .

.

*    4 Π . .

 *.

CD.

*.

D .

CD.

0D 4 Π.

4 (C ( . *.

0 8(0.

(.

.

(.

0(  <.

jsp   .url=/WEB-INF/viewshttps://www.scribd.com/jsp/common/login.0 login.

  ' |     .

(    .

.

.

 !"#$!%&  .

3  > 3   .

 8.

&.

.

.

0 0 .

 .

   .

 .

servlet.CookieLocaleResolver" /> Œ.   <bean id="localeResolver" class="org.web. .i18n.springframework. .

  CD.

.

 .

.

 D CD ( . *.

0 8(0.

(.

.

(>(&.

.

 .

.

 D4 Π.

4 Œ.

*.  CD +  D CD ( .

0 8(0.

(.

.

( .

(-.

!*.

+ 1 .

 D4 <property name="interceptors"> <list> <ref bean="localeChangeInterceptor" /> </list> </property> Π.

4   .

  ' |     .

(    .

.

.

 !"#$!%&  .

.

.

.

).  3  By Rob Harrop Apress publications )3     By Craig Walls Manning Publications   .

  ' |     .

(    .

.

.

 !"#$!%& 2 .

panbay 7 &#7#$ 1$#M+%$3 6 31"$  +%  3$ !% &&! 3 +3 > %.

( >6=(2>6( "/ >6=(2>6(9 777(!-N(    .

   !"#$!%& |     .

    .

.

.

.