SpringMvc×¢½â¿ª·¢ÊµÀý½Ì³Ì ÏÂÔØ±¾ÎÄ

Spring MVC ¿ò¼Ü´î½¨¼°Ïê½â

ÏÖÔÚÖ÷Á÷µÄWeb MVC¿ò¼Ü³ýÁËStrutsÕâ¸öÖ÷Á¦ Í⣬Æä´Î¾ÍÊÇSpring MVCÁË£¬Òò´ËÕâÒ²ÊÇ×÷ΪһÃû³ÌÐòÔ±ÐèÒªÕÆÎÕµÄÖ÷Á÷¿ò¼Ü£¬¿ò¼ÜÑ¡Ôñ¶àÁË£¬Ó¦¶Ô¶à±äµÄÐèÇóºÍÒµÎñʱ£¬¿ÉʵÐеķ½°¸×ÔÈ»¾Í¶àÁË¡£²»¹ýÒªÏëÁé»îÔËÓÃSpring MVCÀ´Ó¦¶Ô´ó¶àÊýµÄWeb¿ª·¢£¬¾Í±ØÐëÒªÕÆÎÕËüµÄÅäÖü°Ô­Àí¡£

Ò»¡¢Spring MVC»·¾³´î½¨£º£¨Spring 2.5.6 + Hibernate 3.2.0£© 1. jar°üÒýÈë

Spring 2.5.6£ºspring.jar¡¢spring-webmvc.jar¡¢commons-logging.jar¡¢cglib-nodep-2.1_3.jar

Hibernate 3.6.8£ºhibernate3.jar¡¢hibernate-jpa-2.0-api-1.0.1.Final.jar¡¢antlr-2.7.6.jar¡¢commons-collections-3.1¡¢dom4j-1.6.1.jar¡¢javassist-3.12.0.GA.jar¡¢jta-1.1.jar¡¢slf4j-api-1.6.1.jar¡¢slf4j-nop-1.6.4.jar¡¢ÏàÓ¦Êý¾Ý¿âµÄÇý¶¯jar°ü 2. web.xmlÅäÖ㨲¿·Ö£©

01

02 03

04 spring

0 org.springframework.web.servlet.DispatcherServle5 t

12 1 13 14

15

16 spring

17 *.do 18 19 20 21

22

23 24

2 org.springframework.web.context.ContextLoaderLi5 stener 26 27 28

29 30

3

contextConfigLocation 1

3 classpath:config/applicationContext.xml

33

3. spring-servlet.xmlÅäÖÃ

spring-servletÕâ¸öÃû×ÖÊÇÒòΪÉÏÃæweb.xmlÖбêÇ©ÅäµÄֵΪspring£¨spring£©£¬ÔÙ¼ÓÉÏ¡°-servlet¡±ºó׺¶øÐγɵÄspring-servlet.xmlÎļþÃû£¬Èç¹û¸ÄΪspringMVC£¬¶ÔÓ¦µÄÎļþÃûÔòΪspringMVC-servlet.xml¡£

01

02

0 xmlns:context=\4 xt\

xsi:schemaLocation=\05

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop 06

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 0 http://www.springframework.org/schema/tx

7 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context

0\09

10 11 12

13

1 15

16

1

1

9

2

4. applicationContext.xmlÅäÖÃ

01

02

http://www.springframework.org/schema/beans 07

http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop 08

http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx 09

http://www.springframework.org/schema/tx/spring-tx-2.5.xsd\10

11

1

13

14 classpath:config/hibernate.cfg.xml 15 16 17

18

1 23 24

25

2

28

29

31

32

3 34 35

¶þ¡¢Ïê½â

Spring MVCÓëStruts´ÓÔ­ÀíÉϺÜÏàËÆ£¨¶¼ÊÇ»ùÓÚMVC¼Ü¹¹£©£¬¶¼ÓÐÒ»¸ö¿ØÖÆÒ³ÃæÇëÇóµÄServlet£¬´¦ÀíÍêºóÌø×ªÒ³Ãæ¡£¿´ÈçÏ´úÂ루ע½â£©£º

01 package controller; 02

03 import javax.servlet.http.HttpServletRequest;

04

05 import org.springframework.stereotype.Controller;

06 import org.springframework.web.bind.annotation.RequestMapping; 07 import org.springframework.web.bind.annotation.RequestParam; 08

09 import entity.User; 10

11 @Controller //ÀàËÆStrutsµÄAction 12 public class TestController { 13

@RequestMapping(\ÇëÇóurlµØÖ·Ó³É䣬ÀàËÆ14

StrutsµÄaction-mapping publicString testLogin(@RequestParam(value=\15

username, String password, HttpServletRequest request) { // @RequestParamÊÇÖ¸ÇëÇóurlµØÖ·Ó³ÉäÖбØÐ뺬ÓеIJÎÊý(³ý·ÇÊô16

ÐÔrequired=false) 17 // @RequestParam¿É¼òдΪ£º@RequestParam(\18

if (!\|| !\19 { return\Ìø×ªÒ³ÃæÂ·¾¶£¨Ä¬ÈÏΪת·¢£©£¬¸Ã·20

¾¶²»ÐèÒª°üº¬spring-servletÅäÖÃÎļþÖÐÅäÖõÄǰ׺ºÍºó׺ 21 }

22 return \23 } 24

25 @RequestMapping(\

public ModelAndView testLogin2(String username, String 26

password, int age){ // requestºÍresponse²»±Ø·ÇÒª³öÏÖÔÚ·½·¨ÖУ¬Èç¹ûÓò»Éϵϰ¿É27

ÒÔÈ¥µô // ²ÎÊýµÄÃû³ÆÊÇÓëÒ³Ãæ¿Ø¼þµÄnameÏàÆ¥Å䣬²ÎÊýÀàÐÍ»á×Ô¶¯±»×ª28 »» 29

if (!\30

|| age < 5) { return newModelAndView(\ÊÖ¶¯ÊµÀý»¯31

ModelAndViewÍê³ÉÌø×ªÒ³Ãæ£¨×ª·¢£©£¬Ð§¹ûµÈͬÓÚÉÏÃæµÄ·½·¨·µ»Ø×Ö·û´®