eclipse的maven 搭建spring mvc
1.构建maven工程,new–>other -->maven project --> 勾选 create a simple project -->填写项目信息 (packaging 选择war )
2. 等待项目的搭建。
3.pom.xml 报错,修改 pom.xml 。
在 中 添加
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
- java resource 报错,点开子菜单,无错。
4.1 build path --> configure build path ,删除jre 运行环境,添加新jre ,Java 6 及以上
右击项目,选择propertie。
4.2 java compiler 选择1.7 。(1.6 不知道是否满足,可以尝试)
在出现的内容中,
Java 1.5 —> 1.7
dynamic web module 选择 3.0 ,
右侧的 runtime 页,勾选 tomcat
4.3 右击项目
maven --update project
(此时 红叉 应该消失了)
5. src/main/webapp 下 新建 WEB-INF文件夹以及 在WEB-INF 下新建 web.xml
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<!--configure the setting of springmvcDispatcherServlet and configure the mapping-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<!-- <load-on-startup>1</load-on-startup> -->
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意 /WEB-INF/springmvc-servlet.xml
6. 在web-inf 下新建 springmvc-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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- scan the package and the sub package -->
<context:component-scan base-package="spring"/>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<!-- configure the InternalResourceViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
注意 <context:component-scan base-package=“spring”/> 是你的代码内容包
- 在 src/main/java 下建包,建 java 类,Test
package spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/mvc")
public class Test {
@RequestMapping("/hello")
public String hello(){
return "hello";
}
}
- 在 WEB-INF 下新建 jsp 文件夹,以及在该文件下新建 hello.jsp (文件名 与 Test 类方法的返回值 一致。)
- tomcat 添加项目。 启动tomcat ,访问http://localhost:8080/springmvc/mvc/hello
(springmvc 是项目名,mvc 是Test类映射 /hello 是hello方法映射 ) 可以正常显示 hello.jsp 的内容。
至此 完成 demo。