标签:
下一步:
下一步: 选择 maven-archetype-webapp
下一步,设置 Group ID, Artifact Id 以及 package 包名。
点击 Finish 完成。
可以看到有一些错误。
解决办法:
1. 在 Project Explorer 中的项目上点击右键,在弹出菜单上点击 "Properties" , 在属性对话框中,定位到 Java Build Path 中,切换到 Source 页,删除不需要项。
<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>yangyxd</groupId> <artifactId>maven.springmvc.web</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>maven.springmvc.web Maven Webapp</name> <url>http://maven.apache.org</url> <!-- 定义依赖库的版本变量 --> <properties> <commons-lang.version>2.6</commons-lang.version> <slf4j.version>1.7.6</slf4j.version> <!-- spring 版本号 --> <spring.version>4.3.3.RELEASE</spring.version> </properties> <!-- 依赖库定义 --> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>${commons-lang.version}</version> </dependency> <!-- 防止 jsp 报错 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- JSTL 定制标签库 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Jakarta Taglibs JSP定制标签库 --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- 各种集合类和集合工具类 --> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <!-- Spring Begin --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- Spring End --> <!-- Maven Compiler Plugins --> <!-- <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <type>maven-plugin</type> </dependency> --> </dependencies> <build> <plugins> <!-- maven 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <!-- 源代码使用的开发版本 --> <source>1.8</source> <!-- 需要生成的目标class文件的编译版本 --> <target>1.8</target> <!-- 一般而言,target与source是保持一致的,但是,有时候为了让程序能在其他版本的jdk中运行 (对于低版本目标jdk,源代码中需要没有使用低版本jdk中不支持的语法),会存在target不同于 source的情况 --> <!-- 默认字符集编码 --> <encoding>UTF-8</encoding> <!-- 这下面的是可选项 --> <meminitial>128m</meminitial> <maxmem>512m</maxmem> <fork>true</fork> <!-- fork is enable,用于明确表示编译版本配置的可用 --> <compilerVersion>1.3</compilerVersion> <!-- 这个选项用来传递编译器自身不包含但是却支持的参数选项 --> <compilerArgument>-verbose -bootclasspath ${java.home}\lib\rt.jar</compilerArgument> </configuration> </plugin> </plugins> <!-- 最终的项目名称 --> <finalName>maven.springmvc.web</finalName> </build> </project>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!-- Spring MVC Config Begin --> <servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <!-- mvc-dispathcer 拦截 html 访问请求 --> <!-- <url-pattern>*.html</url-pattern> --> <!-- mvc-dispathcer 拦截所有的请求 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Spring MVC Config End --> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
在 WEB-INF 下新建 configs 目录,建立 spring-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:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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" > <!--开启注解处理器--> <context:annotation-config /> <!-- 开启组件扫描 --> <!-- 只搜索 @Controller 标注的类,不搜索其它标注的类 --> <!-- base-package: 我们将来扫描的包名(后面会建立) --> <context:component-scan base-package="yxd.maven.springmvc.web"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 让dispertcher servlet 启用基于 annotation 的 HandlerMapping --> <mvc:annotation-driven /> <!-- 指定使用 JstlView 来获取 View --> <bean id="defaultViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:order="3"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="contentType" value="text/html"/> <!-- spring mvc 指定存放 jsp 的目录 --> <property name="prefix" value="/WEB-INF/jsps/"/> <!-- 后缀名为 .jsp --> <property name="suffix" value=".jsp"/> </bean> </beans>
在 WEB-INF 下建立文件夹 jsps,用来存放 spring mvc 中用来显示 view 的 jsp文件。
现在我们的项目变成了这样:
在我们的项目上右键,在弹出菜单中点击 "Run As" -> "Maven install",让 Maven 来初始化所有依赖库,这个时候如果某个库不存在,会自动下载。
如果执行成功,会在 Console 显示如下信息:
如果失败, 请确认环境变量(%JAVA_HOME%)设置是否正确,然后检查Eclipse设置("Preferences"\"Java"\"Installed JREs")与项目Java Build Path 中 Libraries 里面所指定的 JRE System Library 是不是一致,参考maven环境建立的文章。
如果提示下载失败,可以在 maven 的配置中指定镜像站。
如果出现类似下面的错误,一般是maven-compiler-plugin配置有问题。
如果出现类似下面的错误,检测环境变量,这种一般就是上面说的 jre 不一致造成。
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project maven.demo: Compilation failure
[ERROR] Unable to locate the Javac Compiler in:
[ERROR] C:\Program Files\Java\jre1.8.0_101\..\lib\tools.jar
[ERROR] Please ensure you are using JDK 1.4 or above and
[ERROR] not a JRE (the com.sun.tools.javac.Main class is required).
[ERROR] In most cases you can change the location of your Java
[ERROR] installation by setting the JAVA_HOME environment variable.
[ERROR] -> [Help 1]
如果出现类似下面的错误,直接将manen仓库中的 (.m2\repository\) 下对应目录删除(比如我直接将 .m2\repository\org 目录删除了),用 Alt + F5,更新工程 (Manen -> Update Project),然后重新 "Run As" -> "Maven install" 。
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project maven.demo: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\springframework\spring-context-support\4.3.3.RELEASE\spring-context-support-4.3.3.RELEASE.jar时出错; invalid LOC header (bad signature)
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\springframework\spring-aspects\4.3.3.RELEASE\spring-aspects-4.3.3.RELEASE.jar时出错; invalid LOC header (bad signature)
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\aspectj\aspectjweaver\1.8.9\aspectjweaver-1.8.9.jar时出错; invalid LOC header (bad signature)
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\springframework\spring-oxm\4.3.3.RELEASE\spring-oxm-4.3.3.RELEASE.jar时出错; invalid LOC header (bad signature)
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\springframework\spring-jms\4.3.3.RELEASE\spring-jms-4.3.3.RELEASE.jar时出错; invalid LOC header (bad signature)
[ERROR] 错误: 读取E:\Documents\My Documents\My Projects\Java Project\Workspace\JEE\.m2\repository\org\springframework\spring-messaging\4.3.3.RELEASE\spring-messaging-4.3.3.RELEASE.jar时出错; invalid CEN header (bad signature)
[ERROR] 6 个错误
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
在 "Maven install" 成功后,我们可以试着启动一下 Tomcat 服务器。
如果出现类似下面的错误,请检查 <url-pattern> 是否少了 "/"。如果都正常,还是会出错,直接重新建立一个项目吧。
package yxd.maven.springmvc.web; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class MyFirstSpringMvcController { @RequestMapping("mvc") public String HelloSpringMvc() { return "home"; // 返回 jsp 文件名称 } }
在 jsps 目录中新建一个 home.jsp 文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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> HelloSpringMvc </body> </html>
重启 Tomcat 服务器,在浏览器中输入 http://localhost:8080/maven.springmvc.web/mvc ,可以看到成功的显示了 home.jsp 的内容。
感谢: 上上千up、小布布、小不点
标签:
原文地址:http://www.cnblogs.com/yangyxd/p/5955630.html