刚开始学习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,如果有,直接修改即可。直接上代码:
-
<?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" 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">
-
<display-name>HelloWorld</display-name>
-
<welcome-file-list>
-
<welcome-file>index.html</welcome-file>
-
<welcome-file>index.htm</welcome-file>
-
<welcome-file>index.jsp</welcome-file>
-
<welcome-file>default.html</welcome-file>
-
<welcome-file>default.htm</welcome-file>
-
<welcome-file>default.jsp</welcome-file>
-
</welcome-file-list>
-
<servlet>
-
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
-
<servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
-
<init-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>
-
classpath*:applicationContext.xml
-
</param-value>
-
</init-param>
-
<load-on-startup>1</load-on-startup>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
-
<url-pattern>/</url-pattern>
-
</servlet-mapping>
-
</web-app>
|
需要配置的是serverlet,使用Spring的DispatcherServerlet。
然后配置applicationContent.xml,直接上代码:
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:context="http://www.springframework.org/schema/context"
-
xmlns:mvc="http://www.springframework.org/schema/mvc"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="
-
http://www.springframework.org/schema/beans
-
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
-
http://www.springframework.org/schema/context
-
http://www.springframework.org/schema/context/spring-context-3.2.xsd
-
http://www.springframework.org/schema/mvc
-
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
-
-
<context:annotation-config />
-
//配置包扫描目录
-
<context:component-scan base-package="com.iris" />
-
-
//使用 spring的mvc机制
-
<mvc:annotation-driven />
-
-
//配置视图分发器
-
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
-
<property name="prefix">
-
<value>/WEB-INF/views/</value>
-
</property>
-
<property name="suffix">
-
<value>.jsp</value>
-
</property>
-
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
-
</bean>
-
</beans>
|
创建控制器,HelloWorldController类,代码如下:
-
package com.iris.controller;
-
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RequestMethod;
-
-
@Controller
-
public class HelloWorldController {
-
-
@RequestMapping(value = "/hello", method = RequestMethod.GET)
-
public String hello(Model model) {
-
model.addAttribute("message", "Hello world!");
-
return "hello";
-
}
-
}
|
最后就是表现页面index.jsp
-
<%@ page language="java" contentType="text/html; charset=utf-8"
-
pageEncoding="utf-8"%>
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
-
<html>
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
-
<title>Insert title here</title>
-
</head>
-
<body>
-
<center>
-
${message}
-
</center>
-
</body>
-
</html>
|
将项目部署到tomcat,在浏览器中输入http://localhost:8080/HelloWorld/hello就可访问了,如果看到如下页面就表示你成功了。