本教程是基于MyEclipse環(huán)境下基于maven搭建的springmvc步驟(圖文教程),,文章結(jié)尾含有源碼下載地址: 新建maven project File->New->roject 選擇Maven Project 選擇項(xiàng)目路徑 Use default Workspace location默認(rèn)工作空間 選擇項(xiàng)目類型 在Artifact Id中選擇maven-archetype-webapp 輸入Group ID和 Artifact ID,,以及Package Group ID一般寫大項(xiàng)目名稱,。Artifact ID是子項(xiàng)目名稱,。 例如Spring的web包,Group ID:org.springframework,,artifactId:spring-web,。 Package是默認(rèn)給你建一個(gè)包,不寫也可以,。 建好后如下圖: 接下來需要添加src/main/java,,src/test/java ,src/test/resources三個(gè)文件夾,。右鍵項(xiàng)目根目錄點(diǎn)擊New -> Source Folder,, 建出這三個(gè)文件夾。注意不是建普通的Folder,,而是Source Folder,。 更改文件夾顯示的順序:右擊->屬性->Order and Export 更改JDK版本:在Libraries雙擊JRE System Library,要1.6版本 更改class路徑 右鍵項(xiàng)目,,Java Build Path -> Source 下面應(yīng)該有4個(gè)文件夾,。src/main/java,src/main/resources,,src/test/java ,,src/test/resources。 雙擊每個(gè)文件夾的Output folder,,選擇路徑,。 src/main/java,src/main/resources,,選擇target/classes; src/test/java ,,src/test/resources, 選擇target/test-classes; 選上Allow output folders for source folders. 點(diǎn)擊項(xiàng)目名稱右擊,然后如圖選擇 使用Maven POM editor打開項(xiàng)目中的pom.xml文件,,選擇Dependencies,,在Dependencies欄目點(diǎn)擊Add進(jìn)行,首先彈出一個(gè)搜索按鈕,,例如輸入spring-web,就會(huì)自動(dòng)搜索關(guān)于spring-web相關(guān)的jar包,,我們選擇3.0.5版本的spring。將spring包全部添加進(jìn)來,。需要添加的其他jar包有:junit,、jstl?;蛘唿c(diǎn)擊pom.xml直接編輯pom.xml文件,。這樣可以直接copy過來dependencies內(nèi)容。 設(shè)置jar包的scope 當(dāng)添加進(jìn)入一個(gè)jar包后,,有一些屬性需要設(shè)置,,最重要的就是scope,它有以下幾種取值: 1. compile,,缺省值,,適用于所有階段,會(huì)隨著項(xiàng)目一起發(fā)布,。 2. provided,,類似compile,期望JDK,、容器或使用者會(huì)提供這個(gè)依賴,。如servlet.jar。 3. runtime,,只在運(yùn)行時(shí)使用,,如JDBC驅(qū)動(dòng),適用運(yùn)行和測(cè)試階段,。 4. test,,只在測(cè)試時(shí)使用,用于編譯和運(yùn)行測(cè)試代碼,。不會(huì)隨項(xiàng)目發(fā)布,。 5. system,類似provided,,需要顯式提供包含依賴的jar,,Maven不會(huì)在 Repository中查找它。 注意: 在使用maven 打包或者編譯時(shí)報(bào):-source1.3 中不支持注釋錯(cuò)誤解決方案如下: <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> 添加如下配置就不會(huì)再報(bào)錯(cuò)了 pom.xml文件代碼: <project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>wang.gom.mavenspringmvc</groupId> <artifactId>mavenspringmvctest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>mavenspringmvctest Maven Webapp</name> <url>http://maven.</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>javaee-api</artifactId> <version>5.0-1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-api</artifactId> <version>1.2_04</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.faces</groupId> <artifactId>jsf-impl</artifactId> <version>1.2_04</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.1.2.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.5.4</version> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.openejb</groupId> <artifactId>commons-dbcp-all</artifactId> <version>1.3-r699049</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.1.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> </dependencies> <build> <finalName>mavenspringmvctest</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.0.2</version> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> </project> web.xml文件代碼:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java./xml/ns/javaee" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://java./xml/ns/javaee version="2.5" > <!-- 區(qū)分項(xiàng)目名稱,防止默認(rèn)重名 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.example.root</param-value> </context-param> <!-- Spring的log4j監(jiān)聽器 --> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <!-- 字符集 過濾器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring view分發(fā)器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> dispatcher-servlet.xml文件代碼<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:aop="http://www./schema/aop" xmlns:context="http://www./schema/context" xmlns:mvc="http://www./schema/mvc" xmlns:tx="http://www./schema/tx" xmlns:xsi="http://www./2001/XMLSchema-instance" xsi:schemaLocation="http://www./schema/aop <mvc:annotation-driven /> <context:component-scan base-package="Controller" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans> GeneralController類代碼 package Controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class GeneralController { @RequestMapping(value="index.do") public void index_jsp(Model model){ model.addAttribute("ceshi", "測(cè)試你好"); System.out.println("index.jsp"); } } Index.jsp頁面代碼<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java./jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www./TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <cut value="${ceshi}"></cut> </body> </html> 運(yùn)行http://localhost:8080/mavenspringmvctest/index.do結(jié)果為:測(cè)試你好 mavenspringmvctest源碼下載地址:
|
|