在MyEclipse中搭建Spring MVC開發環境


1.打開MyEclipse,選擇New-->Web Project,在打開的對話框里面輸入Project name為SpringMVC,點擊next,選擇自動生成web.xml文件,點擊Finish。如下圖所示:

2.在新建項目上右鍵選擇,Build Path-->Configure Build Path-->Libraries-->Add External JARs,引入spring包。

3.文件結構

image

4.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">
      <!-- 配置SpringMVC -->
    <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配置:

<?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.

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

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

image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM