在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

posted @ 2016-07-01 16:06  qingyuuu【java】  阅读(3024)  评论(0编辑  收藏  举报