去年年初開始使用Spring MVC架構開發管理工具。但是只使用了半年就去做非開發類項目了。現在又開始需要使用Spring MVC架構開發應用。由於之前應用的開發平台都是由前輩搭建好的,我只需要往裡面增加自己的代碼就可以了,那是根本沒有對Spring MVC架構有足夠的瞭解。為了能更清楚Spring MVC本身,我計劃從開開始學習這個架構,為以後的使用提供保證。
我使用的Spring版本是3.1.1,JDK1.6,Eclipse 3.4和Maven 3.0.3。下面一步一步描述如何完成一個Helloworld程式的開發。
1、在Ecplise中建立一個簡單的Maven項目,項目的目錄結構是:
650) this.width=650;" src="http://www.bkjia.com/uploads/allimg/131228/141J31C7-0.jpg" title="maven.jpg" alt="224558842.jpg" />
在建立完maven項目時,webapp下面是空的,src/main/java下面也是空的。記得在建立maven項目的時候要選擇合適的Packaging,要選擇war而不是預設的jar。
2、建立如目錄。
3、對於web.xml檔案,內容為:
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Mince</display-name> <context-param> <param-name>webAppRootKey</param-name> <param-value>mince.idm</param-value> </context-param> <servlet> <servlet-name>Mince</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/mince-servlet.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Mince</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Spring MVC中所有請求都需要經過DispatcherServlet轉寄,這是Spring MVC架構的核心內容。
3、在web.xml指定了一個檔案/WEB-INF/spring/mince-servlet.xml。因此需要建立一個檔案mince-servlet.xml,其內容為:
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"> <context:component-scan base-package="com.bizenit.idm.admin.web" /> <mvc:annotation-driven/> <mvc:view-controller path="/" view-name="index"/> <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
21行表示項目中需要載入的類的包名。23行表示使用註解的方式匹配請求與Controller的關係。通過這2句,當使用者訪問該項目中的URL時,會從包com.bizenit.idm.admin.web
下找帶有@Controller註解的類,將使用者的URL匹配該類中的各個方法。從而獲得請求需要的資源。25行的配置表示當使用者訪問項目根地址的時候,將顯示index頁面。
4、建立2個簡單的JSP頁面:index.jsp和world.jsp。
5、如何顯示world.jsp頁面呢?這就需要類HelloWorld.java其作用了:
package com.bizenit.idm.admin.web;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controller@RequestMapping(value = "/hello")public class HelloWorld { @RequestMapping(value = "/world",method = RequestMethod.GET) public String viewWorld(Model model) { return "hello/world"; }}
6、使用者訪問http://localhost:8080/Mince時顯示index.jsp頁面;使用者訪問http://localhost:8080/Mince/hello/world時顯示world.jsp頁面。
7、另一個很重要的檔案pom.xml的內容是:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Mince</groupId> <artifactId>Mince</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <webappDirectory>${project.build.directory}/${project.build.finalName}</webappDirectory> <spring.version>3.1.1.RELEASE</spring.version> <hibernate.version>3.6.7.Final</hibernate.version> <aspectj.version>1.5.4</aspectj.version> <quartz.version>2.1.3</quartz.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> <exclusions> <exclusion> <artifactId>spring-expression</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> <exclusions> <exclusion> <artifactId>spring-expression</artifactId> <groupId>org.springframework</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.5</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>${hibernate.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.0-api</artifactId> <version>1.0.1.Final</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> <version>${hibernate.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspectj.version}</version> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectj.version}</version> </dependency> </dependencies> <build> <pluginManagement> <plugins> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat6-maven-plugin</artifactId> <version>2.0</version> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.0</version> </plugin> </plugins> </pluginManagement> </build></project>
對於每一個Maven項目都有唯一的pom.xml。對於簡化jar包的管理,Maven是首先的方式。
註:由於本人對Spring MVC也不熟悉,如果上述內容有任何出入,請諒解和指正。謝謝。
本文出自 “圖圖學習吧” 部落格,請務必保留此出處http://feiquan16.blog.51cto.com/336861/1304086