1.加入jar包:
这里为了省事和方便,加入了Spring4.0的全部jar包,以及必不可少的日志logging包。
2.配置web.xml文件:
主要是配置分发器DispatcherServlet:
<servlet>
<servlet-name>helloworld</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>helloworld</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
init-param参数指定了contextConfigLocation,这里DispatcherServlet会加载classpath下的spring-servlet.xml文件;如果没有指定该参数,那么DispatcherServlet会加载/WEB-INF目录下与servlet-name同名的配置文件,这里会是helloworld-servlet.xml。
load-on-startup设置为1,指定了当tomcat容器启动的时候,加载该配置文件。
url-pattern设置为"/",表示匹配任意的请求。
3.配置spring配置文件:
<?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:cxf="http://cxf.apache.org/core"
xmlns:p="http://cxf.apache.org/policy"
xmlns:ss="http://www.springframework.org/schema/security"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/policy http://cxf.apache.org/schemas/policy.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置自动扫描的包,这里表示扫描com.pk路径下的所有包 -->
<context:component-scan base-package="com.pk.*" />
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
这里的spring-servlet.xml配置文件,为了方便省事,完整的引入了Spring支持的命名空间。
视图解析器配置中的prefix和suffix分别表示视图的前缀和后缀,这里的配置意味着会将视图逻辑名解析为"/WEB-INF/pages/视图名.jsp"。
4.编写处理器:
package com.pk.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
@RequestMapping("/helloworld")
public String helloworld(){
System.out.println("Hello world...");
return "success";
}
}
5.编写视图:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
hello world!
</body>
</html>
启动tomcat服务器,输入访问路径localhost:8080/SpringDemo/helloworld.htm,会跳转到/WEB-INF/pages/success.jsp页面,打印出Hello World...