在MyEclipse2014环境下搭建SpringMVC并实现HelloWorld

新建项目:

  File — New — Dynamic Web Project — 输入项目名(这里我命名为MySpringMVC-1) — Finish。

搭建环境:

  右键项目 — MyEclipse — Project Facets[Capabilities] — Install spring Facet:
  
  在MyEclipse2014环境下搭建SpringMVC并实现HelloWorld_第1张图片
  
  点击Next:
  
  
  
  取消所有勾选,点击Finish。
  

配置web.xml:

打开WebRoot/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_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 配置 DispatcherServlet -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 该servlet在项目加载时就被创建,而不是等第一次请求时才创建 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <!-- 可以应答所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

配置SpringMVC的配置文件:

  在WebRoot/WEB-INF目录下新建一个Spring Bean Definition,命名为<servlet-name>-servlet.xml,在我项目的web.xml中,<servlet-name>为springDispatcherServlet,所以此处配置文件名为springDispatcherServlet-servlet.xml:
  
  在MyEclipse2014环境下搭建SpringMVC并实现HelloWorld_第2张图片
  
  点击Next:
  
  

  勾选context和mvc,点击Finish,然后打开springDispatcherServlet-servlet.xml,配置如下:
  
springDispatcherServlet-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.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">


    <!-- 指定要自动扫描的包 -->
    <context:component-scan base-package="com.MySpringMVC"></context:component-scan>

    <!-- 配置视图解析器: 如何把 handler 方法返回值解析为实际的物理视图 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

  其中指定com.MySpringMVC下的子包和类中的注解将会被扫描到。
  

添加控制器类:

  新建包com.MySpringMVC.controller,并在包下新建类Hello.java,代码如下:

Hello.java:

package com.MySpringMVC.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Hello {

    /**
     * 1. 使用 @RequestMapping 注解来映射请求的 URL
     * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于 InternalResourceViewResolver 视图解析器, 会做如下的解析:
     * 通过 prefix + returnVal + 后缀 这样的方式得到实际的物理视图, 然会做转发操作
     * 
     * /WEB-INF/views/success.jsp
     * 
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.println("hello world!");
        return "success";
    }
}

新建页面:

  现在我们来建立页面测试一下:
  
index.jsp: (其中超链接地址“helloworld”对应Hello.java中的“@RequestMapping(“/helloworld”)”)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">

        <title>My JSP 'index.jsp' starting page</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>

      <body>
        <a href="helloworld">Hello World!</a>
      </body>
    </html>

/WEB-INF/views/success.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'success.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
    <h4>Hello world!</h4>
  </body>
</html>

运行测试:

  运行项目:
  
  
  
  点击,跳转到了WEB-INF/views/success.jsp:
  
  
  
  并且在控制台输出了hello world!:
  
  在MyEclipse2014环境下搭建SpringMVC并实现HelloWorld_第3张图片
  
  

你可能感兴趣的:(spring,spring,mvc,Web,MyEclipse)