第一个SpringMVC--HelloWorld

    刚开始学习Spring,做了一个HelloWorld的例子。发到博客上,算是个总结,也方便以后忘记了来查看。

    首先,我们需要创建一个web项目。我使用的时spring-tool-suite,和eclipse一样。点击File->New->Dynamic Web Project,创建一个动态网页站点,就不截图了,大家应该都会。我创建的工程名HelloWorld!。先上一张工程目录结构图。

 

  然后,需要导入spring所需的包,可以到http://www.springsource.org/download/community去下载,上面有各种版本的包,我使用的是spring-framework-3.2.2.RELEASE。我习惯如下的导包方式。在项目上点击右键,选择Build Path->Configure Build Path,在出来的窗口中点Libraries->Add JARS,然后选择要导入的包,点击OK。

  创建web.xml,如果有,直接修改即可。直接上代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
  3.   <display-name>HelloWorld</display-name>
  4.   <welcome-file-list>
  5.     <welcome-file>index.html</welcome-file>
  6.     <welcome-file>index.htm</welcome-file>
  7.     <welcome-file>index.jsp</welcome-file>
  8.     <welcome-file>default.html</welcome-file>
  9.     <welcome-file>default.htm</welcome-file>
  10.     <welcome-file>default.jsp</welcome-file>
  11.   </welcome-file-list>
  12.   <servlet>
  13.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  14.         <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
  15.         <init-param>
  16.             <param-name>contextConfigLocation</param-name>
  17.             <param-value>
  18.                 classpath*:applicationContext.xml
  19.             </param-value>
  20.         </init-param>
  21.         <load-on-startup>1</load-on-startup>
  22.     </servlet>
  23.  
  24.    <servlet-mapping>
  25.         <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
  26.         <url-pattern>/</url-pattern>
  27.     </servlet-mapping>
  28. </web-app>

  需要配置的是serverlet,使用Spring的DispatcherServerlet。

  然后配置applicationContent.xml,直接上代码:

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.        xmlns:context="http://www.springframework.org/schema/context"
  3.        xmlns:mvc="http://www.springframework.org/schema/mvc"
  4.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.        xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans
  7.         http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  8.         http://www.springframework.org/schema/context
  9.         http://www.springframework.org/schema/context/spring-context-3.2.xsd
  10.         http://www.springframework.org/schema/mvc
  11.         http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
  12.  
  13.     <context:annotation-config />
  14. //配置包扫描目录
  15.     <context:component-scan base-package="com.iris" />
  16.  
  17. //使用 springmvc机制 
  18.   <mvc:annotation-driven />
  19.  
  20.   //配置视图分发器 
  21.  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  22.         <property name="prefix">
  23.             <value>/WEB-INF/views/</value>
  24.         </property>
  25.         <property name="suffix">
  26.             <value>.jsp</value>
  27.         </property>
  28.         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  29.     </bean>
  30. </beans>

 

  创建控制器,HelloWorldController类,代码如下:

  1. package com.iris.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7.  
  8. @Controller
  9. public class HelloWorldController {
  10.  
  11.    @RequestMapping(value = "/hello", method = RequestMethod.GET)
  12.    public String hello(Model model) {
  13.       model.addAttribute("message", "Hello world!");
  14.       return "hello";
  15.    }
  16. }

    

    最后就是表现页面index.jsp

                                                    
  1. <%@ page language="java" contentType="text/html; charset=utf-8"
  2.     pageEncoding="utf-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <center>
  11.    ${message}
  12. </center>
  13. </body>
  14. </html>

   将项目部署到tomcat,在浏览器中输入http://localhost:8080/HelloWorld/hello就可访问了,如果看到如下页面就表示你成功了。

posted @ 2013-04-14 23:31  zhangteng  阅读(156)  评论(0编辑  收藏  举报