SpringMVC實例之HelloWorld

本文介紹簡單的SpringMVC的demo

步驟一:添加框架jar包

在myesclipse上創建Web project,並添加相應jar包,如圖:

這裏寫圖片描述

步驟二:在web.xml配置DispatcherServlet

DispatcherServlet爲SpringMVC的前端控制器,它負責接收請求並將請求分發到對應的控制器:

<?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">
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class> 
            org.springframework.web.servlet.DispatcherServlet 
    </servlet-class>
    <!-- 配置springMVC配置文件的名稱和位置,若不設置本節點,
    則會使用默認配置文件/WEB-INF/<servlet-name>-servlet.xml -->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

步驟三:利用註解創建控制器

package test.handlers;

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

/*
 * 
 */
@Controller
public class HelloController {
    /**
     * 1. 使用 @RequestMapping 註解來映射請求的URL
     * 2. 返回視圖會通過視圖解析器解析爲實際的物理視圖,對於 InternalResourceViewResolver 視圖解析器, 會做如下解析
     * 通過 prefix + returnVal + suffix 這樣的方式得到實際的物理視圖,然後做轉發操作
     *      如本例中的: WEB-INF/view/success.jsp
     * @return
     */
    @RequestMapping("/helloworld")
    public String hello(){
        System.out.print("hello!");
        return "success";
    }
}

步驟四:創建要跳轉的頁面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'success.jsp' starting page</title>
  </head>

  <body>
    Hello World <br>
  </body>
</html>

步驟五:創建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.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

    <!-- 配置自動掃描的包 -->
    <context:component-scan base-package="test"></context:component-scan>

    <!-- 配置視圖解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
發佈了95 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章