如今主流的Web MVC框架除了Struts這個主力 外,其次就是Spring MVC了,所以這也是做爲一名程序員須要掌握的主流框架,框架選擇多了,應對多變的需求和業務時,可實行的方案天然就多了。不過要想靈活運用Spring MVC來應對大多數的Web開發,就必需要掌握它的配置及原理。html
1、Spring MVC環境搭建:(Spring 2.5.6 + Hibernate 3.2.0)java
1. jar包引入node
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.一、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包web
2. web.xml配置(部分)spring
-
-
- <servlet>
- <servlet-name>spring</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <!-- 能夠自定義servlet.xml配置文件的位置和名稱,默認爲WEB-INF目錄下,名稱爲[<servlet-name>]-servlet.xml,如spring-servlet.xml
- <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。api
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="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/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/context <a 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 base-package="controller"></context:component-scan>
-
-
- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
-
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/jsp/" p:suffix=".jsp" />
- </beans>
4. applicationContext.xml配置session
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="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/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
-
- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="configLocation">
- <value>classpath:config/hibernate.cfg.xml</value>
- </property>
- </bean>
-
-
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory"/>
- </property>
- </bean>
-
-
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
-
-
- <bean id="loginService" class="service.LoginService"></bean>
-
-
- <bean id="hibernateDao" class="dao.HibernateDao">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
- </beans>
2、詳解架構
Spring MVC與Struts從原理上很類似(都是基於MVC架構),都有一個控制頁面請求的Servlet,處理完後跳轉頁面。看以下代碼(註解):
- package controller;
-
- import javax.servlet.http.HttpServletRequest;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
-
- import entity.User;
-
- @Controller
- public class TestController {
-
- @RequestMapping("test/login.do")
- public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
-
-
-
- if (!"admin".equals(username) || !"admin".equals(password)) {
- return "loginError";
- }
- return "loginSuccess";
- }
-
- @RequestMapping("/test/login2.do")
- public ModelAndView testLogin2(String username, String password, int age){
-
-
-
- if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
- return new ModelAndView("loginError");
- }
- return new ModelAndView(new RedirectView("../index.jsp"));
-
-
- }
-
- @RequestMapping("/test/login3.do")
- public ModelAndView testLogin3(User user) {
-
- String username = user.getUsername();
- String password = user.getPassword();
- int age = user.getAge();
-
- if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
- return new ModelAndView("loginError");
- }
- return new ModelAndView("loginSuccess");
- }
-
- @Resource(name = "loginService")
- private LoginService loginService;
-
- @RequestMapping("/test/login4.do")
- public String testLogin4(User user) {
- if (loginService.login(user) == false) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
以上4個方法示例,是一個Controller裏含有不一樣的請求url,也能夠採用一個url訪問,經過url參數來區分訪問不一樣的方法,代碼以下:
- package controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
-
- @Controller
- @RequestMapping("/test2/login.do")
- public class TestController2 {
-
- @RequestMapping
- public String testLogin(String username, String password, int age) {
-
-
- if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
- return "loginError";
- }
- return "loginSuccess";
- }
-
- @RequestMapping(params = "method=1", method=RequestMethod.POST)
- public String testLogin2(String username, String password) {
-
-
-
- if (!"admin".equals(username) || !"admin".equals(password)) {
- return "loginError";
- }
- return "loginSuccess";
- }
-
- @RequestMapping(params = "method=2")
- public String testLogin3(String username, String password, int age) {
- if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
其實RequestMapping在Class上,可看作是父Request請求url,而RequestMapping在方法上的可看作是子Request請求url,父子請求url最終會拼起來與頁面請求url進行匹配,所以RequestMapping也能夠這麼寫:
- package controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Controller
- @RequestMapping("/test3/*")
- public class TestController3 {
-
- @RequestMapping("login.do")
- public String testLogin(String username, String password, int age) {
- if (!"admin".equals(username) || !"admin".equals(password) || age < 5) {
- return "loginError";
- }
- return "loginSuccess";
- }
- }
3、結束語
掌握以上這些Spring MVC就已經有了很好的基礎了,幾乎可應對與任何開發,在熟練掌握這些後,即可更深層次的靈活運用的技術,如多種視圖技術,例如 Jsp、Velocity、Tiles、iText 和 POI。Spring MVC框架並不知道使用的視圖,因此不會強迫您只使用 JSP 技術。
原文:http://blog.csdn.net/wangpeng047/article/details/6983027