基于MyEclipse搭建maven+springmvc图文教程(含源码)
本教程是基于MyEclipse环境下基于maven搭建的springmvc步骤(图文教程),文章结尾含有源码下载地址:新建maven projectFile->New->Project 选择Maven Project选择项目路径Use default Workspace location默认工作空间选择项目类型在Artifact Id中选择maven-archetype-webapp输入Group ID和 Artifact ID,以及PackageGroup ID一般写大项目名称。Artifact ID是子项目名称。例如Spring的web包,Group ID:org.springframework,artifactId:spring-web。Package是默认给你建一个包,不写也可以。建好后如下图:接下来需要添加src/main/java,src/test/java ,src/test/resources三个文件夹。右键项目根目录点击New -> Source Folder,建出这三个文件夹。注意不是建普通的Folder,而是Source Folder。 更改文件夹显示的顺序:右击->属性->Order and Export更改JDK版本:在Libraries双击JRE System Library,要1.6版本
更改class路径右键项目,Java Build Path -> Source下面应该有4个文件夹。src/main/java,src/main/resources,src/test/java ,src/test/resources。
双击每个文件夹的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.点击项目名称右击,然后如图选择在pom.xml中添加所需要的jar包使用Maven POM editor打开项目中的pom.xml文件,选择Dependencies,在Dependencies栏目点击Add进行,首先弹出一个搜索按钮,例如输入spring-web,就会自动搜索关于spring-web相关的jar包,我们选择3.0.5版本的spring。将spring包全部添加进来。需要添加的其他jar包有:junit、jstl。或者点击pom.xml直接编辑pom.xml文件。这样可以直接copy过来dependencies内容。
设置jar包的scope当添加进入一个jar包后,有一些属性需要设置,最重要的就是scope,它有以下几种取值:1. compile,缺省值,适用于所有阶段,会随着项目一起发布。2. provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。3. runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。4. test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。5. system,类似provided,需要显式提供包含依赖的jar,Maven不会在 Repository中查找它。
注意:在使用maven 打包或者编译时报:-source1.3 中不支持注释错误解决方案如下:<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>添加如下配置就不会再报错了
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/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.apache.org</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.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5" > <!-- 区分项目名称,防止默认重名 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>maven.example.root</param-value> </context-param> <!-- Spring的log4j监听器 --> <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分发器 --> <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.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <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; @Controllerpublic class GeneralController { @RequestMapping(value="index.do") public void index_jsp(Model model){ model.addAttribute("ceshi", "测试你好"); 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.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <c:out value="${ceshi}"></c:out> </body></html> 运行http://localhost:8080/mavenspringmvctest/index.do结果为:测试你好
mavenspringmvctest源码下载地址:
你好强大啊
页:
[1]