在MyEclipse中搭建Spring MVC開發環境

環境版本java

Windows 8.1
IDE:MyEclipse 8.5
Spring:spring 3.2.8
JDK:1.6web

1.打開MyEclipse-->File-->New-->Web Project,在打開的對話框裏面輸入project Name爲SpringMvc,點擊Finish。以下圖所示:spring

image

2.在新建項目上右鍵選擇,properties-->Java Build Path-->Libraries-->Add External JARs,引入spring-framework-3.2.8.RELEASE-dist目錄下幾個必需的jar包.spring-mvc

image

3.文件結構mvc

image

4.web.xml配置:app

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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">

       <servlet>
              <servlet-name>springmvc</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       </servlet>      

       <servlet-mapping>
              <servlet-name>springmvc</servlet-name>
              <!-- 監聽全部請求 -->
              <url-pattern>/</url-pattern>
       </servlet-mapping>
</web-app>

5.springmvc-servlet.xml配置:jsp

<?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/mvc
              http://www.springframework.org/schema/mvc/spring-mvc-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">

       <!-- 掃描全部的 controller -->
       <context:component-scan base-package="com.springmvc.controllers" />

       <!-- 啓動註解驅動 SpringMVC 功能 -->
       <mvc:annotation-driven />

       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <property name="prefix" value="/views/" />
              <property name="suffix" value=".jsp" />
       </bean>
</beans>

6.在WebRoot目錄下,新建views文件夾,添加index.jsp.ui

image

7.在com.springmvc.controllers包下,新建HomeController類文件,url

image

8.部署項目運行,訪問http://localhost:8080/SpringMvc/home,以下圖示:spa

image

相關文章
相關標籤/搜索