创建一个web项目

假设,我们已经安装完毕Spring所需要的依赖包,以及一些其它的扩展包,以及Jetty容器,ps:Jetty容器安装看上一节文章。


运行web项目,必须有web.xml配置文件,web.xml放置在WebContent/WEB-INF/目录下面。
[html]
< xml version="1.0" encoding="UTF-8" >
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">
运行Spring需要有applicationContext.xml这个配置文件,我们也将applicationContext.xml放置在WEB-INF/目录下。
[html]
< xml version="1.0" encoding="UTF-8" >
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
spring-servlet.xml 配置了具体的Spring需要访问的Controller文件夹目录以及模板的目录和模板的后缀名称。
[html]
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
我们需要创建一个com.mvc.rest包,在包下面创建一个名为:HelloController.java的文件。这个文件就是MVC的控制器。那么,我们有两个方法,分别为say和yes。url中访问分别是:/hello/say/和/hello/yes/
[java]
package com.mvc.rest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//@Controller 是一个标识这个是控制器类的注解标签,如果是控制器类 都需要有这个注解。
@Controller
//@RequestMapping(value="/hello") 会映射到url /hello/则访问HelloController中的Action
@RequestMapping(value="/hello")
public class HelloController {
//@RequestMapping(value="/say")