eclipse的maven 搭建spring mvc
阿新 • • 發佈:2018-11-07
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。