如今主流的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配置(部分)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
<!--
<!--
< servlet >
< servlet-name >spring</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
<!--
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
-->
< load-on-startup >1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >spring</ servlet-name >
< url-pattern >*.do</ url-pattern >
</ servlet-mapping >
<!--
<!--
< listener >
< listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class >
</ listener >
<!--
< context-param >
< param-name >contextConfigLocation</ param-name >
< param-value >classpath:config/applicationContext.xml</ param-value >
</ context-param >
|
3. spring-servlet.xml配置
spring-servlet这个名字是由于上面web.xml中<servlet-name>标签配的值为spring(<servlet-name>spring</servlet-name>)。再加上“-servlet”后缀而形成的spring-servlet.xml文件名称。假设改为springMVC,相应的文件名称则为springMVC-servlet.xml。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?
xml
= "1.0"
= "UTF-8" ?>
< beans
= "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
= "http://www.springframework.org/schema/p" xmlns:context = "http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/context href = "http://www.springframework.org/schema/context/spring-context-3.0.xsd" >http://www.springframework.org/schema/context/spring-context-3.0.xsd</ a >">
<!--
< context:annotation-config
<!--
< context:component-scan
= "controller" ></ context:component-scan >
<!--
< bean
= "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"
<!--
< bean
= "org.springframework.web.servlet.view.InternalResourceViewResolver"
= "/jsp/"
= ".jsp"
</ beans >
|
4. applicationContext.xml配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?
xml
= "1.0"
= "UTF-8" ?
< beans
= "http://www.springframework.org/schema/beans"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop = "http://www.springframework.org/schema/aop"
xmlns:tx = "http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/tx
<!--
< bean
= "sessionFactory"
= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
< property
= "configLocation" >
< value >classpath:config/hibernate.cfg.xml</ value >
</ property >
</ bean >
<!--
< bean
= "transactionManager"
= "org.springframework.orm.hibernate3.HibernateTransactionManager" >
< property
= "sessionFactory" >
< ref
= "sessionFactory" />
</ property >
</ bean >
<!--
< tx:annotation-driven
= "transactionManager"
= "true" />
<!--
< bean
= "loginService"
= "service.LoginService" ></ bean >
<!--
< bean
= "hibernateDao"
= "dao.HibernateDao" >
< property
= "sessionFactory"
= "sessionFactory" ></ property >
</ bean >
</ beans >
|
二、具体解释
Spring MVC与Struts从原理上非常相似(都是基于MVC架构),都有一个控制页面请求的Servlet,处理完后跳转页面。看例如以下代码(注解):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package
import
import
import
import
import
@Controller
public
@RequestMapping ( "test/login.do" ) //
public
@RequestParam (value= "username" )String
//
//
if
"admin" .equals(username) "admin" .equals(password))
return
; //
}
return
;
}
@RequestMapping ( "/test/login2.do" )
public
int
//
//
if
"admin" .equals(username) "admin" .equals(password) 5 )
return
"loginError" ); //
}
return
new
"../index.jsp" )); //
//
//
}
@RequestMapping ( "/test/login3.do" )
public
//
String
String
int
if
"admin" .equals(username) "admin" .equals(password) 5 )
return
"loginError" );
}
return
"loginSuccess" );
}
@Resource (name "loginService" ) //
private
//等价于spring传统注入方式写get和set方法,这种优点是简洁工整,省去了不必要得代码
@RequestMapping ( "/test/login4.do" )
public
if
false )
return
;
}
return
;
}
}
|
以上4个方法演示样例,是一个Controller里含有不同的请求url,也能够採用一个url訪问。通过url參数来区分訪问不同的方法,代码例如以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package
import
import
import
@Controller
@RequestMapping ( "/test2/login.do" ) //
public
@RequestMapping
public
int
//
if
"admin" .equals(username) "admin" .equals(password) 5 )
return
;
}
return
;
}
@RequestMapping (params "method=1" ,
public
//
//
if
"admin" .equals(username) "admin" .equals(password))
return
;
}
return
;
}
@RequestMapping (params "method=2" )
public
int
if
"admin" .equals(username) "admin" .equals(password) 5 )
return
;
}
return
;
}
}
|
事实上RequestMapping在Class上。可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url。父子请求url终于会拼起来与页面请求url进行匹配,因此RequestMapping也能够这么写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package
import
import
@Controller
@RequestMapping ( "/test3/*" ) //
public
@RequestMapping ( "login.do" ) //
public
int
if
"admin" .equals(username) "admin" .equals(password) 5 )
return
;
}
return
;
}
}
|
三、结束语
掌握以上这些Spring MVC就已经有了非常好的基础了,差点儿可应对与不论什么开发。在熟练掌握这些后,便可更深层次的灵活运用的技术,如多种视图技术,比如 Jsp、Velocity、Tiles、iText 和 POI。Spring MVC框架并不知道使用的视图,所以不会强迫您仅仅使用 JSP 技术。