SpringMvc注解开发实例教程 下载本文

内容发布更新时间 : 2024/5/4 16:21:49星期一 下面是文章的全部内容请认真阅读。

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地址映射中必须含有的参数(除非属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完成跳转页面(转发),效果等同于上面的方法返回字符串